Hibernate总结要点
2017-04-22 20:51
190 查看
概念:针对数据库的持久化框架,帮助完成对数据库的增删改查工作
重点:ORM
开发步骤:
1.配置环境:导入jar包,目前共17个;
2.Build path;
3.创建实体Bean,声明对象中的属性,并生成get,set方法;注意点:不要使用基本数据类型,要使用包装类型;
4.配置实体类和数据库表的映射关系;建议映射文件名为实体类名称.hbm.xml;
5.引入外部映射文档:
在window->preference->xml->XML Catalog->User Specified Entries添加上述dtd文件;Key
type:URI;Key:http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd和http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd
6.配置核心配置文件:hibernate.cfg.xml:
创建一个file文件,名字为hibernate.cfg.xml,引入hibernate-configuration-3.0.dtd标头;在<hibernate-configuration></hibernate-configuration>中配置相关信息;
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate_day02</property>
<property name="connection.username">root</property>
<property name="connection.password">root123</property>
<!-- 配置hibernate的信息 可选 -->
<!-- 配置本地session JDBC事务 -->
<property name="current_session_context_class">thread</property>
<!-- 数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 输出底层sql语句 -->
<property name="show_sql">true</property>
<!-- 输出底层sql语句格式化 -->
<property name="format_sql">true</property>
<!-- hibernate创建表的策略 update: 没有就创建,不一个就更新 -->
<property name="hbm2ddl.auto">update</property>
<!-- 将映射文件配置到核心文件中 -->
<mapping resource="tm/change/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
7.在对应的domain下,创建User.hbm.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="tm.change.domain.User" table="t_user">
<!-- hibernate中id主键必须要有 -->
<id name="id" column="id">
<!-- 生成id为自增长,需要加上下面语句 -->
<generator class="native"></generator>
</id>
<property name="username" column="username"></property>
<property name="password" column="password"></property>
<property name="address" column="address"></property>
</class>
</hibernate-mapping>
将此文件配置到核心文件中:
<!-- 将映射文件配置到核心文件中 -->
<mapping resource="tm/change/domain/User.hbm.xml"/>
加入到hibernate.cfg.xml文件中;
8.创建HibernateUtils文件,分为七步:
1)加载配置文件:
2)创建SessionFactory对象;
3)使用SessionFactory对象创建session对象;
4)开启事务;
5)执行操作;
6)提交事务;
7)关闭资源;
public class HibernateUtils {
private static SessionFactory
factory=null;
static{
//1 加载配置文件(核心配置文件)
Configuration cfg=new Configuration().configure();
//2 创建一个SessionFactory
factory=cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return factory;
}
public static Session getSession(){
return factory.openSession();
}
public static Session getCurrentSession(){
return factory.getCurrentSession();//本地session,可以在同一个线程下使用同一个session,不需要认为close(),它会在当前线程结束的时候自动关闭
}
public static void main(String[]
args) {
System.out.println("done");
}
}
在数据库可建立hibernate_day02数据库,运行,可生成表格;
9.创建UserDao类实现增删改查:
1)获取Session对象;
2)开启事务;
3)保存对象;
4)提交事务;
5)若有异常,回滚事务;
public void save(User
user){
Session session=null;
Transaction ts = null;
try{
session = HibernateUtils.getCurrentSession();
ts =session.beginTransaction();
session.save(user);
ts.commit();
}
catch(Exception
e){
ts.rollback();
throw new RuntimeException(e);
}
}
查询:
public User get(Integer
id){
Session session=null;
User user=null;
Transaction ts = null;
try{
session = HibernateUtils.getCurrentSession();
ts =session.beginTransaction();
user=session.load(User.class,
id);
//ts.commit();
}
catch(Exception
e){
//ts.rollback();
throw new RuntimeException(e);
}
return user;
}
注意要点:
1.主键生成策略,常用的两种:
1)native,根据数据库不同选择不同来生成自增长值;
2)UUID,生成唯一字符串;
2.Session.get(User.class,id)方法不支持懒加载;Session.load()支持懒加载;load方法产生代理对象,get方法未使用;
1). 对于get方法,hibernate会确认一下该id对应的数据是否存在,首先在session缓存中查找,然后在二级缓存中查找,还没有就查询数据库,数据库中没有就返回null。
2). load方法加载实体对象的时候,根据映射文件上类级别的lazy属性的配置(默认为true),分情况讨论:
(1)若为true,则首先在Session缓存中查找,看看该id对应的对象
是否 存在,不存在则使用延迟加载,返回实体的代理类对象(该代理类为实体类的子类,由CGLIB动态生成)。等到具体使用该对象(除获取OID以外)的时候,
再查询二级缓存和数据库,若仍没发现符合条件的记录,则会抛出一个ObjectNotFoundException。
(2)若为false,就跟get方法查找顺序一样,只是最终若没发现符合条件的记录,则会抛出一个ObjectNotFoundException。
实体对象的四种状态:
1.瞬时态:对象没有id值,对象与session没有关联;
2.持久态,对象有id值,对象与session有关联;
3.托管态,对象有id,但是与session无关;
4.游离态,对象有id值,只是曾经有
持久态对象的变化,会自动更新到数据库中,建议使用持久态;
瞬时态使用session.save()方法,其他状态使用session.update()方法;
重点:ORM
开发步骤:
1.配置环境:导入jar包,目前共17个;
2.Build path;
3.创建实体Bean,声明对象中的属性,并生成get,set方法;注意点:不要使用基本数据类型,要使用包装类型;
4.配置实体类和数据库表的映射关系;建议映射文件名为实体类名称.hbm.xml;
5.引入外部映射文档:
在window->preference->xml->XML Catalog->User Specified Entries添加上述dtd文件;Key
type:URI;Key:http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd和http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd
6.配置核心配置文件:hibernate.cfg.xml:
创建一个file文件,名字为hibernate.cfg.xml,引入hibernate-configuration-3.0.dtd标头;在<hibernate-configuration></hibernate-configuration>中配置相关信息;
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate_day02</property>
<property name="connection.username">root</property>
<property name="connection.password">root123</property>
<!-- 配置hibernate的信息 可选 -->
<!-- 配置本地session JDBC事务 -->
<property name="current_session_context_class">thread</property>
<!-- 数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 输出底层sql语句 -->
<property name="show_sql">true</property>
<!-- 输出底层sql语句格式化 -->
<property name="format_sql">true</property>
<!-- hibernate创建表的策略 update: 没有就创建,不一个就更新 -->
<property name="hbm2ddl.auto">update</property>
<!-- 将映射文件配置到核心文件中 -->
<mapping resource="tm/change/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
7.在对应的domain下,创建User.hbm.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="tm.change.domain.User" table="t_user">
<!-- hibernate中id主键必须要有 -->
<id name="id" column="id">
<!-- 生成id为自增长,需要加上下面语句 -->
<generator class="native"></generator>
</id>
<property name="username" column="username"></property>
<property name="password" column="password"></property>
<property name="address" column="address"></property>
</class>
</hibernate-mapping>
将此文件配置到核心文件中:
<!-- 将映射文件配置到核心文件中 -->
<mapping resource="tm/change/domain/User.hbm.xml"/>
加入到hibernate.cfg.xml文件中;
8.创建HibernateUtils文件,分为七步:
1)加载配置文件:
2)创建SessionFactory对象;
3)使用SessionFactory对象创建session对象;
4)开启事务;
5)执行操作;
6)提交事务;
7)关闭资源;
public class HibernateUtils {
private static SessionFactory
factory=null;
static{
//1 加载配置文件(核心配置文件)
Configuration cfg=new Configuration().configure();
//2 创建一个SessionFactory
factory=cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return factory;
}
public static Session getSession(){
return factory.openSession();
}
public static Session getCurrentSession(){
return factory.getCurrentSession();//本地session,可以在同一个线程下使用同一个session,不需要认为close(),它会在当前线程结束的时候自动关闭
}
public static void main(String[]
args) {
System.out.println("done");
}
}
在数据库可建立hibernate_day02数据库,运行,可生成表格;
9.创建UserDao类实现增删改查:
1)获取Session对象;
2)开启事务;
3)保存对象;
4)提交事务;
5)若有异常,回滚事务;
public void save(User
user){
Session session=null;
Transaction ts = null;
try{
session = HibernateUtils.getCurrentSession();
ts =session.beginTransaction();
session.save(user);
ts.commit();
}
catch(Exception
e){
ts.rollback();
throw new RuntimeException(e);
}
}
查询:
public User get(Integer
id){
Session session=null;
User user=null;
Transaction ts = null;
try{
session = HibernateUtils.getCurrentSession();
ts =session.beginTransaction();
user=session.load(User.class,
id);
//ts.commit();
}
catch(Exception
e){
//ts.rollback();
throw new RuntimeException(e);
}
return user;
}
注意要点:
1.主键生成策略,常用的两种:
1)native,根据数据库不同选择不同来生成自增长值;
2)UUID,生成唯一字符串;
2.Session.get(User.class,id)方法不支持懒加载;Session.load()支持懒加载;load方法产生代理对象,get方法未使用;
1). 对于get方法,hibernate会确认一下该id对应的数据是否存在,首先在session缓存中查找,然后在二级缓存中查找,还没有就查询数据库,数据库中没有就返回null。
2). load方法加载实体对象的时候,根据映射文件上类级别的lazy属性的配置(默认为true),分情况讨论:
(1)若为true,则首先在Session缓存中查找,看看该id对应的对象
是否 存在,不存在则使用延迟加载,返回实体的代理类对象(该代理类为实体类的子类,由CGLIB动态生成)。等到具体使用该对象(除获取OID以外)的时候,
再查询二级缓存和数据库,若仍没发现符合条件的记录,则会抛出一个ObjectNotFoundException。
(2)若为false,就跟get方法查找顺序一样,只是最终若没发现符合条件的记录,则会抛出一个ObjectNotFoundException。
实体对象的四种状态:
1.瞬时态:对象没有id值,对象与session没有关联;
2.持久态,对象有id值,对象与session有关联;
3.托管态,对象有id,但是与session无关;
4.游离态,对象有id值,只是曾经有
持久态对象的变化,会自动更新到数据库中,建议使用持久态;
瞬时态使用session.save()方法,其他状态使用session.update()方法;
相关文章推荐
- Robbin总结的Hibernate性能优化要点
- 大师总结Hibernate性能优化要点(转载)
- Hibernate框架知识要点总结
- 软件测试基础要点总结
- 易被遗忘的C/C++要点总结
- Hibernate检索方式简单总结
- hibernate操作数据库总结
- 关于mysql init_connect的几个要点总结
- Hibernate学习总结
- 开发遇到问题之总结---接触到的hibernate
- (转)【Hibernate总结系列】使用举例
- Hibernate知识点总结
- Hibernate延迟加载小小总结
- 广域网技术-要点总结
- UML建模的要点总结(一)
- Ajax发送与返回数据格式注意的要点总结
- OSPF高级配置-要点总结
- Hibernate总结(一)--之hibernate配置文件
- wince下的USB驱动要点总结