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?

1
  • Here is another wonderful thread which helped to resolve my issue getj2ee.over-blog.com/… Commented Aug 4, 2016 at 14:44

38 Answers 38

1
2
0

Does this help?

User userObj1 = new User();
User userObj2 = userObj1;
.
.
.
rtsession.save(userObj1);
rtsession.save(userObj2); 
Sign up to request clarification or add additional context in comments.

Comments

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();

Comments

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.

Comments

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

Comments

0

On Entity class - primary 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 definition, so you don't get duplicate PK values.

Comments

-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

Comments

-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

Comments

-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()

1 Comment

That would be a very costly operation to do, wouldnt it?
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.