A different object with the same identifier value is already associated with the session

Source: Internet
Author: User

The plan for this article is in line with my requirements and is documented here:

To trigger a scenario, a solution:
1. Show business operations, open a session to identify some entities, when an entity is a persistent object that exists in the session. Use this entity for page presentation through business logic. This session is not closed at this time.

2. Then execute the Save business logic and pass the entity ID obtained in the previous step back to the background. A temporary object is created by new, and I assign the ID returned as the primary key to the temporary object. The Session.save (obj) is then called; Method... Throws an exception.

The reason is simple, there are 2 OID objects in the session, and Hibernate does not know which one to persist into the library. At that time the solution is also clear, direct clear (); Clear the session cache is not OK. but the clear hit surface is too wide (use with caution!) ). Other "innocent" objects have also been killed, causing other businesses to fail.

later, a evict method was found in the session, the "fixed-point Cleanup" object cache. This is good, first with the return of the ID used in fact here with the Load method is still loaded from the cache anyway, get the persistent entity in the session, then kill, and then save the temporary entity. No problem.

Original address: http://www.cnblogs.com/linjiqin/p/3531374.html

Since my session is encapsulated and there is no get,evict method in itself, the session that was encapsulated by him is called first, and then the two methods can be used.

Hbsession sess = Hbutil.gethbsession ();

Query Queryuser = Sess.createquery (from User where name=:name);

Queryuser.setstring ("name", "testers");

List<user> userlist = Queryuser.list ();

...

...

...//Here is the process of using

The next cleanup

for (int i=0;i<userlist.size (); i++) {

Sess.getsession () is to get the encapsulated session because my sess itself does not have get (xxxx,xxx), evict (Object) method

How to use your own Baidu Session.get method

  Object tempuser= sess.getsession (). get (User.class, Userlist.get (i). GetKey ());

Remove the object from the Sess cache

Sess.getsession (). evict (tempuser);

}

A different object with the same identifier value is already associated with the session

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.