Which brings me to this question:
Is there any way to have association as your primary key?

E.g. something that allows you to say session.Add(new UserProfile(user)),
and session.Get<UserProfile>(user)?

Cheers

On Mon, Jan 10, 2011 at 11:02 AM, Hendry Luk <hendrym...@gmail.com> wrote:

> ## Errata...
>
> I didnt use one-to-one because I wanted to identify my UserProfile by its
> User. One-to-one mapping in NH requires me to have an artificial integer to 
> identify
> my UserProfile entity. It's just unnatural for the rest of my code. For
> example, my repository would therefore have to use int as its TKey. (e.g..
> userProfileRepository.Get(user.Id), as well as all other methods that work
> on Id).
>
> The more natural way to approach this from my domain-centric view is for
> UserProfile to be identified by User (instead of by an obscure integer),
> which was the first thing I attempted to model, by placing an association as
> the Id of my entity.
> It seemed to be a very sensible and easy thing to do.
>
> On the DB side, I simply place an FK on my PK (one-to-one).
> On model-side, I simply have a "User user" property as my Id.
> On the NH side, it seemed that composite-id would be what I had to use.
> Wasn't quite successful.
>
> Now that I find that there's no clean way to map an association as your
> entity ID in NHibernate, I think I might have to revert back to one-to-one,
> but I'll have to refactor the rest of my code to work with my UserProfile
> entity using integer values.
> We do our domain modelling using a model-first approach, and try to get NH
> to work out the mapping to fit the model, rather than modelling the domain
> to fit NH mapping. That's how I got to this problem, and hence the need to
> refactor the code if I'm to revert back to using integer in order to have
> so-called "cleaner" NH mapping (using one-to-one). Might be easier to stick
> with composite-id and change my code into session.Get<UserProfile>(new
> UserProfile(user));
>
> Having said that, crashing during the logging doesnt seem ideal to me.
> Logging module shouldn't really cause an exception.
>
> On Sat, Jan 8, 2011 at 8:12 PM, José F. Romaniello <jfromanie...@gmail.com
> > wrote:
>
>> 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<nhusers%2bunsubscr...@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.
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to