休眠中的重复条目

时间:2015-07-27 10:37:15

标签: java spring hibernate hibernate-mapping

我们的数据库中有instanceintegration个表。对于integration表格中的每个条目,我们希望存储来源instance和目标instance

我正在尝试通过设置已有的integration来保存instance个对象。但它正在创建重复的instance条目。

Integration integration = new Integration();
integration.setSourceInstance(sourceInstance); // setting source instance object
integration.setTargetInstance(targetInstance); // setting target instance object

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(integration);
tx.commit();

session.close()
  

Integration.java

private long integrationId;
private Instance sourceInstance;
private Instance targetInstance;
  

Instance.java

private long InstanceId=1L;
private String instanceName;
private String instanceVersion;
  

HBM 文件

<hibernate-mapping package="com.alu.jcb.model">
  <class name="Instance" table="INSTANCE">
     <id name="InstanceId" column="instance_id" type="java.lang.Long">
        <generator class="sequence" />
     </id>

     <property name="instanceName" type="string" column="instance_name" not-null="true" />
     <property name="instanceVersion" type="string" column="instance_version" />              
  </class>
</hibernate-mapping>

<hibernate-mapping package="com.alu.jcb.model">
  <class name="Integration" table="INTEGRATION">
    <id name="integrationId" column="integration_id" type="java.lang.Long">
      <generator class="sequence" />
    </id>
    <many-to-one name="sourceInstance" class="com.alu.jcb.model.Instance" fetch="select" cascade="save-update" >
       <column name="source_instance_id" not-null="true"  />
    </many-to-one>
    <many-to-one name="targetInstance" class="com.alu.jcb.model.Instance" fetch="select" cascade="save-update">
       <column name="target_instance_id" not-null="true" />
    </many-to-one>
  </class>
</hibernate-mapping>

0 个答案:

没有答案