在本次的操作中遇到了一些问题,并且还没有解决。按道理来说我一个Dept下有多个Student,那么我遍历的时候输出的应该就是多个,可是这里遍历出来只输出一个,希望路过的大佬给看看,感激不尽。谢谢! 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"> <hibernate-configuration> <session-factory> <!-- 基本信息 --> <property name="connection.username">hibernate</property> <property name="connection.password">hibernate</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property> <!-- 数据库方言 --> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <!-- 是否在控制台打印sql --> <property name="show_sql">true</property> <!-- 是否对sql格式化 --> <property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 --> <property name="hbm2ddl.auto">update</property> <!-- 指定程序需要的关联的hbm.xml文件,注意不是包名而是路径 --> <mapping resource="com/ls/pojo/Dept.hbm.xml"></mapping> <mapping resource="com/ls/pojo/Student.hbm.xml"></mapping> </session-factory> </hibernate-configuration>dept.java
package com.ls.pojo; import java.util.Set; public class Dept { private String deptid; private String deptname; private Set<Student> students; public Dept(){ } public Dept(String deptid,String deptname){ this.deptid = deptid; this.deptname = deptname; } public Dept(String deptid,String deptname,Set<Student> students){ this.deptid = deptid; this.deptname = deptname; this.students = students; } public String getDeptid() { return deptid; } public void setDeptid(String deptid) { this.deptid = deptid; } public String getDeptname() { return deptname; } public void setDeptname(String deptname) { this.deptname = deptname; } public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } }dept.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-8-6 10:54:40 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.ls.pojo.Dept" table="DEPT"> <id name="deptid" type="java.lang.String"> <column name="DEPTID" /> <generator class="assigned" /> </id> <property name="deptname" type="java.lang.String"> <column name="DEPTNAME" /> </property> <set name="students" table="STUDENT" inverse="true" lazy="true"> <key> <column name="DEPTID" /> </key> <one-to-many class="com.ls.pojo.Student" /> </set> </class> </hibernate-mapping>Student.java
package com.ls.pojo; public class Student { private String sno; private String sname; private String deptid; private Dept dept; //Set<Teacher> teachers; public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getDeptid() { return deptid; } public void setDeptid(String deptid) { this.deptid = deptid; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } }Student.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-8-6 10:54:40 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.ls.pojo.Student" table="STUDENT"> <id name="deptid" type="java.lang.String"> <column name="DEPTID" /> <generator class="assigned" /> </id> <property name="sno" type="java.lang.String"> <column name="SNO" /> </property> <property name="sname" type="java.lang.String"> <column name="SNAME" /> </property> <many-to-one name="dept" class="com.ls.pojo.Dept" fetch="select"> <column name="DEPT" /> </many-to-one> </class> </hibernate-mapping>test类
package com.ls.test; import java.util.Iterator; import java.util.List; import java.util.Set; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.ls.pojo.Dept; import com.ls.pojo.Student; public class OTMTest { @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void test() { SessionFactory sf=null; Configuration con= new Configuration().configure(); Configuration configuration=new Configuration().configure(); ServiceRegistry service= new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); sf=configuration.buildSessionFactory(service); Session session=sf.openSession(); Transaction tr=session.beginTransaction(); Query query=session.createQuery("from Dept"); List list= query.list(); for (int i=0;i<list.size();i++) { Dept dept=(Dept) list.get(i); System.out.println(dept.getDeptid()); Set stu=dept.getStudents(); Iterator itor=stu.iterator(); while(itor.hasNext()) { Student st=(Student) itor.next(); System.out.println(st.getSno()+"::"+st.getSname()+"::"+st.getDeptid()); } } tr.commit(); session.close(); sf.close(); } }