使用JPA / Hibernate存储有序列表

时间:2015-03-02 15:38:12

标签: java hibernate jpa

在设备上,用户可以选择要显示内容的首选语言。我已经使用了很长时间了,但更新了这个列表的订购系统:

@Required
@OrderColumn(name="preferenceOrder")
@ManyToMany(fetch= FetchType.LAZY)
public List<Language> languages;

但是当我尝试使用以下代码更新列表时:

try{
    // Get the codes of the Device
    Device device = Device.findById(deviceId);
    device.languages.clear();
    for(String langCode : languageCodes){
        Language lang = Language.findByCode(langCode);
        device.languages.add(lang);
    }

    device.save();
    return true;
} catch (Exception e) {
    Logger.error(e, "Problem with updating the languages for device: " + e.getLocalizedMessage());
    return false;
}

我收到以下错误:

15:30:36,376 ERROR ~ Problem with updating the languages for device: org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 5; expected: 1
javax.persistence.PersistenceException: org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 5; expected: 1
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1389)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1317)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1323)
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:965)
    at play.db.jpa.JPABase._save(JPABase.java:41)
    at play.db.jpa.GenericModel.save(GenericModel.java:215)
    at logic.helpers.LanguageUpdatePerformer.doJobWithResult(LanguageUpdatePerformer.java:47)
    at logic.helpers.LanguageUpdatePerformer.doJobWithResult(LanguageUpdatePerformer.java:1)
    at play.jobs.Job.call(Job.java:146)
    at play.jobs.Job$1.call(Job.java:66)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 5; expected: 1
    at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:95)
    at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
    at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
    at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
    at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1401)
    at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:86)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:187)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:345)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:962)
    ... 12 more

这是什么原因? 我找到5条记录,这是正确的,但是预计1? 是因为我首先清除整个清单,我应该用另一种方式吗?这对我来说是最容易按照给我的顺序添加它们。

0 个答案:

没有答案