与 Hibernate 工具进行逆向工程多对一单向关联的问题
我在 Eclipse (Helios) 中使用 Hibernate 工具 3.40。 我正在尝试使用 EJB3 样式(即 JPA2.0 注释)从数据库 (MSSQL 2008) 生成 POJO。
假设我有两个表 A
和 B
,其中有一个从 A
到 B
的外键。
默认情况下,这将为 A
生成一个 POJO,其中 B
作为成员(其“父级”),并为 B
生成一个 POJO,其中 B
具有a Set
作为成员(其“子级”)。
我想知道如何控制 rev-eng,以便只创建关联的一侧(我有不同的用例,所以基本上所有三个选项对我都很重要)。
我不想使用 hbm.xml 文件,因为我正在使用注释和 JPA2.0,但我可以在逆向工程过程中指定一些元数据以通过 hibernae.reveng.xml 进行休眠
我已经尝试配置 foreign -key
属性并在那里定义 exclude=true
但这只为我提供了一种情况的一半答案。这生成了一个带有 bPK int 成员的 A
POJO,这是可以容忍和理解的但是生成的 B
POJO 现在不会像 那样编译>one-to-many
注释具有无效属性; mappedby="unresolved"
由于 A
不再具有 hibernate reveng 可以映射回的属性。
因此,我目前无法创建单向关联,希望得到任何帮助。
I'm using Hibernate tools 3.40 in Eclipse (Helios).
I'm trying to generate POJOs from my DB (MSSQL 2008) with EJB3 style (i.e. JPA2.0 annotations).
Let's say I have two tables A
and B
where there is a foreign key from A
to B
.
This generates, by default, a POJO for A
which has B
as a member (its "parent") and a POJO for B
which has a Set<A>
as a member (its "children").
What I'd like is to know how I can control the rev-eng so that only one side of the association is created (I have different use cases so basically all three options are important for me).
I do not want to use hbm.xml files as I'm working with annotations and JPA2.0 but I can specify some metadata on the reverse engineering process to hibernate via hibernae.reveng.xml
I've tried configuring the foreign-key
attribute and defining there the exclude=true
but that only provided me with a half an answer for one scenario. That generated an A
POJO with a bPK int member which is tolerable and understandable but the generated POJO of B
now does not compile as the one-to-many
annotation has an invalid attribute; The mappedby="unresolved"
due to the fact that A
no longer has a property which hibernate reveng can map back to.
So, I currently cannot create uni-directional associations and I'd appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个用于复仇的类。位于
Hibernate 代码生成配置
的策略示例:
方法
excludeForeignKeyAsCollection
的 JavaDoc,还有另一个方法调用
excludeForeignKeyAsManytoOne
Create a class for
reveng. strategy
atHibernate Code Generation Configuration
Example :
JavaDoc for method
excludeForeignKeyAsCollection
and there also have another method call
excludeForeignKeyAsManytoOne
目前(使用 Hibernate Tools 5.2 进行测试),生成单向多对一作品。
在文档中(https:// docs.jboss.org/tools/4.0.0.Final/en/hibernatetools/html_single/index.html#hibernaterevengxmlfile),您可以看到可以排除关系的某些方面:
例如(重命名属性)
或者(不包括属性)
因此,要仅与 @ManyToOne 建立关系的一侧,您可以执行以下操作:
您还可以在此处使用 Docker 获取我的示例数据库的实例:
Currently (tested with Hibernate Tools 5.2), generating unidirectional many-to-one works.
In the documentation (https://docs.jboss.org/tools/4.0.0.Final/en/hibernatetools/html_single/index.html#hibernaterevengxmlfile), you can see that you can exclude some side of the relationship :
For example (renaming properties)
Or (excluding properties)
So to have just one side of the relationship with @ManyToOne only, you could do the following :
You can also fetch an instance of my sample database with Docker here :