gpt4 book ai didi

java - 使用 @OrderColumn 和 @OneToMany 保存实体时出现 ConstraintViolationException

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:40 34 4
gpt4 key购买 nike

我有以下实体。

@Entity
public class Shop {
@OneToMany( cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }, orphanRemoval = true )
@JoinTable( name = "shop_image",
joinColumns = @JoinColumn( name = "shop_id" ),
inverseJoinColumns = @JoinColumn( name = "image_id" ) )
@OrderColumn( name = "order_number" )
@BatchSize( size = 20 )
private List<Image> images = new ArrayList<Image>( 0 );
}

shop_image表==(id PK,shop_id,image_id,order_number)并且具有唯一约束(shop_id,image_id)。

当调用以下代码时(在事务上下文中):

@PersistenceContext
EntityManager em;

@Transactional
// shop exist in DB
public void reorderImages(Shop shop) {
//shop.getImages().size == 2; {img0, img1}
List<Image> newImages = new ArrayList<>();
newImages.add(shop.getImages().get(1));
newImages.add(shop.getImages().get(0));
shop.setImages(newImages); // {img1, img0}

em.merge(shop);
}

我遇到以下异常

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '5458-98711' for key 'uniqShopAndImage'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)
... 76 more

我知道可以:

  1. 删除商店中的所有图片//shop.getImages().clear()
  2. em.flush()
  3. shop.setImages(newImages);
  4. em.merge(商店)

但我不喜欢这个解决方案。我使用 hibernate 4 作为 JPA Provider、MySQL DB。感谢您的回答。

最佳答案

该问题是因为MySQL不支持可延迟约束检查(指在提交数据库事务时检查约束,而不是在执行单个SQL语句时检查约束)。详细答案是here .

关于我的问题,可以通过以下方式解决

show.setImages( Lists.newArrayList() );
entityManager.flush();

但我仍然不喜欢这个解决方案。

关于java - 使用 @OrderColumn 和 @OneToMany 保存实体时出现 ConstraintViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24163381/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com