1新建新闻类News与评论类Comment,对应的映射文件分别是:News.hbm.xml与Comment.hbm.xml。
News.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="hibernatedemo.News" table="news" catalog="wuliu">
<id name="nid" type="java.lang.Integer">
<column name="nid" />
<generator class="native" />
</id>
<property name="nname" type="java.lang.String">
<column name="nname" length="30" not-null="true" />
</property>
<set name="comment" inverse="true" cascade="delete">
<key column="nid"></key>
<one-to-many class="hibernatedemo.Comment"/>
</set>
</class>
</hibernate-mapping>
Comment.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="hibernatedemo.Comment" table="comment" catalog="wuliu">
<id name="cid" type="java.lang.Integer">
<column name="cid" />
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="title" length="50" not-null="true" />
</property>
<property name="comments" type="java.lang.String">
<column name="comments" not-null="true" />
</property>
<!-- <property name="nid" type="java.lang.Integer">
<column name="nid" not-null="true" />
</property>-->
<many-to-one name="news" column="nid" class="hibernatedemo.News"
cascade="save-update"
></many-to-one>
</class>
</hibernate-mapping>
测试类
package hibernatedemo;
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;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestNews {
private Configuration config;
private SessionFactory sessionFactory;
private Session session;
private Transaction tx;
@Before
public void init(){
config=new Configuration().configure();
sessionFactory=config.buildSessionFactory();
session=sessionFactory.openSession();
tx=session.beginTransaction();
}
@After
public void destory(){
tx.commit();
session.close();
}
//查找学生
@Test
public void searchTest(){
//查找一方信息
// Comment comment=(Comment)session.get(Comment.class,1);
// System.out.println(comment);
// System.out.println(comment.getNews());
//查询多方
News news=(News)session.get(News.class,1);
Set comment1=news.getComment();
Iterator<Comment> it=comment1.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
//添加学生
@Test
public void addTest(){
Comment comment=new Comment("cb","cb",4);
News news=(News)session.get(News.class,4);
comment.setNews(news);
System.out.println(news);
session.save(comment);
}
//添加新闻和评论,并将评论加入到某一个新闻里面
@Test
public void addNews(){
News news=new News("cb");//添加新闻
Comment comment=new Comment("cb","cb",3);
//将评论保存到新闻里面
comment.setNews(news);
session.save(comment);
}
//修改。将新闻评论换到另外一条新闻里面
@Test
public void updateNews(){
//找到该新闻评论
Comment comment=(Comment) session.get(Comment.class,1);//将object类型的转换为Comment类型的
//找到新闻
News news=(News)session.get(News.class, 2);
//执行更换操作
comment.setNews(news);
session.save(comment);
}
//删除新闻,则其对应的评论也会被删除
@Test
public void deleteNewsAndComment(){
//找到要删除的新闻评论对象
// Comment comment=(Comment) session.get(Comment.class,2);
// session.delete(comment);
//删除新闻,则先找到对应的新闻,然后再删除新闻,采取级联操作,则对应的评论也会删除
News news=(News)session.load(News.class,2);//get与load的区别
session.delete(news);
//当要进行级联删除时,需要在一方的hbm.xml文件里的set元素里设置 cascade="delete" inverse="true"
//在另外多方里的hbm.xml文件里many-to-one文件里设置cascade="save-update" 表明级联关系是删除级联关系,
//由多方进行维护对象之间的关联关系,然后在多方进行更新设置
}
@Test//指定一方的id,而删除的是多方与此id相关联的
public void deleteComment(){//需要将News.hbm.xml文件里的设置成cascade="all-delete-orphan"
//先找到对听的News的ID
News news=(News)session.get(News.class,4);//删除Comment里面nid为4的(与News里面对应的nid为4)
Comment comment=(Comment)news.getComment().iterator().next();
//解除News与Custommer的级联关系
news.getComment().remove(comment);
}
@Test //只删除指定id的News
public void deleteNews(){
News news=(News)session.load(News.class,3);
session.delete(news);//在News.hbm.xml文件里设置cascade属性值为none
}
}