为什么 hibernate 在映射多对多关系时会生成额外的实体?
我目前正在制作一个应用程序,并且已经开始创建数据库架构,如下所示:
那里有几个表(users_groups_maps、articles_tags_maps、reported_articles、favourite_articles 和 browser_later_articles)表示多对多关系。我创建了一个反向配置文件并开始进行反向工程,这样我就可以从中获取 Java 类(注释、JDK 5 和 EJB3,这会产生 javax.persistence.* 注释)。
基本表(例如警告、用户)就很好(有诸如 之类的字段
private Set<BrowseLaterArticles> browseLaterArticleses = new HashSet<BrowseLaterArticles>(0);
,但我想这也很好 - 我是一个 hibernate 新手)。
但是,问题(我猜)始于那些多对多关系(例如 UserGroupsMaps 构造函数如下所示:
public UsersGroupsMaps(UsersGroupsMapsId id, Users users, Groups groups) {
this.id = id;
this.users = users;
this.groups = groups;
}
,其中 UserGroupsMapsId 是 userId 和 groupId 对的包装器)
我是否出错了,或者应该以不同的方式编码?我知道有一个 ManyToMany 注释,为什么 hibernate 不使用它?
//此外,我有与休眠相关的问题 - 在 comments 和 private_messages 表中,有那些parent__id 字段,它们应该引用相同的表。我为自己的表创建了外键,但我不确定它是否正确,是吗?它应该是什么样子?在这种情况下,hibernate 会生成两个同名的字段: 私人评论评论; 在评论课上。
问候, 马尔钦
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了让“hibernate 工具”/“Jboss 工具”在您的 bean 类中生成多对多注释,您必须使用简单的映射表。
这些映射表应该只包含两个外键的列,这两个外键必须是组合主键。
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.
你的休眠映射在某个地方搞砸了。只要该表中没有其他列,Hibernate 就可以让您选择不为多对多联接对象创建对象。我假设你正在使用 eclipse hibernate 插件?我会检查代码生成器的设置,看看是否可以告诉它不要创建该对象。
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.