zoukankan      html  css  js  c++  java
  • hibernate安装和配置和第一个Hibernate应用

    ssh的各个版本要搭配好,struts2.3一般搭配hibernate4.x,我下的是4.3。

    Hibernate4的改动较大只有spring3.1以上版本能够支持,Spring3.1取消了HibernateTemplate,因为Hibernate4的事务管理已经很好了,不用Spring再扩展了这里简单介绍了hibernate4相对于hibernate3配置时出现的错误,只列举了问题和解决方法,详细原理如果大家感兴趣还是去自己搜吧,网上很多。

    1. Spring3.1去掉了HibernateDaoSupport类。hibernate4需要通过getCurrentSession()获取session。并且设置
      <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
      (在hibernate3的时候是thread和jta)。

    2. 缓存设置改为<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
                      <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>

    3. Spring对hibernate的事务管理,不论是注解方式还是配置文件方式统一改为:
      <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >  
      <property name="sessionFactory"><ref bean="sessionFactory"/>
      </property> 
      </bean>

    4. getCurrentSession()事务会自动关闭,所以在有所jsp页面查询数据都会关闭session。要想在jsp查询数据库需要加入:
      org.springframework.orm.hibernate4.support.OpenSessionInViewFilter过滤器。

    5. Hibernate分页出现 ResultSet may only be accessed in a forward direction 需要设置hibernate结果集滚动
       <prop key="jdbc.use_scrollable_resultset">false</prop>

    --------------------------------------------------------------------

    找到篇好文章,我之前遇到的问题都在这都能找到。其实出现这些问题的关键就是hibernate4和hibernate3出现了session管理的变动。

    spring也作出相应的变动....

    错误1:java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider

    原因:spring的sessionfactory和transactionmanager与支持hibernate3时不同。

    解决:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource"/>

    ...

    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">

    <property name="sessionFactory" ref="sessionFactory"/>

    </bean>

    错误2:java.lang.NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session

    原因:hibernate4之后,spring31把HibernateDaoSupport去除,包括数据访问都不需要hibernatetemplate,这意味着dao需要改写,直接使用hibernate的session和query接口。

    解决:为了改写dao,足足花了一天时间,然后一个个接口进行单元测试,这是蛋疼的一个主要原因。

    错误3:nested exception is org.hibernate.HibernateException: No Session found for current thread

    原因:发现一些bean无法获得当前session,需要把之前一些方法的事务从NOT_SUPPORT提升到required,readonly=true

    见https://jira.springsource.org/browse/SPR-9020, http://www.iteye.com/topic/1120924

    解决:

    <tx:advice id="baseServiceAdvice" transaction-manager="transactionManager">

       <tx:attributes>

          <tx:method name="get*" read-only="true" propagation="REQUIRED"/><!--之前是NOT_SUPPORT-->

          <tx:method name="find*" read-only="true" propagation="REQUIRED"/><!--之前是NOT_SUPPORT-->

          <tx:method name="save*" propagation="REQUIRED"/>

          <tx:method name="update*" propagation="REQUIRED"/>

          <tx:method name="remove*" propagation="REQUIRED"/>

          <tx:method name="add*" propagation="REQUIRED"/>

          <!--默认其他方法都是REQUIRED-->

          <tx:method name="*"/>

       </tx:attributes>

    </tx:advice>

    错误4:与错误3报错类似,java.lang.NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;

            at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:324) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]

            at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:202) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]

            at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

    原因:因为opensessioninview filter的问题,如果你的配置还是hibernate3,需要改为hibernate4 

    <filter>

        <filter-name>openSessionInViewFilter</filter-name>

       <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>

    </filter>

    --------------------------------------------------------------------

    由于Hibernate4已经完全可以实现事务了, 与Spring3.1中的hibernatedao,hibernateTemplete等有冲突,所以Spring3.1里已经不提供Hibernatedaosupport,HibernateTemplete了,只能用Hibernate原始的方式用session:

            Session session = sessionFactory.openSession();
            Session session = sessionFactory.getCurrentSession();
    在basedao里可以用注入的sessionFactory获取session.

    注意, 配置事务的时候必须将父类baseServiceImpl也配上,要不然会出现错误:No Session found for currentthread, 以前是不需要的

    SessionFactory.getCurrentSession()的后台实现是可拔插的。因此,引入了新的扩展接口 (org.hibernate.context.spi.CurrentSessionContext)和

    新的配置参数(hibernate.current_session_context_class),以便对什么是“当前session”的范围和上下文(scope and context)的定义进行拔插。

    Spring @Transactional声明式事务管理,”currentSession”的定义为: 当前被 Spring事务管理器 管理的Session,此时应配置:

    hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext。

     

    此处一定注意 使用 hibernate4,在不使用OpenSessionInView模式时,在使用getCurrentSession()时会有如下问题: 当有一个方法list 传播行为为Supports,当在另一个方法getPage()(无事务)调用list方法时会抛出org.hibernate.HibernateException: No Session found for current thread 异常。 这是因为getCurrentSession()在没有session的情况下不会自动创建一个,不知道这是不是Spring3.1实现的bug。 因此最好的解决方案是使用REQUIRED的传播行为

    打开eclipse工具,选择HELP---Install new software----输入网址,如我们这里要安装hibernate tools进行逆向工程的建立,输入http://download.jboss.org/jbosstools/updates/development 选择hibernate tools----安装更新。

    在线安装太慢,我们可以去http://www.jboss.org/tools/download/

    下载对应eclipse版本的插件。

    我的java ee是kepler版本的,

     第0步:部署

    将lib下required文件夹下的所有jar包复制到WEB-INFlib目录下

    1、  创建数据库,数据库名hibernate

    -- ----------------------------
    -- Table structure for `users`
    -- ----------------------------
    DROP TABLE IF EXISTS `users`;
    CREATE TABLE `users` (
      `id` int(11) NOT NULL,
      `name` varchar(20) DEFAULT NULL,
      `age` int(11) DEFAULT NULL,
      `tel` varchar(20) DEFAULT NULL,
      `address` varchar(50) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    2.在src下创建hibernate.cfg.xml配置文件

     hibernate.cfg.xml

    <?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <!-- 数据库链接地址 -->
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
            <!-- 数据库用户名称 -->
            <property name="hibernate.connection.username">root</property>
            <!-- 数据库密码 -->
            <property name="connection.password">admin</property>
            <!-- 设置数据库连接池默认个数 -->
            <property name="connection.pool_size">1</property>
            <!-- 设置数据库SQL语言类型 -->
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <!-- 设置是否显示SQL语句-->
            <property name="show_sql">true</property>
            <!-- 设置是否格式化SQL语句 -->
            <property name="format_sql">true</property>
            <!-- 设置使用线程-->
            <property name="current_session_context_class">thread</property>
            <!-- 设置hibernate的映射文件-->
            <mapping  resource="edu/dzu/vo/UsersVo.hbm.xml"/>
            
        </session-factory>
    </hibernate-configuration>

    填写好数据库名,密码,用户名即可。

    3.编写java类:(创建实体类)

    Users.java

    package edu.dzu.vo;
    
    import java.io.Serializable;
    
    public class Users implements Serializable {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        private Integer id;
        private String name;
        private Integer age;
        private String tel;
        private String address;
        
        public Users(){
            
        }
        
        public Users(Integer id){
            this.id = id;
        }
        
        public Users(Integer id, String name, 
                Integer age, String tel, String address){
            this.id = id;
            this.name = name;
            this.age = age;
            this.tel = tel;
            this.address = address;
        }
        
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getTel() {
            return tel;
        }
        public void setTel(String tel) {
            this.tel = tel;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        
    }

    4.在src中创建edu.dzu.vo包,然后在包中创建:

    UsersVo.hbm.xml (在實體類的包中創建相應的hbm文件,用來指定實體類和數據庫映射關系)

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Hibernate-mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
        
        <class name="edu.dzu.vo.Users" table="users" catalog="hibernate"> 
            
            <id name="id" type="java.lang.Integer">
                <column name="id"></column>
                <generator class="assigned"></generator>
            </id>
            
            <property name="name" type="java.lang.String">
                <column name="name" length="20"></column>
            </property>
            
            <property name="age" type="java.lang.Integer">
                <column name="age"></column>
            </property>
            
            <property name="tel" type="java.lang.String">
                <column name="tel" length="20"></column>
            </property>
            
            <property name="address" type="java.lang.String">
                <column name="address" length="50"></column>
            </property>
            
        </class>
        
    </hibernate-mapping>

    五、將配置文件添加到hibernate的cfg的配置文件中

    <!-- 加入實體類的映射文件 --> 
     <mapping  resource="edu/dzu/vo/UsersVo.hbm.xml"/>

    六、創建SessionFactory,SessionFactory是線程安全,所以整個SessionFactory應該基於單例的模式來創建
    Configuration cfg = new Configuration().configure();
    //在hibernate3中都是使用該種方法創建,但是在4中被禁用了
    //cfg.buildSessionFactory();
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
    .applySettings(cfg.getProperties()).buildServiceRegistry();
    SessionFactory factory = cfg.buildSessionFactory(serviceRegistry);

     創建session
    Session session = factory.openSession();

     通過session來進行各種操作

    我们的代码:

    HibernateSessionFactory.java:

    package edu.dzu.vo;
    
    import org.hibernate.*;
    import org.hibernate.cfg.Configuration;
    
    
    @SuppressWarnings("deprecation")
    public class HibernateSessionFactory {
    
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
        private static final ThreadLocal<Session> threadLocal = 
                new ThreadLocal<Session>();
        private static Configuration configuration = new Configuration();
        private static SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION;
        
        static {
            sessionFactory = configuration.configure(configFile).buildSessionFactory();
        }
        
        private HibernateSessionFactory(){
            
        }
        
        public static Session getSession(){
            Session session = threadLocal.get();
            if(session == null || !session.isOpen()){
                session = (sessionFactory != null ) ? sessionFactory.openSession():null;
                threadLocal.set(session);
            }
            return session;
        }
    }

    7.测试:

    TestHibernate.java

    package edu.dzu.vo;
    
    import java.awt.Font;
    
    import javax.swing.JOptionPane;
    
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    
    public class TestHibernate {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            
            Users users = new Users();
            users.setId(20121108);
            users.setName("张三");
            users.setAge(24);
            users.setTel("010-87654321");
            users.setAddress("上海黄浦");
            
            Users users2 = new Users();
            users2.setId(20121109);
            users2.setName("李四");
            users2.setAge(23);
            users2.setTel("010-12345678");
            users2.setAddress("北京朝阳");
            
            Session session = HibernateSessionFactory.getSession();
            Transaction trans = session.beginTransaction();
            session.persist(users);
            session.persist(users2);
            trans.commit();
            
            StringBuffer result = new StringBuffer();
            result.append("添加成功!往数据库中添加如下数据:
    ");
            result.append("编号"+"地址"+"姓名"+"年龄"+"联系电话	
    
    ");
            result.append(users.getId()+" ");
            result.append(users.getAddress()+" ");
            result.append(users.getName()+" ");
            result.append(users.getAge()+" ");
            result.append(users.getTel()+"	
    
    ");
            
            result.append(users2.getId()+" ");
            result.append(users2.getAddress()+" ");
            result.append(users2.getName()+" ");
            result.append(users2.getAge()+" ");
            result.append(users2.getTel()+"	
    
    ");
            
            session.close();
            
            JOptionPane.getRootFrame().setFont(new Font("Arial", Font.BOLD, 14));
            JOptionPane.showMessageDialog(null, result.toString());
            
        }
    
    }

    然后运行下TestHibernate.java,会发现数据库中多了几条记录,ok成功。

    以上文件目录结构:

    上面的例子注意参考了:http://www.cnblogs.com/ljmatlight/archive/2012/11/08/2760973.html

    http://www.cnblogs.com/ljmatlight/archive/2012/11/08/2760973.html

    http://rritw.com/a/JAVAbiancheng/Hibernate/20130403/334457.html

    http://blog.csdn.net/undoner/article/details/8958512

    http://www.cnblogs.com/tangyouwei/p/3171938.html

     http://book.51cto.com/art/200905/125850.htm

  • 相关阅读:
    7牛管理凭证生成错误
    安卓截屏如何实现将摄像头显示画面截下来
    realm怎样支持hashmap
    Cordova Android项目如何做代码混淆
    cnmp安装失败,报错npm ERR! enoent ENOENT: no such file or directory,
    iOS中关于字符 “&”的作用?
    float 保留两位小数
    关于iOS声音识别的框架
    iOS崩溃日志
    QT分析之WebKit
  • 原文地址:https://www.cnblogs.com/youxin/p/3522297.html
Copyright © 2011-2022 走看看