-->
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: Select from 2 tables, Hibernate, onetomany with Criteria.
PostPosted: Mon Aug 08, 2011 11:20 am 
Newbie

Joined: Mon Aug 08, 2011 11:13 am
Posts: 1
I am learning Hibernate and I need help with an issue

I have the following tables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@Entity
public class College {

@Id
@GeneratedValue
private int collegeId;
private String collegeName;

@OneToMany(targetEntity=Student.class, mappedBy="college", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private List<Student> students;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@Entity
public class Student {

@Id
@GeneratedValue
private int studentId;
private String studentName;

@ManyToOne
@JoinColumn(name="college_id")
private College college;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I have the following information
Colegio 1
Estudiante 1
Estudiante 2
Colegio 2
Estudiante 1
Estudiante 3

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Quiero seleccionar el “Colegio 1” y el “estudiante 1” en ese colegio, uso el siguiente código
I want to select “Colegio 1” and the “estudiante 1” in Colegio 1, so I do the following code
public static void main(String[] args) {
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(College.class);
config.addAnnotatedClass(Student.class);
config.configure("hibernate.cfg.xml");

SessionFactory factory = config.buildSessionFactory();
Session session1 = factory.getCurrentSession();
session1.beginTransaction();

Criteria criteria1 = session1.createCriteria(College.class);
criteria1.createAlias("students", "st");
Criterion crit1 = Restrictions.eq("collegeName", "Colegio 1");
Criterion crit2 = Restrictions.eq("st.studentId", 1);
LogicalExpression expression = Restrictions.and(crit1, crit2);
criteria1.add(expression);

List<College> colleges = criteria1.list();

for(College college : colleges)
{
System.out.println("El colegio seria: " + college.getCollegeName());

List<Student> students = college.getStudents();

for(Student student : students)
{
//System.out.println("El colegio seria: " + college.getCollegeName());
System.out.println(" El estudiante es: " + student.getStudentName());
}

}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I have the results
Hibernate: select this_.collegeId as collegeId0_2_, this_.collegeName as collegeN2_0_2_, st1_.studentId as studentId1_0_, st1_.college_id as college3_1_0_, st1_.studentName as studentN2_1_0_, college4_.collegeId as collegeId0_1_, college4_.collegeName as collegeN2_0_1_ from College this_ inner join Student st1_ on this_.collegeId=st1_.college_id left outer join College college4_ on st1_.college_id=college4_.collegeId where (this_.collegeName=? and st1_.studentId=?)

Hibernate: select students0_.college_id as college3_1_, students0_.studentId as studentId1_, students0_.studentId as studentId1_0_, students0_.college_id as college3_1_0_, students0_.studentName as studentN2_1_0_ from Student students0_ where students0_.college_id=?

El colegio seria: Colegio 1
El estudiante es: estudiante 1
El estudiante es: estudiante 2

The first select generate by Hibernate is correct, it selects the information I need.

The issue is the second select (I do not want that select). I believe that select is to relate the student with the college.

I need to eliminate se second select, or add a restriction on that select, something like the college id equal to the college selected in the first select or change the table declaration to keep the consistence but eliminated that second select.

Thank you for your help


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.