129

I have two user Objects and while I try to save the object using

session.save(userObj);

I am getting the following error:

Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
[com.pojo.rtrequests.User#com.pojo.rtrequests.User@d079b40b]

I am creating the session using

BaseHibernateDAO dao = new BaseHibernateDAO();          

rtsession = dao.getSession(userData.getRegion(),
                           BaseHibernateDAO.RTREQUESTS_DATABASE_NAME);

rttrans = rtsession.beginTransaction();
rttrans.begin();

rtsession.save(userObj1);
rtsession.save(userObj2);

rtsession.flush();
rttrans.commit();

rtsession.close(); // in finally block

I also tried doing the session.clear() before saving, still no luck.

This is for the first I am getting the session object when a user request comes, so I am getting why is saying that object is present in session.

Any suggestions?

tankucukoglu
  • 455
  • 6
  • 20
harshit
  • 7,925
  • 23
  • 70
  • 97
  • Here is another wonderful thread which helped to resolve my issue http://getj2ee.over-blog.com/article-hibernate-why-a-nonuniqueobjectexception-is-thrown-73738887.html – Reddymails Aug 04 '16 at 14:44

38 Answers38

186

I have had this error many times and it can be quite hard to track down...

Basically, what hibernate is saying is that you have two objects which have the same identifier (same primary key) but they are not the same object.

I would suggest you break down your code, i.e. comment out bits until the error goes away and then put the code back until it comes back and you should find the error.

It most often happens via cascading saves where there is a cascade save between object A and B, but object B has already been associated with the session but is not on the same instance of B as the one on A.

What primary key generator are you using?

The reason I ask is this error is related to how you're telling hibernate to ascertain the persistent state of an object (i.e. whether an object is persistent or not). The error could be happening because hibernate is trying to persist an object that is already persistent. In fact, if you use save hibernate will try and persist that object, and maybe there is already an object with that same primary key associated with the session.

Example

Assuming you have a hibernate class object for a table with 10 rows based on a primary key combination (column 1 and column 2). Now, you have removed 5 rows from the table at some point of time. Now, if you try to add the same 10 rows again, while hibernate tries to persist the objects in database, 5 rows which were already removed will be added without errors. Now the remaining 5 rows which are already existing, will throw this exception.

So the easy approach would be checking if you have updated/removed any value in a table which is part of something and later are you trying to insert the same objects again

Michael Wiles
  • 20,902
  • 18
  • 71
  • 101
  • 6
    Nice Answer². The primary key was my problem, solved with the GeneratedValue setting a sequence for postgresql. – digoferra May 08 '12 at 12:59
  • 2
    I had the same issue. In my case, I had a object searched in a code and I trying to build a new object with the same ID in other piece of code while the first object yet was at hibernate's session. – deldev Apr 23 '14 at 19:21
  • This problem plagued me for a couple of days. `I would suggest you break down your code` This is definitely the best answer. Play around the processes. Change the CascadeTypes. As for me, what worked was the order. Initially what I was doing was deleting the child before the parent. What worked for me was deleting the parent before the child. Which is weird but it worked. So just play up and down your code until you get your desirable results. Hibernate relationships are definitely weird. – Rigo Sarmiento Sep 10 '20 at 06:44
18

This is only one point where hibernate makes more problems than it solves. In my case there are many objects with the same identifier 0, because they are new and don't have one. The db generates them. Somewhere I have read that 0 signals Id not set. The intuitive way to persist them is iterating over them and saying hibernate to save the objects. But You can't do that - "Of course You should know that hibernate works this and that way, therefore You have to.." So now I can try to change Ids to Long instead of long and look if it then works. In the end it's easier to do it with a simple mapper by your own, because hibernate is just an additional intransparent burden. Another example: Trying to read parameters from one database and persist them in another forces you to do nearly all work manually. But if you have to do it anyway, using hibernate is just additional work.

Hein
  • 205
  • 2
  • 2
  • 13
    I hate hibernate too... and databases... After all the problems they caused me I guess it's easier to use a text file (I'm joking, but still...). – Igor Popov Jun 18 '11 at 19:08
  • _In my case there are many objects with the same identifier 0, because they are new and don't have one. The db generates them. Somewhere I have read that 0 signals Id not set. The intuitive way to persist them is iterating over them and saying hibernate to save the objects_. I need to do exactly this. Could you tell me which is the "hibernate way" to do this? – Ramses Sep 30 '14 at 04:12
  • In my case i had to use session.merge(myobject) as I had two instances of this object.Usually that happens when an entity is persisted and detached from the session. Another instance of this entity is requested to hibernate. This second instance stayed attached to the session. The first instance is modified. More at http://getj2ee.over-blog.com/article-hibernate-why-a-nonuniqueobjectexception-is-thrown-73738887.html – Reddymails Aug 25 '16 at 13:49
  • it's not hibernate making problems, but lack of understanding how it works – ACV Jul 17 '18 at 17:09
