I've seen this bad mapping many times, so i wrote a post in nhibernate's
blog for future reference:
http://nhforge.org/blogs/nhibernate/archive/2011/01/08/composite-with-only-a-many-to-one-bad-idea.aspx

2011/1/8 José F. Romaniello <jfromanie...@gmail.com>

> Session.Get for composite ids work as you said (with a full entity).
> There is another approach for composite ids, using a class for the id,
> a component, in that case get expect an instance of the component
> type.
>
> Where did you read about an anonymous type?
>
> You are wrong about the composite id and you are wrong about using a
> many to one. You DONT have many profiles for a user, you have only
> one, thus it is a one-to-one association (by pk) perfectly supported
> by nhibernate.
>
> Composite ids are composite, it is not right having one column.
>
> 2011/1/8, Hendry Luk <hendrym...@gmail.com>:
> > That's because composite-id is the only way to make many-to-one
> association
> > on your ID. There's no other way.
> > You are allowed to have one or more properties as your composite-id. So
> one
> > property is completely a valid mapping.
> >
> > What's invalid here is apparently the way I retrieve the instance, it had
> a
> > semantic error. The correct way to get the object is:
> > session.Get<UserProfile>(new UserProfile{Id = user});
> >
> > Unfortunately the following code doesnt work either (it fails during the
> > logging attempt):
> > session.Get<UserProfile>(new {Id = user});
> >
> > You have to construct an actual fullblown UserProfile entity to represent
> > your ID solely for querying purpose.
> >
> > Similarly, if you have 2 properties under your composite-id, you can't do
> > this either:
> > session.Get<UserProfile>(new {Id1 = a, id2 = b});
> > That will throw an error during logging too. You always have to
> instantiate
> > your entity.
> > I havent' had a chance to investigate if this error is just a logging
> > problem during the debug-mode, and whether it will actually work just
> fine
> > in the production-mode (non-debug).
> >
> > Cheers, and so yes I have managed to get that working by instantiating a
> > dummy instance of my entity for every call to GetById, which is not
> optimal
> > but it works.
> >
> > On Fri, Jan 7, 2011 at 10:24 PM, José F. Romaniello
> > <jfromanie...@gmail.com>wrote:
> >
> >> I didn't go further on reading this message after i saw the mapping.
> >> Composite-id means composite-id, multiples columns are part of the PK
> and
> >> you have *only one.*
> >> The weird part of this, is that I'm sure you have read somewhere how bad
> >> are composite ids, however you have a pretty common case of one-to-one
> by
> >> primary key, that is very well supported by nhbiernate and you are
> trying
> >> to
> >> map it as a composite id.
> >>
> >> Read the section of one-to-one mapping,
> >> http://nhforge.org/doc/nh/en/index.html#mapping-declaration-onetoone
> >> you need to use <generator class="foreign" on UserProfile id as
> described
> >> there.
> >>
> >>
> >> 2011/1/7 Hendry Luk <hendrym...@gmail.com>
> >>
> >>> Hello,
> >>> How does one go about reporting a new bug in NH jira?
> >>> I encountered the following issue.
> >>>
> >>> <?xml version="1.0" encoding="utf-8" ?>
> >>> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
> >>>                                      assembly="MyDomain"
> >>>                                      namespace="MyDomain.Model">
> >>>
> >>>     <class name="UserProfile">
> >>>
> >>>     <composite-id >
> >>>       <key-many-to-one name="Id" column="UserId" class="User" />
> >>>     </composite-id>
> >>>
> >>>   </class>
> >>> </hibernate-mapping>
> >>>
> >>> session.Get<UserProfile>(user);
> >>>
> >>> During debug-mode, that will throw an InvalidCastException from
> >>> ComponentType.ToLoggableString() method. It's fine during non
> debug-mode.
> >>>
> >>> The cause of that error, as I tracked down, is due to an attempt to get
> a
> >>> string description from entity's ID (User object) by calling
> >>> ComponentType.ToLoggableString().
> >>> Since we specify neither the property-name nor the class attribute in
> the
> >>> <composite-id> element, nhibernate mistakenly uses
> EmbeddedComponentType
> >>> of
> >>> UserProfile class (instead of User class) in its attempt to
> >>> GuessEntityMode
> >>> of our User object. This will obviously fail, and the logger decides to
> >>> throw exception on this situation (it should at least resort to other
> >>> method.. afterall its sole purpose is just to print the string
> >>> representation of the Id object).
> >>>
> >>> There's no appropriate workaround that I can think of that wouldn't
> >>> compromise what I want to achieve. I can't specify class name in the
> >>> <composite-id> element because NH will (inappropriately) complaint that
> >>> this
> >>> entity doesnt have an ID (because I set the class without the
> >>> property-name). And obviously I can't specify the property-name because
> I
> >>> want to have "many-to-one"ed complex type as my ID.
> >>>
> >>> I strongly believe this is a bug that needs to be fixed, and make the
> >>> logging less intrusive. This issue has been raiseed in java's Hibernate
> >>> before (
> >>> http://opensource.atlassian.com/projects/hibernate/browse/HHH-3148)
> >>>
> >>> Some ways to fix this that I can think of: not relying on tuplizer for
> >>> logging at all, or may be resort to other way to produce the logging
> >>> string
> >>> in situations when no tuplizer is found (rather than throwing an
> >>> exception).
> >>>
> >>> Cheers
> >>>
> >>> --
> >>> You received this message because you are subscribed to the Google
> Groups
> >>> "nhusers" group.
> >>> To post to this group, send email to nhus...@googlegroups.com.
> >>> To unsubscribe from this group, send email to
> >>> nhusers+unsubscr...@googlegroups.com<nhusers%2bunsubscr...@googlegroups.com>
> <nhusers%2bunsubscr...@googlegroups.com<nhusers%252bunsubscr...@googlegroups.com>
> >
> >>> .
> >>> For more options, visit this group at
> >>> http://groups.google.com/group/nhusers?hl=en.
> >>>
> >>
> >>  --
> >> You received this message because you are subscribed to the Google
> Groups
> >> "nhusers" group.
> >> To post to this group, send email to nhus...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> nhusers+unsubscr...@googlegroups.com<nhusers%2bunsubscr...@googlegroups.com>
> <nhusers%2bunsubscr...@googlegroups.com<nhusers%252bunsubscr...@googlegroups.com>
> >
> >> .
> >> For more options, visit this group at
> >> http://groups.google.com/group/nhusers?hl=en.
> >>
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to nhus...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nhusers+unsubscr...@googlegroups.com<nhusers%2bunsubscr...@googlegroups.com>
> .
> > For more options, visit this group at
> > http://groups.google.com/group/nhusers?hl=en.
> >
> >
>
> --
> Enviado desde mi dispositivo móvil
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to nhus...@googlegroups.com.
To unsubscribe from this group, send email to 
nhusers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to