@Hibernate org.hibernate.StaleStateException:批量更新从update [0]返回意外的行数;实际行数:0;预期:1

时间:2017-04-12 15:22:15

标签: java hibernate jpa

我在实体编辑列表时遇到了这个问题" SejourPerson",Plz可以帮助我吗?

16:19:27.358 [http-8080-1] DEBUG org.hibernate.persister.collection.AbstractCollectionPersister - Deleting collection: [ma.neoxia.sejour.domain.entities.Hebergement.orderers#319]
16:19:27.358 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
16:19:27.358 [http-8080-1] DEBUG org.hibernate.SQL - delete from hebergements_ordonnateurs where id_hebergement=?
Hibernate: delete from hebergements_ordonnateurs where id_hebergement=?
16:19:27.358 [http-8080-1] DEBUG org.hibernate.persister.collection.AbstractCollectionPersister - done deleting collection
16:19:27.358 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - Executing batch size: 1
16:19:27.358 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
16:19:27.359 [http-8080-1] DEBUG org.hibernate.persister.collection.AbstractCollectionPersister - Deleting rows of collection: [ma.neoxia.sejour.domain.entities.SejourPerson.hebergements#242]
16:19:27.359 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
16:19:27.359 [http-8080-1] DEBUG org.hibernate.SQL - update hebergements set id_sejour_personne=null where id_sejour_personne=? and id=?
Hibernate: update hebergements set id_sejour_personne=null where id_sejour_personne=? and id=?
16:19:27.359 [http-8080-1] DEBUG org.hibernate.persister.collection.AbstractCollectionPersister - done deleting collection rows: 1 deleted
16:19:27.359 [http-8080-1] DEBUG org.hibernate.persister.collection.AbstractCollectionPersister - Inserting rows of collection: [ma.neoxia.sejour.domain.entities.SejourPerson.hebergements#242]
16:19:27.359 [http-8080-1] DEBUG org.hibernate.persister.collection.AbstractCollectionPersister - done inserting rows: 0 inserted
16:19:27.359 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - Executing batch size: 1
16:19:27.359 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
16:19:27.359 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
16:19:27.359 [http-8080-1] DEBUG org.hibernate.SQL - delete from hebergements where id=?
Hibernate: delete from hebergements where id=?
16:19:27.360 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - Executing batch size: 1
16:19:27.366 [http-8080-1] ERROR org.hibernate.jdbc.AbstractBatcher - Exception executing batch: 
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
    at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85) ~[hibernate-core-3.6.1.Final.jar:3.6.1.Final]
    at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70) ~[hibernate-core-3.6.1.Final.jar:3.6.1.Final]
    at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90) ~[hibernate-core-3.6.1.Final.jar:3.6.1.Final]
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) ~[hibernate-core-3.6.1.Final.jar:3.6.1.Final]
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) [hibernate-core-3.6.1.Final.jar:3.6.1.Final]
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268) [hibernate-core-3.6.1.Final.jar:3.6.1.Final]
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:189) [hibernate-core-3.6.1.Final.jar:3.6.1.Final]
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) [hibernate-core-3.6.1.Final.jar:3.6.1.Final]
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) [hibernate-core-3.6.1.Final.jar:3.6.1.Final]
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216) [hibernate-core-3.6.1.Final.jar:3.6.1.Final]
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383) [hibernate-core-3.6.1.Final.jar:3.6.1.Final]
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133) [hibernate-core-3.6.1.Final.jar:3.6.1.Final]
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656) [spring-orm-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754) [spring-tx-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723) [spring-tx-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393) [spring-tx-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120) [spring-tx-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621) [spring-aop-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at ma.neoxia.sejour.services.impl.SejourPersonSerivceImpl$$EnhancerByCGLIB$$1eeed1a4.edit(<generated>) [cglib-2.2.2.jar:na]
    at ma.neoxia.sejour.rest.RestSejourPerson.edit(RestSejourPerson.java:124) [ma.neoxia.sejour-0.1.jar:na]
    at sun.reflect.GeneratedMethodAccessor750.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_92]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_92]
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137) [resteasy-jaxrs-3.0.5.Final.jar:na]

我正在使用Hibernate,我有桌子&#34; SejourPerson&#34;包含&#34; hebergements&#34;这个包含&#34; orderers&#34;以这种方式映射

@Entity
@Table( name = "sejours_personnes")
public class SejourPerson extends EntityObject implements Serializable {

    private Integer id;
    private List<Hebergement> hebergements;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn( name = "id_sejour_personne")
    public List<Hebergement> getHebergements() {
        if(hebergements == null)
            hebergements = new ArrayList<Hebergement>();
        return hebergements;
    }

    public void setHebergements(List<Hebergement> hebergements) {
        this.hebergements = hebergements;
    }
}
@Entity
@Table(name = "hebergements")
public class Hebergement extends EntityObject implements Serializable, Cloneable {

    private Integer id;
    private List<Person> orderers;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "hebergements_ordonnateurs",
            joinColumns = {@JoinColumn(name = "id_hebergement",referencedColumnName="id")},
            inverseJoinColumns = {@JoinColumn(name = "id_personne_ordonateur",referencedColumnName="id_tier")})
    public List<Person> getOrderers() {
        if (orderers == null)
            orderers = new ArrayList<Person>();
        return orderers;
    }
}

我使用了flyway,因此在脚本中是这样的:

--
-- Structure de la table `hebergements_ordonnateurs`
--

CREATE TABLE IF NOT EXISTS `hebergements_ordonnateurs` (
  `id_hebergement` int(11) NOT NULL,
  `id_personne_ordonateur` int(11) NOT NULL,
  PRIMARY KEY (`id_hebergement`,`id_personne_ordonateur`),
  KEY `id_hebergement` (`id_hebergement`),
  KEY `id_personne_ordonateur` (`id_personne_ordonateur`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Index pour la table `hebergements_ordonnateurs`
--

ALTER TABLE `hebergements_ordonnateurs`
    ADD INDEX(`id_hebergement`),
    ADD INDEX(`id_personne_ordonateur`);

ALTER TABLE `hebergements_ordonnateurs`
  ADD CONSTRAINT `fk_ho_hebergement` FOREIGN KEY (`id_hebergement`) REFERENCES `hebergements` (`id`),
  ADD CONSTRAINT `fk_ho_personne` FOREIGN KEY (`id_personne_ordonateur`) REFERENCES `personnes` (`id_tier`);

对于thransactional方法,我以这种方式编辑对象

@Override
    public SejourPersonBean edit(SejourPersonBean bean) throws ReferentielServiceException {
        try {
            log.debug("Edit sejour person");
            SejourPerson sejourPerson = sejourPersonDao.findById(bean.getId());
            if (sejourPerson == null) {
                throw new ReferentielServiceException(ReferentielServiceExceptionCode.SEJOUR_PERSON_NOT_FOUND);
            }
            sejourPerson.getHebergements().clear();
            sejourPerson.getHebergements().addAll(HebergementBeanMapper.toEntities(bean.getHebergements()));

            sejourPersonDao.update(sejourPerson);
            SejourPerson objectToReturn = sejourPersonDao.findById(sejourPerson.getId());

            return SejourPersonBeanMapper.toBean(objectToReturn);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("SejourPersonServiceImpl: Error while editing sejour person " + e.getMessage(), e);
            throw new ReferentielServiceException(ReferentielServiceExceptionCode.ERROR_HAS_BEEN_THROWN);
        }
    }

0 个答案:

没有答案