17

USe session.evict(object); The function of evict() method is used to remove instance from the session cache. So for first time saving the object ,save object by calling session.save(object) method before evicting the object from the cache. In the same way update object by calling session.saveOrUpdate(object) or session.update(object) before calling evict().

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
Rahul N
  • 199
  • 1
  • 4
14

This can happen when you have used same session object for read & write. How? Say you have created one session. You read a record from employee table with primary key Emp_id=101 Now You have modified the record in Java. And you are going to save the Employee record in database. we have not closed session anywhere here. As the object that was read also persist in the session. It conflicts with the object that we wish to write. Hence this error comes.

Prashant Bari
  • 141
  • 1
  • 2
9

As somebody already pointed above i ran into this problem when i had cascade=all on both ends of a one-to-many relationship, so let's assume A --> B (one-to-many from A and many-to-one from B) and was updating instance of B in A and then calling saveOrUpdate(A) , it was resulting in a circular save request i.e save of A triggers save of B that triggers save of A... and in the third instance as the entity( of A) was tried to be added to the sessionPersistenceContext the duplicateObject exception was thrown.
  I could solve it by removing cascade from one end.

redzedi
  • 1,957
  • 21
  • 31
  • Solved my problem together with http://stackoverflow.com/questions/4334970/hibernate-cannot-simultaneously-fetch-multiple-bags – Magno C Jul 09 '14 at 19:27
8

You can use session.merge(obj), if you are doing save with different sessions with same identifier persistent object.
It worked, I had same issue before.

artdanil
  • 4,952
  • 2
  • 32
  • 49
Pavan Kumar
  • 81
  • 1
  • 1
5

I ran into this problem by:

  1. Deleting an object (using HQL)
  2. Immediately storing a new object with the same id

I resolved it by flushing the results after the delete, and clearing the cache before saving the new object

String delQuery = "DELETE FROM OasisNode";
session.createQuery( delQuery ).executeUpdate();
session.flush();
session.clear();
wbdarby
  • 1,129
  • 12
  • 12
5

This problem occurs when we update the same object of session, which we have used to fetch the object from database.

You can use merge method of hibernate instead of update method.

e.g. First use session.get() and then you can use session.merge (object). This method will not create any problem. We can also use merge() method to update object in database.

4

I also ran into this problem and had a hard time to find the error.

The problem I had was the following:

The object has been read by a Dao with a different hibernate session.

To avoid this exception, simply re-read the object with the dao that is going to save/update this object later on.

so:

class A{      

 readFoo(){
       someDaoA.read(myBadAssObject); //Different Session than in class B
    }

}

class B{



 saveFoo(){
       someDaoB.read(myBadAssObjectAgain); //Different Session than in class A
       [...]
       myBadAssObjectAgain.fooValue = 'bar';
       persist();
    }

}

Hope that save some people a lot of time!

Frithjof
  • 41
  • 1
3

Get the object inside the session, here an example:

MyObject ob = null;
ob = (MyObject) session.get(MyObject.class, id);
CSchulz
  • 10,882
  • 11
  • 60
  • 114
sock_osg
  • 49
  • 4
  • 3
    Nice try, but the object "ob" is returned without the updated data (considering that it was updated through application during runtime, between the objects retrieving from database and its saving). – Alex Nov 30 '12 at 12:01
3

By default is using the identity strategy but I fixed it by adding

@ID
@GeneratedValue(strategy = GenerationType.IDENTITY)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
sHIFT0O
  • 41
  • 3
2

Are your Id mappings correct? If the database is responsible for creating the Id through an identifier, you need to map your userobject to that ..

cwap
  • 11,087
  • 8
  • 47
  • 61
2

I encountered this problem with deleting an object, neither evict nor clear helped.

/**
 * Deletes the given entity, even if hibernate has an old reference to it.
 * If the entity has already disappeared due to a db cascade then noop.
 */
public void delete(final Object entity) {
  Object merged = null;
  try {
    merged = getSession().merge(entity);
  }
  catch (ObjectNotFoundException e) {
    // disappeared already due to cascade
    return;
  }
  getSession().delete(merged);
}
TimP
  • 925
  • 1
  • 8
  • 13
