【问题标题】:HIbernate tools reverse enginering creates a collection on a unique columnHIbernate 工具逆向工程在唯一列上创建集合
【发布时间】:2014-08-25 18:16:14
【问题描述】:

有一个表 A 和一个表 B,A 对 B 有一个 fk。 在 A 中,此 FK 的列是唯一且可为空的。这意味着,一个 B 仅被引用为一个 A 的最大值。

但是休眠工具逆向工程正在生成pojos:

Pojo A 带有字段 B。好的,没错。 Pobo B 带有一组 A。不,不是,应该是单个字段 A。

怎么了?我错过了什么?

这是我的 reveng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering SYSTEM "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>

<type-mapping>
    <sql-type jdbc-type="BIGINT" hibernate-type="java.math.BigInteger" />
    <sql-type jdbc-type="NUMERIC" precision="19" hibernate-type="java.math.BigInteger"/>
    <sql-type jdbc-type="NUMERIC" precision="10" not-null="true" hibernate-type="int"/>
    <sql-type jdbc-type="NUMERIC" precision="10" not-null="false" hibernate-type="java.lang.Integer"/>
    <sql-type jdbc-type="NUMERIC" precision="4" not-null="true" hibernate-type="byte"/>
    <sql-type jdbc-type="NUMERIC" precision="4" not-null="false" hibernate-type="java.lang.Byte"/>
    <sql-type jdbc-type="OTHER" hibernate-type="timestamp"/>
</type-mapping>

<table-filter match-name="MLOG.*"  exclude="true" /> 
<table-filter match-name="RUPD.*"  exclude="true" />  

<!-- Forzamos a que el id sea Integer. Al eliminar el autoincremental hibernate pasa este campo a int.-->
<!-- Se cambia el codigo para que no afecte el tipo-->
<!--
<table name="cpanel_bloque_canal" catalog="ob_cpanel"> 
    <primary-key>                       
        <key-column name="idBloque" type="java.lang.Integer"/>
    </primary-key>
</table>
-->

【问题讨论】:

  • 你能发布相关的hibernate.reveng.xml文件吗?

标签: java hibernate reverse-engineering hibernate-tools


【解决方案1】:

映射是正确的,因为在这种情况下,表 B 中的一条记录是从表 A 中的几条记录中引用的。

例子

  • 表A数据:
  • ID ID_FROM_B
  • 1 1
  • 2 1
  • 3 2

  • 表 B 数据:

  • 身份证
  • 1
  • 2

如果您从表 B 中获取记录并将 A 记录加入其中,您将获得结果:

对于 B ID 1:ID(TableA) 1; ID(TableA) 2 -- 这必须放入一个集合中,这就是为什么你会得到这样的 pojo 结构。

【讨论】:

  • 但是如果 A 的 FK 是 UNIQUE ,那么表 B 中的一条记录如何被表 A 中的几条记录引用?
  • 你使用什么数据库?据我所知,您所描述的场景并不意味着这种关系是一对一的。
  • 看看这个在mysql中创建一对一:mkyong.com/mysql/how-to-define-one-to-one-relationship-in-mysql
  • 这不能解决我的问题,因为关系应该为空。我的意思是,关系是 0:1 - 1
猜你喜欢
  • 2017-06-23
  • 2012-03-15
  • 1970-01-01
  • 1970-01-01
  • 2014-11-24
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多