欢迎投稿

今日深度:

Hibernate基础-HelloWord,-helloword

Hibernate基础-HelloWord,-helloword


1. ORM ORM (Object /Relation Mapping ): 对象/关系映射(理解)      1 ORM 主要解决对象 -关系的映射      2 .ORM的思想:将关系数据库中表中的记录映射成为对象,以对象的形式展现,程序员可以把对数据库的操作转化为对对象的操作。
2. Hibernate  HelloWord      1 . 加入 jar 包:加入到当前项目的 classpath 下                                       hibernate-release-4.2.4.F inal\lib\required\*.jar(所有的包)                                                      MySQL 的驱动mysql -connector -java -5.1.29 -bin.jar                                          
     2 . 配置 hibernate 的配置文件: hibernate.cfg.xml
     . 利用 hibernate 插件生成 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 >                              </session -factory >           </hibernate-configuration>
     . 编辑 hibernate.cfg.xml 文件
     I. 加入链接数据库的基本信息:
         <! -- 配置连接数据库的基本信息 -->          <property name ="connection.driver_class" >com.mysql.jdbc.Driver </property >          <property name ="connection.username" >root </property >          <property name ="connection.password" >123456 </property >          <property name ="connection.url" >jdbc :mysql:///hibernate4</property>
     II. 配置 Hibernate 使用的数据库方言:          每一种数据库使用的基本语法会有细微的区别,例如分页 MySQL 使用 limit,而 Oracle 使用 rownum。这就需要告诉 Hibernate 底层使用的是哪一种数据库
         <! -- 配置 Hibernate 的数据库方言 -->          <property name ="dialect" >org.hibernate.dialect.MySQLInnoDBDialect </property >             注意:方言对应的类来自于 hibernate-release- 4.2.4.Final\project\etc\hibernate.properties           III. 编辑 Hibernate 的一般属性
         <! -- 创建数据表的策略(了解,最多使用的值是 update -->          <property name ="hbm2ddl.auto" >update </property >
         > create : 每次运行都会删除上一次的表 ,重新生成表 , 哪怕二次没有任何改变          > create -drop :会根据 .hbm.xml 文件生成表, 但是SessionFactory 一关闭,  表就自动删除          > update :最常用的属性值,也会根据 .hbm.xml 文件生成表 , 但若 .hbm.xml  文件和数据库中对应的数据表的表结构不同 ,             Hiberante  将更新数据表结构,但不会删除已有的行和列          > validate : 会和数据库中的表进行比较 ,  .hbm.xml 文件中的列在数据表中不存在,则抛出异常

         <! -- 是否打印 SQL -->          <property name ="show_sql" >true</property>
         <! -- 是否格式化 SQL -->          <property name ="format_sql" >true</property>
3 . 编写实体类( POJO)及 Hibernate 映射文件: xxx.hbm.xml
    I. 编写一个 POJO :必须包含一个 OID 字段和数据表主键对应;必须有一个无参数的构造器;为字段定义 getter setter;非 final 
    II.  hibernate 插件生成 xxx.hbm.xml 文件
    注意:需要对文件进行简单的修改:修改主键生成方式(使用 id  generator 子节点的 class 属性配置主键的生成方式, native 表示使用数据库本地的方式来     生成主键, MySQL 会自动的选用 auto_increment,而 Oracle 则使用序列的方式
    <generator class ="assigned" /> 修改为 <generator class="native" />
    III.  hibernate 配置文件(hiberante.cfg.xml )中关联 hibernate 持久化类的映射文件     <mapping resource ="com/atguigu/hibernate/entities/News.hbm.xml" />
4 . 通过 Hibernate API 完成持久化操作
    1. 创建 SessionFactory : Session 的工厂类。SessionFactory 是线程安全的,一般地,在一个 Java 应用中只有一个 SessionFactory 实例         Configuration configuration = new Configuration ().configure ();         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings( configuration.getProperties())                                                       .buildServiceRegistry ();         SessionFactory sessionFactory = configuration.buildSessionFactory (serviceRegistry );
    2. 创建 Session : 表示 Hibernate 应用程序和数据库的一次会话         Session session = sessionFactory.openSession ();
    3. 开启事务         Transaction transaction = session.beginTransaction ();
    4. 执行持久化操作          //save          //session.save (news );
         //利用 OID 加载对象         News news2 = (News ) session.get (News.class , 1);         System.out.println (news2 );         news2.setContent ("myBatis" );
    5. 提交事务         transaction.commit ();
    6. 关闭 Session         session.close ();
    7. 关闭 SessionFactory         sessionFactory.close ();         

    ★写测试类的时候一般采用注解更方便些:         public class Testing {             private SessionFactory sessionFactory ;             private Session session ;             private Transaction transaction = null ;                          @Before             public void init (){                 Configuration configuration = new Configuration ().configure ();                 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings( configuration.getProperties()).buildServiceRegistry();                 sessionFactory = configuration.buildSessionFactory (serviceRegistry );                                  session = sessionFactory.openSession ();                 transaction = session.beginTransaction ();              }                          @After             public void destroy (){                 transaction.commit ();                 session.close ();                 sessionFactory.close ();              }                          @Test             public void test (){                 //测试部分              }          }               


hibernate问题

你的jdbc连接出错,看看jdbc的用户名和密码有没有写错,还有主机名,数据库名,端口号都检查一遍
 

Exception in thread "main" javalangExceptionInInitializerError at orghibernatecfgConfiguration

从at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:167)
这句话来看,应该是加载hibernate的config文件的时候就已经出错了,
也就是说session根本就没有拿到。
你先试试能不能获取到cfg DUG看看有没有东西。我怀疑就是这里。
 

www.htsjk.Com true http://www.htsjk.com/shujukunews/4438.html NewsArticle Hibernate基础-HelloWord,-helloword 1. ORM : ORM ( Object / Relation Mapping ): 对象 / 关系映射(理解) 1 ) ORM 主要解决对象 - 关系的映射 2 ) .ORM 的思想:将关系数据库中表中的记录映射成为对象...
评论暂时关闭