-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Two collections of same class
PostPosted: Fri Jul 11, 2008 8:59 pm 
Regular
Regular

Joined: Mon Aug 22, 2005 1:11 pm
Posts: 50
Location: Pasadena, CA
I have a few classes similar to this example:
Code:
public class User{

List<Group> primaryGroups;
List<Group> secondaryGroups;

}

I want Hibernate to generate the schema for me, for everything else in my app it was worked great.

When it see two collection to the same class annotated with either @Many-to-Many or @CollectionOfElements it creates one table between User and Group with one column for each collection.

ie
Code:
CREATE TABLE USER_GROUP (

USER_PK INTEGER NOT NULL
PRIMARYGROUP_GROUP_PK INTEGER NOT NULL
SECONDARYGROUP_GROUP_PK INTEGER NOT NULL

)

The problem with this is that I cannot have either collection be empty. Since all of the columns are keys they are not allowed to be null.

Is there away to annotate this so they it creates separate tables or allows for nulls?

I have been beating on this all day, any help would be greatly appreciated.

Thanks,
Mike


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 14, 2008 5:34 pm 
Newbie

Joined: Mon Jul 14, 2008 1:49 pm
Posts: 1
I am having a similar problem. Is there an annotation that can be set so that Hibernate will create a separate map table for each Collection?

Thanks,
-Steve

_________________
Steve Orin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 15, 2008 1:41 pm 
Regular
Regular

Joined: Mon Aug 22, 2005 1:11 pm
Posts: 50
Location: Pasadena, CA
The only I could think of to have Hibernate generate one table per collection would be to create different class for each collection:

ie

Code:

public abstract class Group{

//The group class

}

public abstract class PrimaryGroup extends Group{

}

public abstract class SecondaryGroup extends Group{

}




Then the user would look something like this:
Code:


public class User{

List<PrimaryGroup> primaryGroups;
List<SecondaryGroup> secondaryGroups;

}



But that would mean changing a lot of my existing objects and some of my business logic.

If there is no other way that's what I will do, but it seems like this could be done some other way.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 7:37 pm 
Regular
Regular

Joined: Mon Aug 22, 2005 1:11 pm
Posts: 50
Location: Pasadena, CA
So I tired my suggestion above of creating two abstract children and I have the same problem I started with.

Any help or ideas that I can try would be greatly appreciated.

Thanks,
Mike


Top
 Profile  
 
 Post subject: two collections same class
PostPosted: Fri Jul 18, 2008 10:58 am 
Newbie

Joined: Tue Feb 20, 2007 10:00 pm
Posts: 8
First of all, I think you may need to convert your List declarations to Set. I have had issues with Hibernate saying it could not retrieve two bags simultaneously when I had multiple attributes of a class which were of type List as the collection class. Furthermore, a set actually is semantically correct. It would not make sense for a user to have the same Group object in either collection multiple times which a List would allow.

For your original issue, try explicitly specifying two join tables using annotations:

@ManyToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "ID")
@JoinTable(name = "USER_PRIMARY_GROUP_MAP",
joinColumns = @JoinColumn(name = "USER_ID"),
inverseJoinColumns = @JoinColumn(name = "GROUP_ID"))
protected Set<Group> primaryGroups = new LinkedHashSet<Group>() ;

@ManyToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "ID")
@JoinTable(name = "USER_SECONDARY_GROUP_MAP",
joinColumns = @JoinColumn(name = "USER_ID"),
inverseJoinColumns = @JoinColumn(name = "GROUP_ID"))
protected Set<Group> secondaryGroups = new LinkedHashSet<Group>() ;

NOTE: The above assumes that the USER and GROUP tables both are using ID as
their primary key column.

I have gotten away from using external hbm.xml mapping files but if you are using
the xml mapping files, I assume there is an equivalent mechanism for accomplishing
what the annotations above do.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 18, 2008 10:45 pm 
Regular
Regular

Joined: Mon Aug 22, 2005 1:11 pm
Posts: 50
Location: Pasadena, CA
Thanks for the reply.

I had it setup that way originally.

The problem with that is that this is class is the parent of many other classes so hibnerate puts constraints on all of the child tables. So I can never insert anything since no one mapping is going to exist in all of the child tables.

As far as List or Set agreed is should be set but its not my call :)

What I ended up doing was leaving it the way I had it but manually removing the "not null" on the two collection foreign keys. Seems to be working.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.