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

时间:2014-06-11 12:48:47

标签: java spring hibernate jpa spring-transactions

我有以下实体。

@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 table ==(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. 中的所有图片
  3. em.flush()
  4. shop.setImages(newImages);
  5. em.merge(店)
  6. 但我不喜欢这个解决方案。 我使用hibernate 4作为JPA Provider,MySQL DB。 谢谢你的回答。

1 个答案:

答案 0 :(得分:1)

问题是由于MySQL不支持延迟约束检查(意味着在提交数据库事务时检查约束,而不是在执行单个SQL语句时)。详细答案是here

关于我的问题,可以用

解决它
show.setImages( Lists.newArrayList() );
entityManager.flush();

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