-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Java Hibernate Criteria Projection Transformer PropertyNotFo
PostPosted: Sun Jun 02, 2013 2:06 pm 
Newbie

Joined: Sun Nov 13, 2011 4:35 pm
Posts: 6
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.