package Collection.Sort; import java.io.File; import java.util.Iterator; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; publicclass Test ...{ publicstaticvoid main(String[] args) ...{ String filePath=System.getProperty("user.dir")+File.separator+"src/Collection/Sort"+File.separator+"hibernate.cfg.xml"; File file=new File(filePath); System.out.println(filePath); SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory(); Session session=sessionFactory.openSession(); Transaction t=session.beginTransaction(); Team team=(Team)session.createQuery("from Team t where t.teamname='team1'").uniqueResult(); Set result=team.getStudents(); Iterator iter=result.iterator(); while(iter.hasNext())...{ System.out.println(iter.next()); } t.commit(); } }
运行结果:
Hibernate: select team0_.id as id0_, team0_.teamname as teamname0_ from teamSort team0_ where team0_.teamname='team1' Hibernate: select students0_.team_id as team1_0_, students0_.name as name0_ from studentSort students0_ where students0_.team_id=? a b c
Hibernate: select team0_.id as id0_, team0_.teamname as teamname0_ from teamSort team0_ where team0_.teamname='team1' Hibernate: select students0_.team_id as team1_0_, students0_.name as name0_ from studentSort students0_ where students0_.team_id=? c b a