(1) oneToMany 单向
java类:Comment Message
public class Comment {
private int id;
private String content;
省略get(),set();
}
public class Message {
private int id;
private String title;
private String content;
private Set<Comment> comments;
省略get(),set();
}
Message.hbm.xml文件
<hibernate-mapping package="org.th.model">
<class name="Message" table="t_message" >
<id name="id">
<generator class="native"></generator>
</id>
<property name="title"></property>
<property name="content"></property>
<!-- 使用了lazy="extra" 当返回结果是数字会使用count,否则使用投影 (智能一点)-->
<set name="comments" lazy="extra">
<!--key用来指定在对方的外键名称 -->
<key column="mid"></key>
<!-- class 用来设置列表中的对象类型 -->
<one-to-many class="Comment"/>
</set>
</class>
</hibernate-mapping>
Comment.hbm.xml
<hibernate-mapping package="org.th.model">
<class name="Comment" table="t_comment" >
<id name="id">
<generator class="native"></generator>
</id>
<property name="content"></property>
</class>
</hibernate-mapping>
测试类:OneToMany
public class OneToMany {
@Test
public void testAdd(){
Session session=null;
try {
session = HibernateUtil.openSession();
session.beginTransaction();
Comment c1=new Comment();
c1.setContent("黑河");
Comment c2=new Comment();
c2.setContent("白河");
session.save(c1);session.save(c2);
Message m=new Message();
m.setTitle("中国好声音");
m.setContent("123");
m.addComment(c1);
m.addComment(c2);
session.save(m);
//此时会发出5条sql 2条保存c1,c2。一条保存Message(其中addComment(c1) 由于延迟加载,只是将c1的id存在当中)
//当提交事务的 时候session中的对象会与内存中进行比较,不相同即update
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
if(session!=null) session.getTransaction().rollback();
}finally{
if(session!=null) HibernateUtil.close(session);
}
}
@Test
public void testLoad(){
Session session=null;
try {
session = HibernateUtil.openSession();
session.beginTransaction();
Message message=(Message)session.load(Message.class, 1);
System.out.println(message.getContent());
for(Comment c:message.getComments()){
System.out.println(c.getContent());
}
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
if(session!=null) session.getTransaction().rollback();
}finally{
if(session!=null) HibernateUtil.close(session);
}
}
//特别注意:OneToMany在添加和维护关系时比较麻烦,所以在开发中不建议使用OneToMany的单向
@Test
public void testLoad2(){
Session session=null;
try {
session = HibernateUtil.openSession();
session.beginTransaction();
Message message=(Message)session.load(Message.class, 1);
System.out.println(message.getComments().size());
// OneToMany 在不使用特别标签时,
//Hibernate: select message0_.id as id4_0_, message0_.title as title4_0_, message0_.content as content4_0_ from t_message message0_ where message0_.id=?
//Hibernate: select comments0_.mid as mid4_1_, comments0_.id as id5_1_, comments0_.id as id5_0_, comments0_.content as content5_0_ from t_comment comments0_ where comments0_.mid=?
// OneToMany 在使用<set name="comments" lazy="extra">时 当返回结果是数字会使用count,否则使用投影 (智能一点)
//Hibernate: select message0_.id as id4_0_, message0_.title as title4_0_, message0_.content as content4_0_ from t_message message0_ where message0_.id=?
//Hibernate: select count(id) from t_comment where mid =?
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
if(session!=null) session.getTransaction().rollback();
}finally{
if(session!=null) HibernateUtil.close(session);
}
}
}