2

before the position where repetitive objects begin , you should close the session and then you should start a new session

session.close();      
session = HibernateUtil.getSessionFactory().openSession();

so in this way in one session there is not more than one entities that have the same identifier.

engtuncay
  • 867
  • 10
  • 29
2

I had a similar problem. In my case I had forgotten to set the increment_by value in the database to be the same like the one used by the cache_size and allocationSize. (The arrows point to the mentioned attributes)

SQL:

CREATED         26.07.16
LAST_DDL_TIME   26.07.16
SEQUENCE_OWNER  MY
SEQUENCE_NAME   MY_ID_SEQ
MIN_VALUE       1
MAX_VALUE       9999999999999999999999999999
INCREMENT_BY    20 <-
CYCLE_FLAG      N
ORDER_FLAG      N
CACHE_SIZE      20 <-
LAST_NUMBER     180

Java:

@SequenceGenerator(name = "mySG", schema = "my", 
sequenceName = "my_id_seq", allocationSize = 20 <-)
kaba713
  • 484
  • 6
  • 17
2

Late to the party, but may help for coming users -

I got this issue when i select a record using getsession() and again update another record with same identifier using same session causes the issue. Added code below.

Customer existingCustomer=getSession().get(Customer.class,1);
Customer customerFromUi;// This customer details comiong from UI with identifer 1

getSession().update(customerFromUi);// Here the issue comes

This should never be done . Solution is either evict session before update or change business logic.

Vipin CP
  • 3,642
  • 3
  • 33
  • 55
2

Check if you forgot to put @GenerateValue for @Id column. I had same problem with many to many relationship between Movie and Genre. The program threw Hibernate Error: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session error. I found out later that I just have to make sure you have @GenerateValue to the GenreId get method.

Thupten
  • 2,158
  • 1
  • 24
  • 31
  • how can I apply your solution to my question? do I create an id column in the Task class? https://stackoverflow.com/questions/55732955/moving-primary-key-creation-to-a-seperate-changeset-causes-error – sbattou Apr 17 '19 at 17:48
1

just check the id whether it takes null or 0 like

if(offersubformtwo.getId()!=null && offersubformtwo.getId()!=0)

in add or update where the content are set from form to Pojo

EdChum
  • 376,765
  • 198
  • 813
  • 562
1

I'm new to NHibernate, and my problem was that I used a different session to query my object than I did to save it. So the saving session didn't know about the object.

It seems obvious, but from reading the previous answers I was looking everywhere for 2 objects, not 2 sessions.

DustinA
  • 499
  • 5
  • 9
1

@GeneratedValue(strategy=GenerationType.IDENTITY), adding this annotation to the primary key property in your entity bean should solve this issue.

Jay
  • 701
  • 7
  • 19
1

I resolved this problem .
Actually this is happening because we forgot implementation of Generator Type of PK property in the bean class. So make it any type like as

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

when we persist the objects of bean ,every object acquired same ID ,so first object is saved ,when another object to be persist then HIB FW through this type of Exception: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
1

Try this. The below worked for me!

In the hbm.xml file

  1. We need to set the dynamic-update attribute of class tag to true:

    <class dynamic-update="true">
    
  2. Set the class attribute of the generator tag under unique column to identity:

    <generator class="identity">
    

Note: Set the unique column to identity rather than assigned.

Michael
  • 8,362
  • 6
  • 61
  • 88
RaGa__M
  • 2,550
  • 1
  • 23
  • 44
1

The problem happens because in same hibernate session you are trying to save two objects with same identifier.There are two solutions:-

  1. This is happening because you have not configured your mapping.xml file correctly for id fields as below:-

    <id name="id">
      <column name="id" sql-type="bigint" not-null="true"/>
      <generator class="hibernateGeneratorClass"</generator>
    </id>
    
  2. Overload the getsession method to accept a Parameter like isSessionClear, and clear the session before returning the current session like below

    public static Session getSession(boolean isSessionClear) {
        if (session.isOpen() && isSessionClear) {
            session.clear();
            return session;
        } else if (session.isOpen()) {
            return session;
        } else {
            return sessionFactory.openSession();
        }
    }
    

This will cause existing session objects to be cleared and even if hibernate doesn't generate a unique identifier ,assuming you have configured your database properly for a primary key using something like Auto_Increment,it should work for you.

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
1

