/**
*
*/
package example.test;
import java.util.Date;
import java.util.HashSet;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import example.customer.Customer;
import example.jihemapping.Book;
/**
* @author phj
* @time 10:53:37 AM
* @date Jun 2, 2008
* @verison 1.0
*
*/
public class TestSet {
private static SessionFactory factory ;
static {
Configuration cfg = new Configuration();
cfg.configure();
factory = cfg.buildSessionFactory();
}
public void testSave() {
Session session = factory.openSession() ;
Book book =new Book() ;
book.setBookName("j2ee") ;
// book.setTopics(new HashSet()) ;
book.addTopic("java") ;
book.addTopic("O/R Mapping") ;
Transaction tx = null ;
try {
tx = session.beginTransaction() ;
System.out.println(book);
session.save(book) ;
System.out.println(book.toString()) ;
// System.out.println(customer.getId()) ;
// System.out.println(customer.getName()) ;
// System.out.println(customer.getBirthday()) ;
tx.commit() ;
}catch(HibernateException ex) {
ex.printStackTrace() ;
if(tx!=null) {
tx.rollback() ;
}
}finally {
session.close() ;
}
}
public void testLoad() {
Session session = factory.openSession() ;
Transaction tx = null ;
try {
tx = session.beginTransaction() ;
// Book book = (Book)session.load(Book.class, new Long(3));
Book book = (Book)session.get(Book.class, new Long(8));
// System.out.println(book) ;
// book.setName("zhangsan") ;
// book.setBookName("j2ee") ;
// book.addTopic("java") ;
// book.addTopic("O/R Mapping") ;
//强制加载关联的集合数据
// Hibernate.initialize(book.getTopics()) ;
// System.out.println(book.getTopics()) ;
// book.getTopics().clear();
// book.getTopics().remove("O/R Mapping") ;
tx.commit() ;
session.close() ;
System.out.println(book.getTopics()) ;
}catch(HibernateException ex) {
ex.printStackTrace() ;
if(tx!=null) {
tx.rollback() ;
}
}
}
public void testUpdate() {
Session session = factory.openSession() ;
Session session1 = factory.openSession() ;
Transaction tx = null ;
Transaction tx1 = null ;
try {
tx = session.beginTransaction() ;
Book book = (Book)session.load(Book.class, new Long(7));
// Book book = (Book)session.get(Book.class, new Long(7));
System.out.println(book.getTopics()) ;
// session.clear() ;
session.close() ;
// System.out.println(book.getTopics()) ;
Book book1 =new Book() ;
tx1 = session1.beginTransaction() ;
book1.setId(new Long(7)) ;
book1.setBookName("java") ;
book1.addTopic("java") ;
book1.addTopic("j2se") ;
// session1.clear() ;
session1.update(book1);
// session1.clear() ;
System.out.println(book1.getBookName()) ;
tx1.commit() ;
session1.close() ;
}catch(HibernateException ex) {
ex.printStackTrace() ;
if(tx!=null) {
tx.rollback() ;
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestSet testSet = new TestSet() ;
// testSet.testSave() ;
testSet.testLoad() ;
// testSet.testUpdate() ;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
j2ee.hibernate.rar (56个子文件)
j2ee.hibernate
bin
example
zhuheyingse
Person.hbm.xml 1KB
Person.class 2KB
Address.class 1KB
uuid
UUID.hbm.xml 528B
UUID.class 1KB
test
TestStudents.class 2KB
TestOneToMany.class 4KB
TestPerson.class 2KB
TestUser.class 4KB
TestCustomer.class 3KB
TestUUID.class 3KB
TestSet.class 3KB
jihemapping
Book.hbm.xml 623B
Book.class 1KB
guanlianmapping
Income.class 1KB
Income.hbm.xml 1KB
Department.class 2KB
Department.hbm.xml 1023B
bloborclob
User.hbm.xml 910B
User.class 2KB
customer
Customer.hbm.xml 521B
Customer.class 1KB
lianhezhujian
Students.hbm.xml 682B
StudentPK.class 1KB
Students.class 1KB
log4j.properties 215B
hibernate.cfg.xml 2KB
src
example
zhuheyingse
Person.java 1KB
Person.hbm.xml 1KB
Address.java 1KB
uuid
UUID.hbm.xml 528B
UUID.java 657B
test
TestUser.java 2KB
TestStudents.java 2KB
TestSet.java 3KB
TestCustomer.java 3KB
TestPerson.java 1KB
TestUUID.java 3KB
TestOneToMany.java 3KB
jihemapping
Book.hbm.xml 623B
Book.java 1KB
guanlianmapping
Department.java 859B
Income.java 891B
Income.hbm.xml 1KB
Department.hbm.xml 1023B
bloborclob
User.hbm.xml 910B
User.java 3KB
customer
Customer.hbm.xml 521B
Customer.java 667B
lianhezhujian
Students.hbm.xml 682B
Students.java 1KB
StudentPK.java 2KB
log4j.properties 215B
hibernate.cfg.xml 2KB
.project 390B
.classpath 388B
共 56 条
- 1
phj19841123
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0