简体   繁体   English

逆向工程与hibernate工具的多对一单向关联问题

[英]Problem with reverse engineering a many-to-one unidirectional association with hibernate tools


I'm using Hibernate tools 3.40 in Eclipse (Helios). 我在Eclipse(Helios)中使用Hibernate工具3.40。 I'm trying to generate POJOs from my DB (MSSQL 2008) with EJB3 style (ie JPA2.0 annotations). 我正在尝试使用EJB3样式(即JPA2.0注释)从我的数据库(MSSQL 2008)生成POJO。
Let's say I have two tables A and B where there is a foreign key from A to B . 假设我有两个表AB ,其中有一个从AB的外键。
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"). 默认情况下,这会生成A的POJO,其中B作为成员(其“父”)和B的POJO,其中Set<A>作为成员(其“子”)。
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). 我想知道如何控制rev-eng以便只创建关联的一面(我有不同的用例,所以基本上所有三个选项对我来说都很重要)。
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 我不想使用hbm.xml文件,因为我正在使用注释和JPA2.0但是我可以通过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. 我已经尝试配置foreign-key属性并在那里定义exclude=true但这只为我提供了一个场景的半答案。 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; 这生成了A带有bPK int成员的A POJO,这是可以容忍和可理解的, 但是生成的B POJO现在不能编译,因为one-to-many注释具有无效属性; The mappedby="unresolved" due to the fact that A no longer has a property which hibernate reveng can map back to. 由于A不再具有hibernate reveng可以映射回的属性这一事实, mappedby="unresolved"

So, I currently cannot create uni-directional associations and I'd appreciate any help. 所以,我目前无法创建单向关联,我会感激任何帮助。

Create a class for reveng. strategy 创建一个reveng. strategy的类reveng. strategy reveng. strategy at Hibernate Code Generation Configuration Hibernate Code Generation Configuration reveng. strategy

Example : 示例:

public class MyReverseEngineeringStrategy extends DelegatingReverseEngineeringStrategy {

   public MyReverseEngineeringStrategy(ReverseEngineeringStrategy delegate) {
       super(delegate);
   }

   @Override
   public void setSettings(ReverseEngineeringSettings settings) {
       super.setSettings(settings);
   }

   @Override
   public boolean excludeForeignKeyAsCollection(String keyname, 
    TableIdentifier fromTable, java.util.List fromColumns, 
    TableIdentifier referencedTable, java.util.List referencedColumns) {

    // TODO : Your work here
    if (keyname.equals("___") && 
        fromTable.getName().equals("___") && 
        fromColumns.contains("___") && 
        referencedTable.getName().equals("___") && 
        referencedColumns.contains("___")) {

        return true;
    }

    return false;
   }
}

JavaDoc for method excludeForeignKeyAsCollection 方法excludeForeignKeyAsCollection JavaDoc

Should this foreignkey be excluded as a oneToMany 

and there also have another method call excludeForeignKeyAsManytoOne 并且还有另一个方法调用excludeForeignKeyAsManytoOne

Should this foreignkey be excluded as a many-to-one 

Currently ( tested with Hibernate Tools 5.2 ), generating unidirectional many-to-one works. 目前( 使用Hibernate Tools 5.2测试 ),生成单向多对一工作。

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 : 在文档( https://docs.jboss.org/tools/4.0.0.Final/en/hibernatetools/html_single/index.html#hibernaterevengxmlfile )中,您可以看到您可以排除关系的某些方面:

For example (renaming properties) 例如(重命名属性)

 <!-- control many-to-one and set names for a specific named foreign key constraint -->
 <foreign-key constraint-name="ORDER_CUST">
   <many-to-one property="customer"/>
   <set property="orders"/>
 </foreign-key>

Or (excluding properties) 或(不包括房产)

 <!-- can also control a pure (shared pk) one-to-one  -->
  <foreign-key constraint-name="ADDRESS_PERSON">
   <one-to-one exclude="false"/>
   <inverse-one-to-one exclude="true"/>
  </foreign-key>

So to have just one side of the relationship with @ManyToOne only, you could do the following : 因此,要仅与@ManyToOne建立关系的一方,您可以执行以下操作:

<table name="city" schema="public">
    <primary-key property="id">
        <key-column name="id" type="integer"/>
    </primary-key>
</table>

<table name="country" schema="public">
    <foreign-key constraint-name="country_capital_fkey" foreign-schema="public">
        <many-to-one property="capital" exclude="false"/>
        <set exclude="true" />
    </foreign-key>
</table>

You can also fetch an instance of my sample database with Docker here : 您还可以在此处使用Docker获取示例数据库的实例:

docker pull ghusta/postgres-world-db:2.1 docker pull ghusta / postgres-world-db:2.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM