简体   繁体   English

在休眠状态下为表创建唯一索引,其中一列是外键

[英]Creating unique Index for table in hibernate with one column being a foreign key

well I am not that advanced in hibernate and I tried to search a lot, found lots of similar questions but none really applying my case. 好吧,我的休眠状态不那么先进,我尝试了很多搜索,发现了很多类似的问题,但没有一个真正适用我的情况。 I have a table TABLE1 which has an index "id" and another table which has as index (TABLE1_ID and NUMBER). 我有一个表TABLE1,该表的索引为“ id”,另一个表为索引(TABLE1_ID和NUMBER)。 Only TABLE1_ID is a foreign key while number doesn't reference anything specific 只有TABLE1_ID是外键,而数字未引用任何特定内容

TABLE 1 has the following hibernate mapping 表1具有以下休眠映射

 <class name="com.test.basic.BASICTABLE1"
    entity-name="com.test.TABLE1" table="TABLE1"
    dynamic-update="true" optimistic-lock="version">
    <id name="id" type="long">
        <column name="id" />
        <generator class="native">
            <param name="sequence">${table1_id.generator.sequence}</param>
        </generator>
    </id>
    <version name="versionNumber" column="verno" generated="always"
        access="field" />
    <property name="column1" column="column1" type="string" length="19"
        not-null="true" access="field" />
 </class>

I am not sure what should be the mapping for the other table. 我不确定其他表的映射应该是什么。 I did it the following way 我是通过以下方式做到的

  <class name="com.test.basic.BASICTABLE2"
    entity-name="com.test.TABLE2" table="table2"
    dynamic-update="true" optimistic-lock="version">

    <composite-id name="id" class="com.test.basic.TABLE1TABLE2Id" >
      <key-property name="TABLE1_ID" column="TABLE1_ID" type="long" />
      <key-property name="NUMBER" column="NUMBER" type="short"/>
    </composite-id>
 </class>

Please note that I created the class TABLE1TABLE2Id after I read somewhere that there should be some intermediate mapping. 请注意,在阅读到应该有一些中间映射的地方之后,我创建了TABLE1TABLE2Id类。

I'm sure there's something wrong with TABLE2 mapping (I am not so advanced with hibernate) but when trying to install the app, I am getting the following error 我确定TABLE2映射有问题(我对休眠的了解不那么高),但是在尝试安装应用程序时,出现以下错误

 Foreign key (TABLE1 [id]) must have same number of columns as the referenced primary key (TABLE2 [TABLE1_ID, NUMBER])

I appreciate anybody's help thanks :) 我感谢任何人的帮助,谢谢:)

Identity copy (foreign generator) 身份副本(外部生成器)

Finally, you can ask Hibernate to copy the identifier from another associated entity. 最后,您可以要求Hibernate复制另一个关联实体的标识符。 In the Hibernate jargon, it is known as a foreign generator but the JPA mapping reads better and is encouraged. 在Hibernate术语中,它被称为外来生成器,但JPA映射读起来更好,值得鼓励。

The primary key from one entity might be as foreign key be primary key for another table. 来自一个实体的主键可能作为外键成为另一张表的主键。

@Entity
class MedicalHistory implements Serializable {
  @Id Integer id;

  @MapsId @OneToOne
  @JoinColumn(name = "patient_id")
  Person patient;
}

@Entity
class Person {
  @Id @GeneratedValue Integer id;
}

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

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