i have one to many relationship using hibernate [School]-->Student.
Code:
@Entity
@Table(name = "schools")
public class School implements java.io.Serializable
{
private Integer id;
private String phone;//is not being populate
private Set<Student> students = new HashSet<Student(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = "school")
public Set<Student> getStudents(){return this.students;}
public void setStudents(Set<Student>students){this.students = students;}
}
@Entity
@Table(name = "students")
public class Student implements java.io.Serializable
{
private Integer id;
private School school;
private Boolean phone;//this is being populate but is wrong.
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="school_id")
public School getSchool() {return this.school;}
}
i need a criteria which returns a School only a field[PhoneNumber] applying filters on both tables....
public Student getSchoolName(String studentId)
{
ProjectionList p = Projections.projectionList().add(Projections.property("phone"),"phone");//i only need the phone of the school the parent of student.
Criterion filters = Restrictions.and(Restrictions.isNotNull("phone"));//restrictions on parent....
Session session = getHibernateTemplate().getSessionFactory().openSession();
Criteria criteria = session.createCriteria(Student.class,"student").add(Restrictions.eq("id",studentId)).add(Restrictions.eq("status",true))
.createCriteria("school","school",JoinType.LEFT_OUTER_JOIN,filters).setProjection(p);// i bring the parent[School] apply the filters...
criteria.setResultTransformer(Transformers.aliasToBean(Student.class));//unfortunately Student have a phone column as well and Student.phone is populate and School is NULL.
Student student =(Student)criteria.uniqueResult();
//i would like a student with school class with School.phone being populate but Student.phone is being populate and School is NULL
session.close();
return student;
}
the join is working smoothly but i am receiving the Student with phone populate[student.getPhone()] and School returning null [student.getSchool()] i need the student not null and School not null and student.getSchool().getPhone() the resulting resultset. i have try this
Code:
Projections.projectionList().add(Projections.property("school.phone"),"school.phone");
but throws.
Code:
org.hibernate.PropertyNotFoundException: Could not find setter for school.phone on class com.generic.model.Student
i have try using DTO is very easy but the requirement is return student from the method..
my question is know can i instruct hibernate that the fetch populate on the parent table[School] in the phone field that i can navigate on the relationShip in this way
Code:
student.getSchool().getPhone()
the only i need is apply the filters and return [Student.getSchool().getParent()] from the 2 tables
thanks a lot.