Org.hibernate.StaleStateException:Batch Update returned unexpected row count

Source: Internet
Author: User
Tags flush sessions

Transferred from http://hi.baidu.com/besun/blog/item/01973ff7a9ce0929730eec38.html

1, a different object with the same identifier value is already associated with the session.   Error reason: There are two identical identities in the same session in Hibernate but different entities. Workaround one: Session.clean () PS: If the Saveorupdate (object) is changed after the clean operation, it is possible to quote "Found two representations of same   Collection "Exception. Workaround Two: Session.refresh (object) PS: Session.refresh (object) cannot be used when object is not already a data in the database because the method is from   Hibernate in the session to pick object, if there is no this object, it will be an error so when you use Saveorupdate (object) before you need to judge. Solution Three: Session.merge (object) Ps:hibernate inside the method, recommended use.   2. Found two representations of same collection error reason: See 1. Workaround: Session.merge (object) Both of these exceptions often appear in One-to-many mappings and many-to-many mappings. 3. Net.sf.hibernate.TransientObjectException:object references an unsaved transient instance-save the transient instanc E before Flushing:bbusinessman

From this bug literal, it should be that an attribute of Bbusinessman is an entity that saves businessman objects before the entity is saved, resulting in an error. So I've been looking at the businessman attribute in the end which is the entity, the result found three, respectively city, Type, Status, and these three entities are Dao.load (ID, session) obtained from the database, not already have ID, or is null , and there is no entity that is not saved.

So I wonder if the same transaction requires a session, and if this session is not used, then it is not the reason why I put all the dao.getsession () in the method of the transaction into the session with the method. This session is the same, but there is still this error.

In this way, the various properties of the Bbusinessman are not problematic. If it is not saved before Bbusinessman, and Bbusinessman is treated as an entity property, first save the entity and then save the Bbusinessman, whether this will cause this error. As a result, I found that this is the problem.

================ the wrong writing: ===================

Session session = Dao.getsession ();

Transaction tx = Session.begintransaction ();

Bo.setbman (form, man,session);

Bo.savechangetable (man,session); Assign the man as a property to Changetable and save the changetable. It's the wrong thing,

Dao.save (man,session); Save Man

Tx.commit ();

==================== should write this: ===============

Session session = Dao.getsession ();

Transaction tx = Session.begintransaction ();

Bo.setbman (form, man,session);

Dao.save (man,session); Save Man

Bo.savechangetable (man,session); Assign the man as a property to Changetable and save the changetable. It's the wrong thing,

Tx.commit ();

In this way, the problem is solved.

4, Write operations are not allowed in read-only mode (flushmode.never)-Turn your sessions into Flushmode.auto or remove ' ReadOnly ' marker from transaction definition error resolution

Error code:
Org.springframework.dao.InvalidDataAccessApiUsageException:Write operations are not allowed in read-only mode ( Flushmode.never)-Turn your session to Flushmode.auto or remove ' readOnly ' marker from transaction definition
Error Reason:
When Opensessioninviewfilter getsession, the flush mode of the session that gets back is set to Flushmode.never. The sessionfactory is then bound to the Transactionsynchronizationmanager so that the entire process of the request uses the same session, followed by the binding of the sessionfactory after the request, Finally closesessionifnecessary determines whether to close the session based on whether the session has been bound to transaction. In this process, if hibernatetemplate found that from the current session there is not READONLY transaction, will get Flushmode.auto sessions, so that the method has write permission.
That is, if there is not ReadOnly transaction can be converted from flush.never to Flush.auto, with insert,update,delete operation Rights, if not transaction, And no other artificially set flush model, then dofilter the whole process is flush.never. Therefore, the method protected by the transaction has write permission and is not protected.
Reference articles:
Http://calvin.blog.javascud.org/post/46.htm
Solution:
Use spring's transaction declaration to make methods transaction controlled by
<bean id= "Basetransaction"
Class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
Abstract= "true" >
<property name= "TransactionManager" ref= "TransactionManager"/>
<property name= "Proxytargetclass" value= "true"/>
<property name= "Transactionattributes" >
<props>
<prop key= "get*" >PROPAGATION_REQUIRED,readOnly</prop>
<prop key= "find*" >PROPAGATION_REQUIRED,readOnly</prop>
<prop key= "load*" >PROPAGATION_REQUIRED,readOnly</prop>
<prop key= "save*" >PROPAGATION_REQUIRED</prop>
<prop key= "add*" >PROPAGATION_REQUIRED</prop>
<prop key= "update*" >PROPAGATION_REQUIRED</prop>
<prop key= "remove*" >PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id= "UserService" parent= "Basetransaction" >
<property name= "Target" >
<bean class= "Com.phopesoft.security.service.impl.UserServiceImpl"/>
</property>
</bean>

5, about Hibernate Batch update returned unexpected row count from update exception

ERROR [Http-8080-processor22] (batchingbatcher.java:60)-Exception executing batch:
Org.hibernate.StaleStateException:Batch Update returned unexpected row count from update:0 actual row count:0 expected: 1

1. The Hibernate saveorupdate method is used to save the instance. The Saveorupdate method requires an ID of NULL to execute save and, in other cases, to execute update. It's new when you save the instance, but your ID is not NULL, so you're using update, but there's no primary key-related value in the database, so there's an exception.


=================================================================

Abnormal:
At the time of insertion:
Org.hibernate.StaleStateException:Batch Update returned unexpected row count from update:0 actual row count:0 expected: 1

Workaround:
What if it's a self-added primary key?
Some databases can be modified by the self-added primary key such as: MySQL, some databases are not allowed to modify the self-added primary key for example PostgreSQL
Do not set the value of the self-added primary key


2)

In the hibernate mapping One-to-many, Many-to-many, Many-to-many, this exception is often added, the code is as follows:

public void Savefunctioncell (Functioncell Functioncell, Integer pid) {
SYSTEM.OUT.PRINTLN ("New Action Now");
Functioncell FC = new Functioncell ();
try {
Beanutils.copyproperties (FC, Functioncell);
catch (Illegalaccessexception e) {
E.printstacktrace ();
catch (InvocationTargetException e) {
E.printstacktrace ();
}
Fc.setfunccellid (NULL);
Get Parent Permissions
Functioncell PFC = Functioncelldao.findfunctioncellbyid (PID);
Fc.setparentfunctioncell (PFC);
Functioncelldao.savefunctioncell (FC);
}

Note that this place is specifically identified, and when beanutils copies Bean attributes, it will set your integer to 0, setting a null here so that no errors are thrown.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.