标签:static cto configure cti products uil open 多个 关系
1:为Category类增加Set<Product> products;
2:为Category.hbm.xml增加one-to-many映射;
<set name="products" lazy="false">
<key column="cid" not-null="false" />
<one-to-many class="Product" />
</set>
//一对多关系 public static void oneToMany() { SessionFactory sf=new Configuration().configure().buildSessionFactory(); Session s=sf.openSession(); s.beginTransaction(); //获得主键为28的Category对象 Category c=(Category)s.get(Category.class, 28); //获得外键为28的Product集合 Set<Product> set=c.getProducts(); for(Product p:set) { System.out.println(p.getName()); } }
参考:http://how2j.cn/k/hibernate/hibernate-many-to-many/42.html#nowhere
标签:static cto configure cti products uil open 多个 关系
原文地址:https://www.cnblogs.com/lastingjava/p/9912256.html