Hi!!
I have this class:
public class Program implements java.io.Serializable {
private long programId;
private Course course;
//With Course mapped this way:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CURS_ID", nullable = false)
@NotNull
public CursEscolar getCursEscolar()....etc
}
If I do this query:
List<Program> programs = entityManager.createQuery("From Program p ").getResultList;
I retrieve a list of programs and the course the program belongs OK.
But if I do this other query:
List<Program> programs = entityManager.createQuery("From Program p where p.programId= :progId ")
.setParameter("progId", pro.getProgramId())
.getResultList();
I retrieve the Course with id=0, and the fields null. The only difference is the condition:
p.programId= :progId
Why happens this??
It happens because you don't fetch the course. Modify the query to
from Program p join fetch p.cursEscolar c where ...