0
I have a bug when I try to insert a new user for the second time.
Error:
12:51:54 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
WARNING: #{testeHibernate.save}: org.hibernate.PersistentObjectException: detached entity passed to persist: classhiber.Utilizador
javax.faces.FacesException: #{testeHibernate.save}: org.hibernate.PersistentObjectException: detached entity passed to persist: classhiber.Utilizador
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
... 24 more
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 26 more
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: classhiber.Utilizador
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:843)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:818)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:822)
at Impl.UtilizadorDAOimpl.addutilizador(UtilizadorDAOimpl.java:32)
at TesteHibernate.save(TesteHibernate.java:48)
... 35 more
User class:
@Entity
@Table(name = "utilizador")
public class Utilizador implements java.io.Serializable {
private Integer idUt;
private String user;
private String pass;
private String tip;
public Utilizador() {
}
public Utilizador(Integer idUT, String user, String pass, String tip) {
this.user = user;
this.pass = pass;
this.tip = tip;
}
@Id
@GeneratedValue(strategy =GenerationType.AUTO)
@Column(name = "id_ut", unique = true, nullable = false)
public Integer getIdUt() {
return this.idUt;
}
public void setIdUt(Integer idUt) {
this.idUt = idUt;
}
@Column(name = "user", nullable = false, length = 100)
public String getUser() {
return this.user;
}
public void setUser(String user) {
this.user = user;
}
@Column(name = "pass", nullable = false, length = 100)
public String getPass() {
return this.pass;
}
public void setPass(String pass) {
this.pass = pass;
}
@Column(name = "tip", nullable = false, length = 100)
public String getTip() {
return this.tip;
}
public void setTip(String tip) {
this.tip = tip;
}
}
Adduser function:
public void addutilizador(Utilizador ut) {
System.out.println(this.sessionFactory);
Session session = this.sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.persist(ut);
tx.commit();
session.close();
}
Try to make the insertions in the same session
– Rafael Telles