-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to use Hibernate createCriteria()
PostPosted: Wed May 17, 2006 12:24 pm 
Newbie

Joined: Wed May 17, 2006 12:07 pm
Posts: 4
I have two tables: Project and Study. Their relationship is many-to-one. I want to write the following query:
select * from Project project
inner join Study study
on project.study_id = study.study_id and
project.name = projectname and
study.name = studyname;

The code I wrote for the query is:

Criteria crit = session.createCriteria(Project.class);

crit.add(Restrictions.eq("name", "B2")); // set restriction on project's name

crit.createCriteria(study).add(Restrictions.eq("name", "S2")); // perform the join and set restriction on study's name

However, the generated sql query looks like:

select
this_.project_id as project1_9_2_,
this_.name as name9_2_,
this_.study_id as study4_9_2_,
this_.description as descript3_9_2_,
this_.created_by_user_id as created5_9_2_,
study1_.study_id as study1_8_0_,
study1_.name as name8_0_,
study1_.description as descript3_8_0_,
study1_.created_by_user_id as created5_8_0_,
study1_.created_date as created4_8_0_,
studyproje4_.study_id as study4_4_,
studyproje4_.project_id as project1_4_,
studyproje4_.project_id as project1_9_1_,
studyproje4_.name as name9_1_,
studyproje4_.study_id as study4_9_1_,
studyproje4_.description as descript3_9_1_,
studyproje4_.created_by_user_id as created5_9_1_

from projects_v1 this_
inner join studies_v1 study1_
on this_.study_id=study1_.study_id
left outer join projects_v1 studyproje4_
on study1_.study_id=stud yproje4_.study_id
where this_.name like ? and study1_.name like ?

Anybody know why there is a "left outer join" and how to write criteria which works correctly?
Thank you !


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 12:34 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Code:
Criteria crit =  session.createCriteria( Project.class , "myproj" );
crit.createAlias( "studies", "mystudy" );
crit.add( Restrictions.eqProperty( "myproj.studyId", "mystudy.studyId" ) );
crit.add( Restrictions.eq( "myproj.name", "MyProjectNameValue" ) );
crit.add( Restrictions.eq( "mystudy.name", "MyStudyNameValue" ) );


I think you can change the above as needed according to your requirements.


Top
 Profile  
 
 Post subject: What is the "left outer join"
PostPosted: Wed May 17, 2006 2:14 pm 
Newbie

Joined: Wed May 17, 2006 12:07 pm
Posts: 4
I did the way you said. But it resulted the same query and the results were duplicate. I still don't understand why there is "left outer join".

(DEBUG SQL)
select
this_.project_id as project1_9_2_,
this_.name as name9_2_,
this_.description as descript3_9_2_,
this_.created_by_user_id as created4_9_2_,
this_.study_id as study5_9_2_,
mystudy1_.study_id as study1_8_0_,
mystudy1_.name as name8_0_,
mystudy1_.description as descript3_8_0_,
mystudy1_.created_by_user_id as created5_8_0_,
mystudy1_.created_date as created4_8_0_,
studyproje4_.study_id as study5_4_,
studyproje4_.project_id as project1_4_,
studyproje4_.project_id as project1_9_1_,
studyproje4_.name as name9_1_,
studyproje4_.description as descript3_9_1_,
studyproje4_.created_by_user_id as created4_9_1_,
studyproje4_.study_id as study5_9_1_
from projects_v1 this_
inner join studies_v1 mystudy1_
on this_.study_id=mystudy1_.study_id
left outer join projects_v1 studyproje4_
on mystudy1_.study_id=studyproje4_.study_id
where this_.study_id=mystudy1_.study_id and this_.name=? and mystudy1_.name=?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 2:30 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
With the following code

Code:
Criteria dc =  session.createCriteria( TableA.class , "myTable1" );
dc.createAlias( "tableBSet", "myTable2" );
dc.add( Restrictions.eqProperty( "myTable1.columnOne", "myTable2.columnOne" ) );
dc.add( Restrictions.eqProperty( "myTable1.columnTwo", "myTable2.colunTwo" ) );
dc.add( Restrictions.eq( "myTable1.columnOne", new Long( 1 ) ) );
dc.add( Restrictions.eq( "myTable2.columnThree", new Boolean(false) ) );


I have the following SQL logged in console

Code:
select
     table1.ALL_COLUMNS, table2,ALL_COLUMNS
from
     TABLE_A table1
inner join
     TABLE_B table2
         on table1.COL_1=table2.COL_1
         and table1.COL_2=table2.COL_2
where
     table1.COL_1 = table2.COL_1
     and table1.COL_2 = table2.COL_2
     and table1.COL_1=?
     and table2.COL_3=?



and I have bi-directional relation defined on the mapping files. Dont know why you have a different SQL with the same code.

If you want distinct results, try setting
Criteria.setResultTransformer( Criteria.DISTINCT_ROOT_ENTITY );


Last edited by bkmr_77 on Thu May 18, 2006 9:47 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 9:13 am 
Newbie

Joined: Wed May 17, 2006 12:07 pm
Posts: 4
The mapping between Study and Project is as following:
In Project.java

@ManyToOne( cascade={CascadeType.REFRESH})
@JoinColumn(name="study_id")
public Study getStudy () { return ( _study ); }
public void setStudy ( Study study ) { _study = study; }


In Study.java

@OneToMany(mappedBy="study", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
public List<Project> getProjects() throws DataStoreException {
return _projects;
}
public void setProjects(List<Project> projects) {
this._projects = projects;
}
public void addProjects(Project proj) throws DataStoreException {
if (proj == null)
throw new IllegalArgumentException("Can't add a null Project.");
this.getProjects().add(proj);
}


Can you teach me how to map beans?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 9:46 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
I havent used annotations yet, so atleast I will provide mapping files so that you can build on this example, if it helps.

Code:
<?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="learn.hibernate">   
   <class name="TableA" table="table1" > 
      <composite-id>
         <key-property name="columnOne" column="COL_1"/>
         <key-property name="columnTwo" column="COL_2"/>
      </composite-id>

      <set name="tableBSet" inverse="true" lazy="true" cascade="all-delete-orphan">
         <key>
            <column name="COL_1"/>
            <column name="COL_2"/>
         </key>
         <one-to-many class="TableB">
      </set>
   </class>

   <class name="TableB" table="table2" > 
      <composite-id>
         <key-property name="columnOne" column="COL_1"/>
         <key-property name="columnTwo" column="COL_2"/>
         <key-property name="columnThree" column="COL_3"/>
      </composite-id>

      <many-to-one name="tableAObject" class="TableA" insert="false" update="false">
         <column name="COL_1"/>
         <column name="COL_2"/>
      </many-to-one>
   </class>
</hibernate-mapping>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.