-->
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.  [ 2 posts ] 
Author Message
 Post subject: Criteria problem with double inner join
PostPosted: Tue Nov 09, 2010 12:04 pm 
Newbie

Joined: Tue Nov 09, 2010 11:48 am
Posts: 1
Good morning, im newbie in Hibernate and im facing this challenge.
Im trying to develop a many to many relationship split into two one to many.

This is my configuration mapping file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="bean.hibernate">
<class name="StudentBean" table="STUDENTS">
<id name="idStudent" column="ID_STUDENT">
<generator class="increment"/>
</id>
<set name="stCourse" table="STUDENT_COURSE" cascade="all">
<key column="ID_STUDENT"/>
<!-- <many-to-many column="ID_COURSE" class="CourseBean"/>-->
<one-to-many class="StudentCourseBean"/>
</set>
<property name="nomStudent" column="NOM_STUDENT"/>
<property name="addressStudent" column="ADDRESS"/>
<property name="phoneStudent" column="TELEPHONE"/>
</class>

<class name="CourseBean" table="COURSES">
<id name="idCourse" column="ID_COURSE">
<generator class="increment"/>
</id>
<set name="stCourse" table="STUDENT_COURSE">
<key column="ID_COURSE"/>
<one-to-many class="StudentCourseBean"/>
</set>
<property name="nomCourse" column="NOM_COURSE"/>
<property name="classRoom" column="CLASS_ROOM"/>
</class>

<class name="StudentCourseBean" table="STUDENT_COURSE">
<composite-id name="pkStc" class="PkStudentCourse">
<key-many-to-one name="idStudent" class="StudentBean" column="ID_STUDENT"/>
<key-many-to-one name="idCourse" class="CourseBean" column="ID_COURSE"/>
</composite-id>
</class>
</hibernate-mapping>

These are my POJOS:

public class StudentBean {
private int idStudent;
private String nomStudent;
private String addressStudent;
private String phoneStudent;
//private Set<CourseBean> courses=new HashSet<CourseBean>();
private Set<StudentCourseBean> stCourse=new HashSet<StudentCourseBean>();


public StudentBean() {
super();
}

private int getIdStudent() {
return idStudent;
}

private void setIdStudent(int idStudent) {
this.idStudent = idStudent;
}

public String getNomStudent() {
return nomStudent;
}

public void setNomStudent(String nomStudent) {
this.nomStudent = nomStudent;
}

public String getAddressStudent() {
return addressStudent;
}

public void setAddressStudent(String addressStudent) {
this.addressStudent = addressStudent;
}

public String getPhoneStudent() {
return phoneStudent;
}

public void setPhoneStudent(String phoneStudent) {
this.phoneStudent = phoneStudent;
}

public Set<StudentCourseBean> getStCourse() {
return stCourse;
}

public void setStCourse(Set<StudentCourseBean> stCourse) {
this.stCourse = stCourse;
}
}

public class CourseBean {
private int idCourse;
private String nomCourse;
private String classRoom;
private Set<StudentCourseBean> stCourse=new HashSet<StudentCourseBean>();

public CourseBean(){
super();
}

private int getIdCourse() {
return idCourse;
}

private void setIdCourse(int idCourse) {
this.idCourse = idCourse;
}

public String getNomCourse() {
return nomCourse;
}

public void setNomCourse(String nomCourse) {
this.nomCourse = nomCourse;
}

public String getClassRoom() {
return classRoom;
}

public void setClassRoom(String classRoom) {
this.classRoom = classRoom;
}

public Set<StudentCourseBean> getStCourse() {
return stCourse;
}

public void setStCourse(Set<StudentCourseBean> stCourse) {
this.stCourse = stCourse;
}
}

public class StudentCourseBean {
private PkStudentCourse pkStc;

public StudentCourseBean(){
super();
}

public PkStudentCourse getPkStc() {
return pkStc;
}

public void setPkStc(PkStudentCourse pkStc) {
this.pkStc = pkStc;
}
}

public class PkStudentCourse implements Serializable{
private StudentBean idStudent;
private CourseBean idCourse;

public PkStudentCourse(){
super();
}


public CourseBean getIdCourse() {
return idCourse;
}


public void setIdCourse(CourseBean idCourse) {
this.idCourse = idCourse;
}


public StudentBean getIdStudent() {
return idStudent;
}

public void setIdStudent(StudentBean idStudent) {
this.idStudent = idStudent;
}
}

This is the sentence which is created the query in:

System.out.println("Listando con criteria...");
List listMany=sessionOrder.createCriteria(StudentBean.class)
.createCriteria("stCourse", "stc")
.createAlias("pkStc.idCourse", "c")
.setResultTransformerCriteria.ALIAS_TO_ENTITY_MAP).list();

The execution of this sentence returns this query:
select this_.ID_STUDENT as ID1_3_1_, this_.NOM_STUDENT as NOM2_3_1_, this_.ADDRESS as ADDRESS3_1_, this_.TELEPHONE as TELEPHONE3_1_, stc1_.ID_STUDENT as ID1_5_0_, stc1_.ID_COURSE as ID2_5_0_ from STUDENTS this_ inner join STUDENT_COURSE stc1_ on this_.ID_STUDENT=stc1_.ID_STUDENT

My problem is that i need a second inner join beetween COURSE and STUDENT_COURSE table and as you can show the code doesnt build it. With HQL works fine but with Criteria.....

Any suggestions?.

Thanks in advanced.


Top
 Profile  
 
 Post subject: Re: Criteria problem with double inner join
PostPosted: Thu Dec 16, 2010 11:11 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Hi,

I'm trying to do the same thing, only I took a slight variation from your design.

I haven't solved it yet and you can see it at viewtopic.php?f=1&t=1008718

The difference with my design is that, my two entities do not have a collection of the joining type.

I'm talking about your following source code:
Code:
<one-to-many class="StudentCourseBean"/>


I don't have that in my source code.

I reckoned such a collection could be quite large and preferred not to have it. I don't need it anyway.

If Hibernate wants me to have such a collection in each of the two entities then I'll add it.

But I would prefer not to have it.

I don't know if my forum post is of any help, but I'm still sitting next to you in the trench :-)


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

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.