Sim*_*mon 3 hibernate unique-constraint
我们有一个基于 Hibernate (33.) 的 Web 应用程序,并发现与交换受唯一索引限制的数据值对相关的问题。在我们的机票 HBM 中,我们有
<many-to-one name="participants" class="net.umbrella.entity.ParticipantModel" fetch="select">
<column name="PARTICIPANTID" not-null="false" />
</many-to-one>
<many-to-one name="flights" class="net.umbrella.entity.FlightModel" fetch="select">
<column name="FLIGHTID" not-null="true" />
</many-to-one>
Run Code Online (Sandbox Code Playgroud)
从功能上讲,可以采用分配给参与者 P1 的票证 A 和分配给参与者 P2 的票证 B 的数据对并交换两个参与者。票证 A 最终将分配给参与者 P2,票证 B 将分配给参与者 P1。然而,FLIGHTID + PARTICIPANTID 有一个唯一的约束。
当 Hibernate 发出第一次更新以将票证 A 上的参与者 P1 更改为 P2 时,会引发 GenericJDBCException
Attempt to insert duplicate key row in object 'FLIGHTTICKET' with unique index 'IDX_FLIGHTTICKET_UQ'
Run Code Online (Sandbox Code Playgroud)
有人对此有共同的解决方案吗?
谢谢西蒙
所以,你有这样的东西:
Participant temp = pair1.getParticipant();
pair1.setParticipant(pair2.getParticipant());
pair2.setParticipant(temp);
Run Code Online (Sandbox Code Playgroud)
相反,只需执行以下操作:
Participant temp1 = pair1.getParticipant();
Participant temp2 = pair2.getParticipant();
pair2.setParticipant(null);
session.flush();
pair1.setParticipant(temp2);
pair2.setParticipant(temp1);
Run Code Online (Sandbox Code Playgroud)
中间冲洗将确保一次只有一对引用参与者。
归档时间: |
|
查看次数: |
1360 次 |
最近记录: |