-
1. Re: Map multiple rows to one object?
texan May 4, 2007 7:46 PM (in response to texan)To clarify, I want the primary key of the UserPreference class to effectively be distinct(user_id,preference_id).
-
2. Re: Map multiple rows to one object?
swjackson May 8, 2007 1:58 PM (in response to texan)If I am understanding your question you need to have the relationship in your User POJO that returns the collection.
in your setter method add the annotation for @JoinTable to enforce the sql relationship you want
you also may have to implement Comparable to enforce the collection you want on UserPreference.
Another approach that may be more effective is to use a NamedQuery -
3. Re: Map multiple rows to one object?
texan May 8, 2007 2:27 PM (in response to texan)The problem I have is that, in the table, the combination of user_id and preference_id are not unique. I haven't found any way to have distinct(user_id,preference_id) as the primary key while also rolling up the multiple rows into a collection based on the value column.
Frankly, I don't think Hibernate can support this, unless there's some sort of custom PK mechanism that could be provided.
The simple (but ugly) solution is to create a User_Preference table with just (user_id,preference_id) columns. Then I can map the UserPreference object to that and do the collection mapping for the values.
I was just trying to avoid having to create an extra table that adds no value. -
4. Re: Map multiple rows to one object?
swjackson May 8, 2007 4:38 PM (in response to texan)You can create a custom ID by using (Assuming you are using ejb3)
@Embeddable and @EmbeddedId
if you are not using ejb3 (assume you are using xdoclets)
create a new class that has your columns you want to be apart of the id
only add the xdoclets tags to the getter methods
Then add that class as the id