通过Student对象导航到class对象的id
如下:
POJO
package com.bjsxt.hibernate; import java.util.Date; public class Student { private int id; private String name; private Date createTime; private Classes classes; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Classes getClasses() { return classes; } public void setClasses(Classes classes) { this.classes = classes; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
package com.bjsxt.hibernate; import java.util.Set; public class Classes { private int id; private String name; private Set students; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getStudents() { return students; } public void setStudents(Set students) { this.students = students; } }
<?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> <class name="com.bjsxt.hibernate.Classes" table="t_classes"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <!-- <set name="students" cascade="all" order-by="id"> --> <set name="students" inverse="true"> <key column="classid"/> <one-to-many class="com.bjsxt.hibernate.Student"/> </set> </class> </hibernate-mapping>
<?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> <class name="com.bjsxt.hibernate.Student" table="t_student"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <property name="createTime"/> <many-to-one name="classes" column="classid"/> <filter name="idfilter" condition="id < :myid"/> </class> <query name="searchStudents"><![CDATA[ select s from Student s where s.id < ? ]]> </query> <filter-def name="idfilter"> <filter-param name="myid" type="integer"/> </filter-def> </hibernate-mapping>
下面测试
package com.bjsxt.hibernate; import java.util.Iterator; import java.util.List; import org.hibernate.Session; import junit.framework.TestCase; /** * 对象导航查询测试 * @author Administrator * */ public class ObjectNavQueryTest extends TestCase { public void testQuery1() { Session session = null; try { session = HibernateUtils.getSession(); List students = session.createQuery("select s.name from Student s where s.classes.id < 2").list(); for (Iterator iter = students.iterator();iter.hasNext();) { String name = (String)iter.next(); System.out.println(name); } }catch(Exception e) { e.printStackTrace(); }finally { HibernateUtils.closeSession(session); } } }
上面select s.name from Student s where s.classes.id < 2实际上使用了隐式的inner join
对应的sql语句有两种形式
mysql> select s.name from t_student s ,t_classes c where s.classid = c.id and c. id<2; mysql> select t.name from t_student t inner join t_classes c on t.classid=c.id w here c.id<2;
对象导航其实就是一种隐式join的应用。