Reputation: 513
I'm making an application currently and I've started from creating a DB schema, which looks like this:
There are few tables (users_groups_maps, articles_tags_maps reported_articles, favourite_articles and browse_later_articles) that represent many-to-many releationship. I created a reverse configuration file and started to reverse-engineer so I could get Java classes out of it (Annotations, JDK 5 and EJB3, which results in javax.persistence.* annotations).
The basic tables (such as warnings, users) are just fine (there are fields such as
private Set<BrowseLaterArticles> browseLaterArticleses = new HashSet<BrowseLaterArticles>(0);
, but I guess it's just fine - I'm a hibernate newbie).
However, the problem (I guess) starts with those many-to-many relationships (for example UserGroupsMaps constructor looks like this:
public UsersGroupsMaps(UsersGroupsMapsId id, Users users, Groups groups) {
this.id = id;
this.users = users;
this.groups = groups;
}
, where UserGroupsMapsId is a wrapper for userId and groupId pair)
Am I getting something wrong or should it be coded differently? I know there's a ManyToMany annotation, why isn't hibernate using it?
//Additionally, I have not-so-hibernate-related question - in comments and private_messages tables, there are those parent__id fields, which should refer to the same tables. I created a foreign keys to their own tables, but I'm not sure if it's right, is it? How should it look like? In this care hibernate generates two fields with the same name: private Comments comments; in Comments class.
Regards, Marcin
Upvotes: 4
Views: 2532
Reputation: 11
to let "hibernate tools"/"Jboss tools" generate many to many annotations in your bean classes, you have to use simple mapping tables. These mapping Tables should only have columns for the both foreign keys which have to be an combined primary key.
Upvotes: 1
Reputation: 136
Your hibernate mapping is messed up somewhere. Hibernate gives you the option to not create objects for many-to-many join objects as long as there are no additional columns in that table. I assume you are using eclipse hibernate plugin? I would check the settings of the code generator to see if you can tell it not to create that object.
Upvotes: 0