与 Hibernate 工具进行逆向工程多对一单向关联的问题

发布于 2024-10-21 14:47:13 字数 864 浏览 2 评论 0原文


我在 Eclipse (Helios) 中使用 Hibernate 工具 3.40。 我正在尝试使用 EJB3 样式(即 JPA2.0 注释)从数据库 (MSSQL 2008) 生成 POJO。
假设我有两个表 AB,其中有一个从 AB 的外键。
默认情况下,这将为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双手揣兜 2024-10-28 14:47:13

创建一个用于复仇的类。位于 Hibernate 代码生成配置 的策略

示例:

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;
   }
}

方法 excludeForeignKeyAsCollection 的 JavaDoc

Should this foreignkey be excluded as a oneToMany 

,还有另一个方法调用 excludeForeignKeyAsManytoOne

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

Create a class for reveng. strategy at Hibernate Code Generation Configuration

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

Should this foreignkey be excluded as a oneToMany 

and there also have another method call excludeForeignKeyAsManytoOne

Should this foreignkey be excluded as a many-to-one 
凉风有信 2024-10-28 14:47:13

目前(使用 Hibernate Tools 5.2 进行测试),生成单向多对一作品。

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

例如(重命名属性)

 <!-- 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>

或者(不包括属性)

 <!-- 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>

因此,要仅与 @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>

您还可以在此处使用 Docker 获取我的示例数据库的实例:

docker pull ghusta/postgres-world-db:2.1

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)

 <!-- 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 :

<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 pull ghusta/postgres-world-db:2.1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文