2

I use in my project Hibernate. And have error when the server start:

org.hibernate.PersistentObjectException: detached entity passed to persist

Following "autorization" code:

   @Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private long id;

@Column(name = "login", unique = true, updatable = false)
private String name;

//Important to Hibernate!
@SuppressWarnings("MySQLConfig")
public UsersDataSet() {
}

@SuppressWarnings("MySQLConfig")
public UsersDataSet(long id, String name) {
    this.setId(id);
    this.setName(name);
}

public UsersDataSet(String name) {
    this.setId(-1);
    this.setName(name);
}

Please, explain me, where am I wrong?

2 Answers 2

3

I'm suspecting that you are trying to do session.persist(userData) rather than doing session.saveOrUpdate(..) (will update the data on the detached object) or session.save(..) (Will create a new row). Please try any of these last two depending on what you want.

Sign up to request clarification or add additional context in comments.

Comments

2

Next line to create id automatically. That's why you have an exception.

@GeneratedValue(strategy = GenerationType.IDENTITY)

JPA strategy generated id automatically when saving essence. But the constructor has written that is necessary to do manually. That's why JPA thinking detached from persistence context.

For fix it you can change direction id or GeneratedValue(strategy = …)

 public UsersDataSet(long id, String name) {
   // this.setId(id);
    this.setName(name);
}

Comments

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.