我们的数据库中有instance
和integration
个表。对于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>