Hello.
I have two entities with one-to-one bidirectional mapping and lazy fetch (named as Exam and ExamDetail).
Entity EXAM:
Code:
@Entity
@NamedQueries({
@NamedQuery(name = "Exam.teste", query = "SELECT e FROM Exam e join fetch e.detail")
})
@Table(name = "exam")
public class Exam implements BaseEntity, FieldHandled{
private static final long serialVersionUID = 1L;
/*
* @Basic annotation has been added to all basic fields
*/
private FieldHandler fieldHandler;
private int id;
private String shortName;
private ExamDetail detail;
@Id
@GeneratedValue
@Column(name = "id")
public int getId() {
return id;
}
private void setId(int id) {
this.id = id;
}
@OneToOne(fetch = FetchType.LAZY, mappedBy = "exam")
@LazyToOne(LazyToOneOption.NO_PROXY)
public ExamDetail getDetail() {
if (detail == null && fieldHandler != null)
detail = (ExamDetail) fieldHandler.readObject(this, "detail", detail);
return detail;
}
public void setDetail(ExamDetail detail) {
if (fieldHandler != null)
this.detail = (ExamDetail) fieldHandler.writeObject(this, "detail", this.detail, detail);
else
this.detail = detail;
}
@Basic
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Exam other = (Exam) obj;
if (id != other.id)
return false;
return true;
}
@Override
public void setFieldHandler(FieldHandler handler) {
fieldHandler = handler;
}
@Override
public FieldHandler getFieldHandler() {
return fieldHandler;
}
}
Entity EXAMDETAIL:
Code:
@Entity
@Table(name = "exam_detail")
public class ExamDetail implements BaseEntity {
/*
* @Basic annotation has been added to all basic fields
*/
private int id;
private String fullName;
private int numberOfQuestions;
private int passingPercentage;
private Exam exam;
public ExamDetail() {
System.out.println("xxx");
}
@Id
@GeneratedValue
@Column(name = "id")
public int getId() {
return id;
}
private void setId(int id) {
this.id = id;
}
@OneToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name = "exam_id")
public Exam getExam() {
return exam;
}
public void setExam(Exam exam) {
this.exam = exam;
}
@Basic
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
@Basic
public int getNumberOfQuestions() {
return numberOfQuestions;
}
public void setNumberOfQuestions(int numberOfQuestions) {
this.numberOfQuestions = numberOfQuestions;
}
@Basic
public int getPassingPercentage() {
return passingPercentage;
}
public void setPassingPercentage(int passingPercentage) {
this.passingPercentage = passingPercentage;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ExamDetail other = (ExamDetail) obj;
if (id != other.id)
return false;
return true;
}
}
When i look for "ExamDetail" with join option to "Exam" (
SELECT e FROM ExamDetail e join fetch e.exam) i can get it.
My problem: i´m trying to query the parent entity "Exam" with join option to "ExamDetail" (
SELECT e FROM Exam e join fetch e.detail). Hibernate runs the query but i can´t get "ExamDetail" entity with join fetch.
Note: I´m using field handler to resolve lazy load problem (
http://justonjava.blogspot.com/2010/09/lazy-one-to-one-and-one-to-many.html).
Hope anybody can help me.
Best regards,
Charles.