Hibernate在数据库表中为双向关联创建了多个外键

时间:2019-02-20 10:07:48

标签: java hibernate jpa orm

我想创建具有相应关联的下表: enter image description here

但是,似乎我已经模仿了一些东西,因为当Hibernate创建表时,会创建多个外键,如下所示:

  

信息:HHH10001501:从JdbcConnectionAccess获得的连接   [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@5e5595f3]   对于(非JTA)DDL执行未处于自动提交模式;的   连接“本地事务”将被提交,并且连接   将设置为自动提交模式。

     

休眠:创建表APP.ITEMS(ITEM_ID整数,由   默认为身份,NAME varchar(100),NET_PRICE整数,VAT_RATE   整数,CATEGORY_ID整数不为空,主键(ITEM_ID))

     

休眠:创建表APP.LIMIT(LIMIT_ID整数不为null,   LIMIT_VALUE整数,主键(LIMIT_ID))

     

休眠:创建表APP.PARTNER(由PARTNER_ID生成的整数   默认为身份,PARTNER_NAME varchar(100),主键   (PARTNER_ID))

     

休眠:创建表APP.TRANSACTIONS(TRANSACTIONS_ID整数   默认情况下作为标识生成,FLOW整数不为null,NET_PRICE   整数,ITEM_ID整数不为空,通知单整数,ADVICENOTE_ID   整数不为null,主键(TRANSACTIONS_ID))

     

休眠:更改表APP.TRANSACTIONS添加约束   UK_r7btjtpy72nugbhuia8y0xnk6唯一(ITEM_ID)

     

休眠:更改表APP.ADVICENOTE添加约束   FK4n4r6ej5i983gk8fqj20jl899 外键(伙伴)参考   APP.PARTNER

     

休眠:更改表APP.ADVICENOTE添加约束   FKr96drd4j6pds8vocvsstcd2a2 外键(PARTNER_ID)参考   APP.PARTNER

     

休眠:更改表APP.ITEMS添加约束   FKbbkng91eiiqu522okqq0nq7pm外键(CATEGORY_ID)参考   应用类别

     

休眠:更改表APP.TRANSACTIONS添加约束   FK13kk35nfl3iff7f2cs8er8w0s 外键(建议)参考   APP.ADVICENOTE

     

休眠:更改表APP.TRANSACTIONS添加约束   FK8ilbnqyk8lemxj0yvbqm3p7tt外键(ITEM_ID)引用APP.ITEMS

     

休眠:更改表APP.TRANSACTIONS添加约束   FKqrbn7tojjjkjyusy98a300btp 外键(ADVICENOTE_ID)参考   APP.ADVICENOTE

     

休眠:从APP.CATEGORIES中选择类别0_.NAME作为col_0_0_   类别0 _

     

休眠:选择items0_.ITEM_ID作为ITEM_ID1_2_,选择items0_.NAME作为   NAME2_2_,项目0_.NET_PRICE为NET_PRIC3_2_,项目0_.VAT_RATE为   来自APP.ITEMS的VAT_RATE4_2_,项0_.CATEGORY_ID为CATEGORY5_2_   items0 _

     

休眠:选择items0_.ITEM_ID作为ITEM_ID1_2_,选择items0_.NAME作为   NAME2_2_,项目0_.NET_PRICE为NET_PRIC3_2_,项目0_.VAT_RATE为   来自APP.ITEMS的VAT_RATE4_2_,项0_.CATEGORY_ID为CATEGORY5_2_   items0 _

     

休眠:从APP.CATEGORIES中选择类别0_.NAME作为col_0_0_   类别0 _

     

休眠:选择items0_.ITEM_ID作为ITEM_ID1_2_,选择items0_.NAME作为   NAME2_2_,项目0_.NET_PRICE为NET_PRIC3_2_,项目0_.VAT_RATE为   来自APP.ITEMS的VAT_RATE4_2_,项0_.CATEGORY_ID为CATEGORY5_2_   items0 _

这是正常行为还是我缺少什么?

这是我的XML映射:

Advicenote.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Feb 9, 2019, 3:57:52 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="classes.Advicenote" table="ADVICENOTE" schema="APP" optimistic-lock="version">
        <id name="advicenoteId" type="int" column="ADVICENOTE_ID">
            <generator class="native" />
        </id>
        <property name="advicedate" type="date" column="ADVICE_DATE" length="10" />
        <property name="inOrOut" type="java.lang.Short" column="IN_OR_OUT" />
        <many-to-one 
            name="partner" class="classes.Partner"/>  
        <set name="transactions" table="TRANSACTIONS" inverse="false" cascade="all" lazy="true" fetch="select">       
            <key column="ADVICENOTE_ID" not-null="true"/>
            <one-to-many class="classes.Transactions"/>
        </set>
    </class>
</hibernate-mapping>

Categories.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="classes.Categories" table="CATEGORIES" schema="APP" optimistic-lock="version">
        <id name="categoryId" type="int">
            <column name="CATEGORY_ID" />
            <generator class="native" />
        </id>
        <property name="name" type="string">
            <column name="NAME" length="100" />
        </property>
     <set name="items" table="ITEMS" inverse="true" cascade="all" lazy="false" fetch="select">
        <key>
            <column name="CATEGORY_ID" not-null="true" />
        </key>
        <one-to-many class="classes.Items" />
    </set>
    </class>
</hibernate-mapping>

Items.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="classes.Items" table="ITEMS" schema="APP" optimistic-lock="version">
        <id name="itemId" type="int" column="ITEM_ID">
            <generator class="native" />
        </id>
        <property name="name" type="string" column="NAME" length="100"/>
        <property name="netPrice" type="java.lang.Integer" column="NET_PRICE"/>
        <property name="vatRate" type="java.lang.Integer" column="VAT_RATE"/>
        <many-to-one name="category" class="classes.Categories" fetch="select" column="CATEGORY_ID" not-null="true" lazy="false"/>
    </class>
</hibernate-mapping>

Limit.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="classes.Limit" table="LIMIT" schema="APP" optimistic-lock="version">
        <id name="limitId" type="java.lang.Integer" column="LIMIT_ID">
            <generator class="assigned"/>
        </id>
        <property name="value" type="int" column="LIMIT_VALUE" length="1000"/>
    </class>
</hibernate-mapping>

Partner.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="classes.Partner" table="PARTNER" schema="APP">
        <id name="partnerId" type="int">
            <column name="PARTNER_ID"/>
            <generator class="native"/>
        </id>
        <property name="partnerName" type="string" column="PARTNER_NAME" length="100"/>
        <set name="advicenotes" table="ADVICENOTE" inverse="false" cascade="all" lazy="true" fetch="select">
            <key column="PARTNER_ID" not-null="true"/>
            <one-to-many class="classes.Advicenote" />
        </set>
            </class>
</hibernate-mapping>

Transactions.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="classes.Transactions" table="TRANSACTIONS" schema="APP" optimistic-lock="version">
        <id name="transactionsId" type="int" column="TRANSACTIONS_ID">
            <generator class="native" />
        </id>
        <property name="flow" type="int" column="FLOW" not-null="true" />
        <property name="netPrice" type="java.lang.Integer" column="NET_PRICE" />

        <many-to-one name="advicenote" class="classes.Advicenote"/>
        <many-to-one name="item" class="classes.Items" fetch="select" cascade="save-update" column="ITEM_ID" unique="true" not-null="true"/>
    </class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:0)

缺少AdviceNote映射JoinColumn

<many-to-one name="partner" class="classes.Partner">
   <join-column name="PARTNER_ID"/>
</many-to-one>