此处用的是eclipse,已安装hibernate插件
package com.cwh.hibernate.entity;
import static org.junit.Assert.*;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* hibernate基础
* [@author](https://my.oschina.net/arthor) chenwenhuan
*
*/
public class HibernateTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
[@Before](https://my.oschina.net/u/3870904)
public void init(){
//4.1.1版本写法
Configuration configuration = new Configuration().configure();
ServiceRegistry registry =
new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(registry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
System.out.println("begin");
}
@After
public void destroy(){
transaction.commit();
session.close();
sessionFactory.close();
System.out.println("close");
}
/**
* session提供了三个方法 flush():如果查询数据表之前,执行了修改之类的语句,事务commit前会flush,保持数据库和缓存一致
* refresh()重新查数据库,保证数据是最新的,事务隔离级别必须是2
* clear()清理缓存,就是还需要再查一遍
*/
[@Test](https://my.oschina.net/azibug)
public void test() {
//这个方法只查了一次数据库,又缓存
News news = (News) session.get(News.class, 1);
System.out.println(news);
News news2 = (News) session.get(News.class, 1);
System.out.println(news2);
}
/**
* 此处会自动执行update语句,因为commit之前要保证缓存的数据和数据库的一致
*/
[@Test](https://my.oschina.net/azibug)
public void test2(){
News news = (News) session.get(News.class, 1);
System.out.println(news.toString());
news.setAuthor("chen5");
}
/**
* 这里的update方法可以省略,除非中间操作让session中的缓存清掉了
* 如果数据库没有该对象,调用此方法会抛异常
*/
@Test
public void update(){
News news = (News) session.get(News.class, 1);
news.setTitle("test3");
session.update(news);
}
/**
* save还是update,主要看OID是否为空
*/
@Test
public void saveOrUpdate(){
News news = new News("fen", "fen", new Date());
session.saveOrUpdate(news);
}
@Test
public void delete(){
News news = (News) session.get(News.class, 1);
session.delete(news);
}
/**
* 原生态语句
*/
@Test
public void query(){
String hql = "select a from News a where a.author like '%chenwh%'";
String hql2 = "update News set author = 'chen4' where id = 4";
Query query = session.createQuery(hql);
Query query2 = session.createQuery(hql2);
List<News> list = query.list();
System.out.println(list.size()+"ok"+query2.executeUpdate());
System.out.println(list.get(0));
}
}
new Configuration().configure();手动添加源文件
导入包:
设置:
hibernate.cfg.xml文件:
Configuration().configure()就是指向这个文件,文件名默认"hibernate.cfg.xml",可以另起,不过需要在Configuration().configure()的source进行修改。建议使用"hibernate.cfg.xml"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置数据库基本信息-->
<property name="connection.username">root</property>
<property name="connection.password">chenwenhuan</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=UTF-8</property>
<!-- 配置hibernate的基本信息 -->
<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 控制台打印sql -->
<property name="show_sql">true</property>
<!-- 按格式打印sql -->
<property name="format_sql">true</property>
<!-- 数据表生成策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- 事务隔离级别 -->
<property name="connection.isolation">2</property>
<!-- 映射 wenjian -->
<mapping resource="com/cwh/hibernate/hellow/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>