Hibernate级联操作

package com.secn.user;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;


//在多的一端来维护表
public class SecnTest {
@Test
public void danxiangtianjia(){ //单行多对一
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
NewsSort newsSort = new NewsSort();
newsSort.setSortName("news FenLei");
News news = new News();
news.setNewName("Li Lian Jie");
news.setNewsSort(newsSort);
SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(news);
//System.out.println(newsSort.getSortName());
session.getTransaction().commit();
session.close();
sf.close();
}
@Test
public void shuangxiangtianjia(){ //双向多对一
//new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
News news1 = new News();
news1.setNewName("Li Lian Jie");
News news2 = new News();
news2.setNewName("Li Xiao Long");
NewsSort newsSort = new NewsSort();
newsSort.setSortName("news FenLei");
newsSort.getNews().add(news1);
newsSort.getNews().add(news2);
news1.setNewsSort(newsSort);
news2.setNewsSort(newsSort);
SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(newsSort);
System.out.println(newsSort.getSortName());
session.getTransaction().commit();
session.close();
sf.close();
}
@Test
public void getNews(){ //读取记录
//new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
//@ManyToOne(cascade={CascadeType.ALL})
//cascade:只真对增删改;
//默认情况下也会地取出NewsSort的信息
News news = (News)session.get(News.class, 1);
session.getTransaction().commit();
session.close();
sf.close();
}
@Test
public void GetNewsSort(){ //读取分类
//new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
//默认情况下不会读取出News表的内容
//可以通过fetch=FetchType.EAGER(渴望|勤快)LAZY(懒惰)读取出来
NewsSort newsSort = (NewsSort)session.get(NewsSort.class, 1);
session.getTransaction().commit();
session.close();
sf.close();
}
@Test
public void getLoad(){ //读取记录
//new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
//@ManyToOne(cascade={CascadeType.ALL})
//cascade:只真对增删改;
//默认情况下也会地取出NewsSort的信息
//读新闻时默认不会读出新闻对应分类
News news = (News)session.load(News.class, 1);
System.out.println(news.getNewName());//在这一行调用时才会执行SQL语句
session.getTransaction().commit();
session.close();
sf.close();
}
@Test
public void getUpdateNews(){ //读取记录
//new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
//@ManyToOne(cascade={CascadeType.ALL})
//cascade:只真对增删改;
//默认情况下也会地取出NewsSort的信息
//在分类和新闻中不要都设计fetch=FetchType.EAGER属性
News news = (News)session.load(News.class, 1);
System.out.println(news.getNewsSort().getSortName());//在这一行调用时才会执行SQL语句
session.getTransaction().commit();
session.close();
sf.close();
}

}

你可能感兴趣的:(Hibernate)