package com.hbp.chapter2.model;
import junit.framework.TestCase;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class CompanyTest extends TestCase {
private SessionFactory sessionFactory;
protected void setUp() throws Exception {
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
}
public void testCreateCompany() {
Company cp1 = new Company();
cp1.setName("Alibaba.com");
cp1.setUrl("http://www.alibaba.com");
cp1.setCreationDate(new Date());
Company cp2 = new Company();
cp2.setName("Alipay.com");
cp2.setUrl("http://www.alipay.com");
cp2.setCreationDate(new Date());
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(cp1);
session.save(cp2);
// session.evict(cp);
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
fail();
} finally {
session.close();
}
}
public void testBatchEvict() {
Session session = sessionFactory.openSession();
Company c1 = (Company) session.load(Company.class, new Long(1));
System.out.println("装载 Company [" + c1.getName() + "] 到一级缓存");
Company c2 = (Company) session.load(Company.class, new Long(2));
System.out.println("装载 Company [" + c2.getName() + "] 到一级缓存");
session.evict(c1);
System.out.println("清空了id为1的对象的一级缓存,下次在一级缓存中加载时将执行sql,而不会得到脏数据。");
Transaction tx = session.beginTransaction();
Query query = session.createQuery(
"update Company set name = 'BraveSoft'");
query.executeUpdate();
tx.commit();
Company cc1 = (Company) session.get(Company.class, new Long(1));
System.out.println("由于清空id为1的对象的一级缓存,上面会执行sql查询,不会得到脏数据:Company [" +
cc1.getName() + "]");
Company cc2 = (Company) session.get(Company.class, new Long(2));
System.out.println("由于没有清空id为2的对象的一级缓存,将得到脏数据:Company [" +
cc2.getName() + "]");
session.close();
Session session2 = sessionFactory.openSession();
Company cc = (Company) session2.get(Company.class, new Long(1));
if (cc != null) {
System.out.println("换了一个session, 二级缓存已被清空了,需要执行sql查询 Company [" +
cc.getName() + "]");
}
session2.close();
}
public void testBatchUpdateCompany() {
Session session = sessionFactory.openSession();
Company c = (Company) session.load(Company.class, new Long(1));
System.out.println("装载 Company [" + c.getName() + "] 到一级缓存");
Transaction tx = session.beginTransaction();
Query query = session.createQuery(
"update Company set name = 'BraveSoft'");
query.executeUpdate();
tx.commit();
Company c1 = (Company) session.get(Company.class, new Long(1));
System.out.println("装载一级缓存中对象(脏数据):Company [" + c1.getName() + "]");
session.close();
Session session2 = sessionFactory.openSession();
Company c2 = (Company) session2.get(Company.class, new Long(1));
if (c2 != null) {
System.out.println("重新执行Hibernate查询获取对象:Company [" + c2.getName() +
"]");
}
session2.close();
}
//
// public void testIterate() {
// Session session = sessionFactory.openSession();
// Iterator iter = session.createQuery(
// "FROM Company c, Category cat ORDER BY c.creationDate").iterate();
//
// while (iter.hasNext()) {
// Company c = (Company) iter.next();
// System.out.println("Result: " + c.getName());
// }
// }
//
// public void testTuple() {
// Session session = sessionFactory.openSession();
// List result = session.createQuery(
// "FROM Company AS comp JOIN Category AS cate").list();
// Iterator iter = result.iterator();
//
// while (iter.hasNext()) {
// Company c = (Company) iter.next();
// System.out.println("Result: " + c.getName());
// }
// }
public void testMapResults() {
Session session = sessionFactory.openSession();
List result = session.createQuery(
"SELECT new map(c.name AS name, c.url AS url) FROM Company c")
.list();
Iterator iter = result.iterator();
while (iter.hasNext()) {
Map m = (Map) iter.next();
for (Iterator i = m.entrySet().iterator(); i.hasNext();) {
Map.Entry e = (Map.Entry) i.next();
System.out.println(e.getKey() + ", " + e.getValue());
}
}
}
public void testListResults() {
Session session = sessionFactory.openSession();
List result = session.createQuery(
"SELECT new Company(c.name, c.url) FROM Company c").list();
Iterator iter = result.iterator();
while (iter.hasNext()) {
Company c = (Company) iter.next();
System.out.println("Result: " + c);
}
}
public void testResults() {
Session session = sessionFactory.openSession();
List result = session.createQuery("SELECT c.name, c.url FROM Company c")
.list();
Iterator iter = result.iterator();
while (iter.hasNext()) {
Object[] row = (Object[]) iter.next();
System.out.println("Result: " + row[0] + "," + row[1]);
}
}
public void testList() {
Session session = sessionFactory.openSession();
List result = session.createQuery(
"FROM Company AS c ORDER BY c.creationDate").list();
Iterator iter = result.iterator();
while (iter.hasNext()) {
Company c = (Company) iter.next();
System.out.println("Result: " + c.getName());
}
}
public void testUpdate() {
Session s1 = sessionFactory.openSession();
Transaction tx = s1.beginTransaction();
Company c2 = (Company) s1.load(Company.class, new Long(1));
c2.setName("Taobao.com");
s1.createQuery("from Company").list();
tx.commit();
s1.close();
}
public void testSessionCache() {
Session s1 = sessionFactory.openSession();
Transaction tx = s1.beginTransaction();
Company c1 = new Company();
c1.setName("Albiaba.com");
s1.save(c1);
Long id = c1.getId();
c1 = null;
Company c2 = (Company) s1.load(Company.class, id);
tx.commit();
s1.close();
System.out.println(c2.getName());
c2 = null;
}
public void testLoad() {
Session s1 = sessionFactory.openSession();
Transaction tx1 = s1.beginTransaction();