I´m trying to merge an Entity that have a @OneToMany relationship using JPA but it throws the detached entity pased to persist error. I´m not persisting the entity, i´m merging it. I only have the problem while loading the Alumno entity with his Matricula entities using join fetch. Here is the code:
Note: Alumno means Student, Asignatura means Subject and Matricula means Student-Subject, is the relationship between one Student and his Subjects. The relationship is ManyToMany with extra table.
More detailed: Get the Student with the next query: "select a from Student a inner join fetch a.matriculas where iduser=:iduser and password=:password". Then I close the entity manager and change the name of the student. When I try to merge (using entityManager.merge(student)) the student I get the detached entity pased to persist error. If the List on the entity Alumno is null I don´t have the problem.
First entity:
package com.sdi.model;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "Alumno")
public class Alumno implements UsuarioRegistrado {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String nombre, apellidos, iduser, email, password;
private boolean cuentaActivada;
@OneToMany(mappedBy="alumno")
private List<Matricula> matriculas = new ArrayList<Matricula>();
public Alumno() { }
public Alumno(Long id) {
setId(id);
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApellidos() {
return apellidos;
}
public void setApellidos(String apellidos) {
this.apellidos = apellidos;
}
public String getIduser() {
return iduser;
}
public void setIduser(String iduser) {
this.iduser = iduser;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public boolean isCuentaActivada() {
return cuentaActivada;
}
public void setCuentaActivada(boolean cuentaActivada) {
this.cuentaActivada = cuentaActivada;
}
public List<Matricula> getMatriculas() {
return matriculas;
}
public void setMatriculas(List<Matricula> matriculas) {
this.matriculas = matriculas;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Alumno other = (Alumno) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@Override
public String toString() {
return "Alumno [id=" + id + ", nombre=" + nombre + ", apellidos="
+ apellidos + ", iduser=" + iduser + ", email=" + email
+ ", password=" + password + ", cuentaActivada="
+ cuentaActivada + "]";
}
@Override
public String paginaPrincipal() {
return "/alumno/alumno-opciones.xhtml";
}
public void addMatricula(Matricula matricula){
if(!getMatriculas().contains(matricula)){
getMatriculas().add(matricula);
matricula.setAlumno(this);
}
}
@Override
public List<Docencia> getImparte() {
return null;
}
}
Second entity: package com.sdi.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="MATRICULADO")
@IdClass(MatriculaPk.class)
public class Matricula {
@Id @ManyToOne
private Alumno alumno;
@Id @ManyToOne
private Asignatura asignatura;
public Matricula() { }
public Matricula(Alumno alumno, Asignatura asignatura) {
this.alumno = alumno;
this.asignatura = asignatura;
alumno.addMatricula(this);
asignatura.addMatricula(this);
}
public Alumno getAlumno() {
return alumno;
}
public void setAlumno(Alumno alumno) {
this.alumno = alumno;
}
public Asignatura getAsignatura() {
return asignatura;
}
public void setAsignatura(Asignatura asignatura) {
this.asignatura = asignatura;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((alumno == null) ? 0 : alumno.hashCode());
result = prime * result
+ ((asignatura == null) ? 0 : asignatura.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Matricula other = (Matricula) obj;
if (alumno == null) {
if (other.alumno != null)
return false;
} else if (!alumno.equals(other.alumno))
return false;
if (asignatura == null) {
if (other.asignatura != null)
return false;
} else if (!asignatura.equals(other.asignatura))
return false;
return true;
}
@Override
public String toString() {
return "Matricula [alumno=" + alumno + ", asignatura=" + asignatura
+ "]";
}
}
And this is the merge operation:
@Override
public Object execute() throws BusinessException {
return Jpa.getManager().merge(alumno);
}
There is a bug using @IdClass on hibernate and that is what was causing the error. The easiest solution is to avoid using @IdClass and create a @Id for the entity (Matricula) between Subject and Student.