Otherwise than what wbdarby said, it even can happen when an object is fetched by giving the identifier of the object to a HQL. In the case of trying to modify the object fields and save it back into DB(modification could be insert, delete or update) over the same session, this error will appear. Try clearing the hibernate session before saving your modified object or create a brand new session.

Hope i helped ;-)

Arash moradabadi
  • 230
  • 1
  • 12
1

I have the same error I was replacing my Set with a new one get from Jackson.

To solve this I keep the existing set, I remove from the old set the element unknown into the new list with retainAll. Then I add the new ones with addAll.

    this.oldSet.retainAll(newSet);
    this.oldSet.addAll(newSet);

No need to have the Session and manipulate it.

Ôrel
  • 7,044
  • 3
  • 27
  • 46
1

I just had the same problem .I solve it by adding this line:

@GeneratedValue(strategy=GenerationType.IDENTITY)

enter image description here

4b0
  • 21,981
  • 30
  • 95
  • 142
0

You can check your Cascade Settings. The Cascade settings on your models could be causing this. I removed Cascade Settings (Essentially not allowing Cascade Inserts/Updates) and this solved my problem

user1609848
  • 143
  • 1
  • 9
0

Another thing that worked for me was to make the instance variable Long in place of long


I had my primary key variable long id; changing it to Long id; worked

All the best

0

I found this error as well. What worked for me is to make sure that the primary key (that is auto-generated) is not a PDT (i.e. long, int, ect.), but an object (i.e. Long, Integer, etc.)

When you create your object to save it, make sure you pass null and not 0.

Jaco Van Niekerk
  • 4,180
  • 2
  • 21
  • 48
0

Does this help?

User userObj1 = new User();
User userObj2 = userObj1;
.
.
.
rtsession.save(userObj1);
rtsession.save(userObj2); 
George Papatheodorou
  • 1,539
  • 19
  • 23
0

I have solved a similar problem like that:

plan = (FcsRequestPlan) session.load(plan.getClass(), plan.getUUID());
while (plan instanceof HibernateProxy)
    plan = (FcsRequestPlan) ((HibernateProxy) plan).getHibernateLazyInitializer().getImplementation();
user3132194
  • 2,381
  • 23
  • 17
0

One workaround to solve this issue is try to read the object back from hibernate cache/db before you make any updates and then persist.

Example:

            OrderHeader oh = orderHeaderDAO.get(orderHeaderId);
            oh.setShipFrom(facilityForOrder);
            orderHeaderDAO.persist(oh);

Note: Keep in mind that this does not fix the root cause but solves the issue.

Nuwan
  • 31
  • 3
0

We are using an old version hibernate (3.2.6) and the problem for us was, that Hibernate expected the first column in the result set to be the generated primary key. Took me ages to figure that out.

Solution: Ensure in that in the DDL the generated primary key is always the first column. Solution 2: Update hibernate

Tim Malich
  • 1,301
  • 14
  • 22
0

On Entity class - primery key column, keep like this: @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private int id; this: @GeneratedValue(strategy = GenerationType.IDENTITY) helps generating sequential id values according to db defination. so you dont get duplicate PK values.

0

You always can do a session flush. Flush will synchronize the state of all your objects in session (please, someone correct me if i'm wrong), and maybe it would solve your problem in some cases.

Implementing your own equals and hashcode may help you too.

caarlos0
  • 20,020
  • 27
  • 85
  • 160
-1

In my model object class i ha defined the annotations like this

@Entity
@Table(name = "user_details")
public class UserDetails {  
    @GeneratedValue
    private int userId;
    private String userName;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }   

    @Id
    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }
}

the issue resolved when I writing the both @Id and @GenerateValue annotation together @ the variable declaration.

@Entity
@Table(name = "user_details")
public class UserDetails {
    @Id
    @GeneratedValue
    private int userId;
    private String userName;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }   

    public int getUserId() {
        return userId;
    }
...
}

Hope this is helpful

anaximander
  • 7,083
  • 3
  • 44
  • 62
SG719
  • 1
-1

In my run I follows

1- Switch to Lazy keys in the entity 2- download the most up to date from Maven

--http://mvnrepository.com/artifact/org.javassist/javassist/3.19.0-GA

rsonet
  • 1
-2

It's because you have open a session maybe for get data and then you forget to close it. When you delete you open session again then it becomes error.

SOLUTION: every function should open and close session

session.getTransaction.begin(); /* your operation */ session.close()

7PlusCoder
  • 33
  • 1
  • 8