Hibernate Criteria的嵌套查询

假设有Artist和Track实体,是1对多的关系,现在我们想使用Criteria查询所有artist中,其对应的Track实体集合中,有名为track1的Track实体所对应的Artist

比较乱,用SQL来举例

只有artist1和artist2对应的track集合中1有名为“track1”的记录,我们就要找出artist1和artist2来

create   table  artist(id  varchar ( 20 ),name  varchar ( 20 ));
create   table  track (id  varchar ( 20 ),name  varchar ( 20 ),artist_id  varchar ( 20 ));

insert   into  artist  values (" 1 ","artist1");
insert   into  artist  values (" 2 ","artist2");
insert   into  artist  values (" 3 ","artist3");


insert   into  track  values (" 1 ","track1"," 1 ");
insert   into  track  values (" 2 ","track2"," 1 ");
insert   into  track  values (" 3 ","track3"," 1 ");

insert   into  track  values (" 4 ","track1"," 2 ");

insert   into  track  values (" 5 ","track4"," 3 ");
insert   into  track  values (" 6 ","track5"," 3 ");

 

 pojo:

 

package  RelationCriteria;

import  java.util.Set;

public   class  Artist  ... {
   
private String id;
   
private String name;
   
private Set tracks;
public String getId() ...{
    
return id;
}

public void setId(String id) ...{
    
this.id = id;
}

public String getName() ...{
    
return name;
}

public void setName(String name) ...{
    
this.name = name;
}

public Set getTracks() ...{
    
return tracks;
}

public void setTracks(Set tracks) ...{
    
this.tracks = tracks;
}

}




package  RelationCriteria;

public   class  Track  ... {
   
private String id;
   
private String name;
   
private Artist artist;
public String getId() ...{
    
return id;
}

public void setId(String id) ...{
    
this.id = id;
}

public String getName() ...{
    
return name;
}

public void setName(String name) ...{
    
this.name = name;
}

public Artist getArtist() ...{
    
return artist;
}

public void setArtist(Artist artist) ...{
    
this.artist = artist;
}

}

 

Artist.hbm.xml

 

<? xml version="1.0" encoding="utf-8" ?>
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<!--  
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping  package ="RelationCriteria"   >
    
< class  name ="RelationCriteria.Artist"  table ="artist"  lazy ="true" >
       
< id  name ="id"  column ="id" >
         
< generator  class ="uuid.hex" ></ generator >
       
</ id >
       
< property  name ="name"  column ="name" ></ property >
       
< set  name ="tracks"  lazy ="true"  inverse ="true"  outer-join ="false" >
         
< key  column ="artist_id" ></ key >
         
< one-to-many  class ="RelationCriteria.Track" />
       
</ set >
      
</ class >
</ hibernate-mapping >

 

Track.hbm.xml

 

<? xml version="1.0" encoding="utf-8" ?>
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<!--  
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping  package ="RelationCriteria"   >
   
    
< class  name ="RelationCriteria.Track"  table ="track"  lazy ="true" >
       
< id  name ="id"  column ="id"  unsaved-value ="null" >
         
< generator  class ="uuid.hex" ></ generator >
       
</ id >

       
< property  name ="name"  column ="name" ></ property >
       
< many-to-one  name ="artist"  
                    column
="artist_id"
                    outer-join
="true"  
                    class
="RelationCriteria.Artist" ></ many-to-one >
      
</ class >
</ hibernate-mapping >

 

Hibernate.cfg.xml

 

<? xml version='1.0' encoding='UTF-8' ?>
<! DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>

<!--  Generated by MyEclipse Hibernate Tools.                    -->
< hibernate-configuration >

< session-factory >
    
< property  name ="connection.username" > root </ property >
    
< property  name ="connection.url" >
        jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312
&amp; useUnicode=true
    
</ property >
    
< property  name ="dialect" >
        org.hibernate.dialect.MySQLDialect
    
</ property >
    
< property  name ="myeclipse.connection.profile" > mysql </ property >
    
< property  name ="connection.password" > 1234 </ property >
    
< property  name ="connection.driver_class" >
        com.mysql.jdbc.Driver
    
</ property >
    
< property  name ="hibernate.dialect" >
        org.hibernate.dialect.MySQLDialect
    
</ property >
    
< property  name ="hibernate.show_sql" > true </ property >
    
< property  name ="current_session_context_class" > thread </ property >
    
< mapping  resource ="RelationCriteria/Artist.hbm.xml"   />
    
< mapping  resource ="RelationCriteria/Track.hbm.xml"   />

</ session-factory >

</ hibernate-configuration >

 

测试代码:

 

package  RelationCriteria;

import  java.io.File;
import  java.util.Iterator;
import  java.util.List;

import  org.hibernate.Criteria;
import  org.hibernate.Session;
import  org.hibernate.SessionFactory;
import  org.hibernate.Transaction;
import  org.hibernate.cfg.Configuration;
import  org.hibernate.criterion.Expression;

;

public   class  Test  ... {


    
public static void<spa>

你可能感兴趣的:(sql,Hibernate,mysql,xml,MyEclipse)