heibernate学习

测试类

package com.yh.hib.test;

import java.util.List;

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 org.junit.Test;

import com.yh.hib.entity.Emp;


public class HibernateTest {

@Test
public void selEmp(){
Configuration cfg=null;
SessionFactory sf=null;
Session session=null;
//Transaction tx=null;
try {
cfg=new Configuration().configure();
sf=cfg.buildSessionFactory();
session=sf.openSession();
//tx=session.beginTransaction();
Query query=session.createQuery("from Emp");//从Emp类
List<Emp> list=query.list();
for (Emp emp : list) {
System.out.println(emp.getEname());
}
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(session!=null){
session.close();
}
}
}
/**
* 按主键查找
*/
@Test
public void findById(){
Configuration cfg=null;
SessionFactory sf=null;
Session session=null;

try {
cfg=new Configuration().configure();
sf=cfg.buildSessionFactory();
session=sf.openSession();

//Emp emp=(Emp)session.get(Emp.class, 9999);//立即加载 ,查询不存在的时候,返回null
Emp emp=(Emp)session.load(Emp.class, 9999);//延迟加载、懒加载:用的时候才发送sql .查询不存在的时候,用的时候会抛出异常
System.out.println("7369员工姓名:"+emp.getEname());


} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(session!=null){
session.close();
}
}


}

@Test
public void saveEmp(){
Configuration cfg=null;
SessionFactory sf=null;
Session session=null;
Transaction tx=null;


try {
cfg=new Configuration().configure();
sf=cfg.buildSessionFactory();
session=sf.getCurrentSession();//获取当前线程中的session ,当事务结束,session自动关闭
tx=session.beginTransaction();


Emp emp=new Emp(); //瞬时状态:内存中存在,数据库中不存在,不在session 中
emp.setEmpno(9995);
emp.setEname("王加宾");
emp.setJob("保洁");


session.save(emp);
//session.update(emp);
//session.saveOrUpdate(emp);
//session.merge(emp);


tx.commit();//持久状态:内存中存在,数据库中存在,session 存在
// session.evict(emp);
//
// emp.setJob("经理");
//
// session.update(emp);


} catch (Exception e) {
// TODO Auto-generated catch block

e.printStackTrace();
tx.rollback();
}finally{
if(session!=null&&session.isOpen()){
session.close();//游离状态:数据库中存在,session中不存在
}
}
}

@Test
public void delEmp(){
Configuration cfg=null;
SessionFactory sf=null;
Session session=null;
Transaction tx=null;


try {
cfg=new Configuration().configure();
sf=cfg.buildSessionFactory();
session=sf.getCurrentSession();//获取当前线程中的session ,当事务结束,session自动关闭
tx=session.beginTransaction();

Emp emp=(Emp)session.get(Emp.class, 9999);

session.delete(emp);
tx.commit();

} catch (Exception e) {
// TODO Auto-generated catch block

e.printStackTrace();
tx.rollback();
}finally{
if(session!=null&&session.isOpen()){
session.close();//游离状态:数据库中存在,session中不存在
}
}
}
}

基础类Emp

package com.yh.hib.entity;

public class Emp {
private Integer empno;
private String ename;
private String job;

public Integer getEmpno() {
return empno;
}

public void setEmpno(Integer empno) {
this.empno = empno;
}

public String getEname() {
return ename;
}

public void setEname(String ename) {
this.ename = ename;
}

public String getJob() {
return job;
}

public void setJob(String job) {
this.job = job;
}

}

hibernate 配置文件

xml名:hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- 连接数据库信息 -->
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.Oracle10gDialect
</property>
<property name="hibernate.connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="current_session_context_class">thread</property>

<mapping resource="com/yh/hib/entity/Emp.hbm.xml" />
</session-factory>

</hibernate-configuration>

hibernate映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.yh.hib.entity">
<class name="Emp" table="Emp">
<id name="empno" column="empno" type="java.lang.Integer">
<generator class="assigned"></generator>
</id>
<property name="ename" column="ename" type="java.lang.String"></property>
<property name="job" type="java.lang.String"></property>
</class>
</hibernate-mapping>

原文地址:https://www.cnblogs.com/jimorulang/p/5522204.html