Reputation: 45
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?
Upvotes: 2
Views: 113
Reputation: 6566
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.
Upvotes: 3
Reputation: 603
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);
}
Upvotes: 2