spring4_Struts2_Hibernate4开发bug日志

BUG1:获取hibernate的session各种异常

在测试中出现bug

public class Test2 {
    @Test
    public void sessionTest(){
        Session session = HibernateSessionFactory.getSession();
        System.out.println(session);
    }

}

bug信息

现将dom4j-1.6.1.jar拷贝至lib,运行,出现以下bug:

先将jboss-logging-3.1.3.GA.jar,jboss-logging-annotations-1.2.0.Beta1.jar两个包拷贝至lib,出现以下bug:

拷贝jta.jar至lib(在status_hibernate项目中为何不需要此jar?),出现以下异常:

在http://www.cnblogs.com/frank-ming/archive/2013/04/20/3033073.html这位网友的提示下,发现是由hibernate.cfg.xml与News.hbm.xml表头相同引起的:

下面截取一张正确表头的配置与原来我错误的配置进行比较:

正确的表头配置:

我原来错误的表头配置:

可以看出:在正确的表头配置中,hibernate.cfg.xml与News.hbm.xml是不同的。前者是:

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

后者是:

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

将News.hbm.xml修改成正确的表头,就可以了。

现在,在测试类中的sessionTest方法中,可以获取session了。

打印信息:

SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];...

 

小结:出现以上异常信息的原因有2方面造成:1是缺少jar,2是hibernate的配置文件中表头配置错误。其中漏掉的jar总结如下:

dom4j-1.6.1.jar

jboss-logging-3.1.3.GA.jar,jboss-logging-annotations-1.2.0.Beta1.jar

jta.jar

 

BUG2:查询News信息列表时,报以下异常:

将antlr-2.7.7.jar加上,好了。

小结:出现以上异常原因在于遗漏一个jar:antlr-2.7.7.jar

 

将spring与hibernate整合后


 

BUG3:将spring与hibernate整合后,获取hibernateTemplate出现以下异常:

1.spring.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!-- 引入外部文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

 <!-- c3p0数据源 -->  
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
     <!-- 驱动程序 -->  
     <property name="driverClass">  
         <value>${driverName}</value>  
     </property>  

     <!-- 连接的url地址 -->  
     <property name="jdbcUrl">  
         <value>${url}</value>  
     </property>  

     <!-- 连接的用户名 -->  
     <property name="user">  
         <value>${userName}</value>  
     </property>  

     <!-- 连接的密码 -->  
     <property name="password">  
         <value>${passWord}</value>  
     </property>  

     <!-- 最大池数 -->  
     <property name="maxPoolSize">  
         <value>40</value>  
     </property>  

     <!-- 最小池数 -->  
     <property name="minPoolSize">  
         <value>10</value>  
     </property>  
     
     <!-- 默认初始化的池数 -->  
     <property name="initialPoolSize">  
         <value>10</value>  
     </property>  
 </bean> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
           <prop key="hibernate.show_sql">true</prop>
           <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
    <property name="mappingResources">
        <list>
           <value>com/chen/vo/News.hbm.xml</value>
        </list>
    </property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">  
    <property name="sessionFactory" ref="sessionFactory" />  
</bean>
    
<!-- 开启注解模式 -->
<context:annotation-config/>
<!-- 扫包(使用“注解模式”需要此配置)。服务器在启动时会扫描base-package所指定的包,并将相应的bean注入ApplicationContext容器。 -->
<context:component-scan base-package="com.chen"></context:component-scan>

</beans>

2.测试类测试:

package com.chen.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml") 
public class Spring_HibernateTest {
    
    @Test
    public void getHibernateTemplate(){
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:springTest.xml"); 
        System.out.println(applicationContext.getBean("hibernateTemplate"));
    }

}

3.异常信息:

修改加载地址后--ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:spring.xml"); 异常依旧。

查看hibernate包后得知,在hibernate4中的核心包中没有org.hibernate.cache.CacheProvider这个类,而在hibernate3中有。

现在将hibernate4换成hibernate3

删除hibernate4相关的jar:

加上hibernate3jar:

但因为hibernate3的核心包中又缺少HibernateSessionFactory类中的import org.hibernate.service.ServiceRegistry;所以删除HibernateSessionFactory。

运行getHibernateTemplate方法,报以下异常:

加上slf4j-api-1.5.8.jar,slf4j-log4j12-1.5.8.jar,运行,出现以下异常:

加上commons-collections-3.1.jar,运行,正常!

打印信息:

org.springframework.orm.hibernate3.HibernateTemplate@e79f7a

小结:A.出现以上异常的原因在于:1.hibernate4不适合与spring4.0整合,这是因为hibernate4的核心包中缺少相应的类(如CacheProvider),换成hibernate3即可。;2.缺少jar,其中遗漏的jar如下:

slf4j-api-1.5.8.jar,slf4j-log4j12-1.5.8.jar

commons-collections-3.1.jar

B.使用hibernate3后,因为hibernate3缺少相应jar(如ServiceRegistry),所以不能通过ServiceRegistry获取SessionFactory了。--实际上也没必要,因为现在无需通过自己创建的HibernateSessionFactory获取Session了,直接通过spring获取hibernateTemplate即可

 

BUG4:将spring与hibernate整合后,运行项目出现以下异常:

spring.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!-- 引入外部文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

 <!-- c3p0数据源 -->  
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
     <!-- 驱动程序 -->  
     <property name="driverClass">  
         <value>${driverName}</value>  
     </property>  

     <!-- 连接的url地址 -->  
     <property name="jdbcUrl">  
         <value>${url}</value>  
     </property>  

     <!-- 连接的用户名 -->  
     <property name="user">  
         <value>${userName}</value>  
     </property>  

     <!-- 连接的密码 -->  
     <property name="password">  
         <value>${passWord}</value>  
     </property>  

     <!-- 最大池数 -->  
     <property name="maxPoolSize">  
         <value>40</value>  
     </property>  

     <!-- 最小池数 -->  
     <property name="minPoolSize">  
         <value>10</value>  
     </property>  
     
     <!-- 默认初始化的池数 -->  
     <property name="initialPoolSize">  
         <value>10</value>  
     </property>  
 </bean> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
           <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
           <prop key="hibernate.show_sql">true</prop>
           <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
    <property name="mappingResources">
        <list>
           <value>com/chen/vo/News.hbm.xml</value>
        </list>
    </property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">  
    <property name="sessionFactory" ref="sessionFactory" />  
</bean>

<bean id="NewsDaoImpl" class="com.chen.dao.NewsDaoImpl">
    <property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
    
<!-- 开启注解模式 -->
<context:annotation-config/>
<!-- 扫包(使用“注解模式”需要此配置)。服务器在启动时会扫描base-package所指定的包,并将相应的bean注入ApplicationContext容器。 -->
<context:component-scan base-package="com.chen"></context:component-scan>

</beans>

配置log4j后,打印异常为userName和passWord无效(java.sql.SQLException: ORA-01017: invalid username/password; logon denied),所以现在将它们改为soctt、root,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!-- 引入外部文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
     <property name="driverClass">  
         <value>${driverName}</value>  
     </property>   
     <property name="jdbcUrl">  
         <value>${url}</value>  
     </property>  
     <property name="user">  
         <value>scott</value>  
     </property>  
     <property name="password">  
         <value>root</value>  
     </property>  
     <property name="maxPoolSize">  
         <value>40</value>  
     </property>  
     <property name="minPoolSize">  
         <value>10</value>  
     </property>   
     <property name="initialPoolSize">  
         <value>10</value>  
     </property>  
</bean> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
           <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
           <prop key="hibernate.show_sql">true</prop>
           <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
    <property name="mappingResources">
        <list>
           <value>com/chen/vo/News.hbm.xml</value>
        </list>
    </property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">  
    <property name="sessionFactory" ref="sessionFactory" />  
</bean>

<bean id="newsDaoImpl" class="com.chen.dao.NewsDaoImpl">
    <property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean> 
    
<!-- 开启注解模式 -->
<context:annotation-config/>
<!-- 扫包(使用“注解模式”需要此配置)。服务器在启动时会扫描base-package所指定的包,并将相应的bean注入ApplicationContext容器。 -->
<context:component-scan base-package="com.chen"></context:component-scan>

</beans>

可以成功启动服务器了!(其中原因未可知..)

 

BUG4.1:此bug源自于BUG4:在spring.xml中无法读取jdbc.properties中的${userName}${passWord},会报异常:

java.sql.SQLException: ORA-01017: invalid username/password; logon denied

现在,将jdbc.propertiesuserNamepassWord改一下名字,如下图所示:

driverName=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:myorcl
name=scott
pwd=root

在spring.xml中用EL表达式获取:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
     <property name="driverClass">  
         <value>${driverName}</value>  
     </property>   
     <property name="jdbcUrl">  
         <value>${url}</value>  
     </property>  
     <property name="user">  
         <value>${name}</value>  
     </property>  
     <property name="password">  
         <value>${pwd}</value>  
     </property>  
     <property name="maxPoolSize">  
         <value>40</value>  
     </property>  
     <property name="minPoolSize">  
         <value>10</value>  
     </property>   
     <property name="initialPoolSize">  
         <value>10</value>  
     </property>  
</bean> 

启动服务器,正常!

小结:推测应该是原来起的名字涉及到关键字或者跟其它什么重名,所以导致其在读取时无效(invalid )。

 

 

 


 

附:HibernateSessionFactory

package com.oa.utils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateSessionFactory {
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    //private static String configfile = "/hibernate.cfg.xml";
    private static Configuration configuration = new Configuration().configure();
    private static SessionFactory sessionFactory;

    static {
        try {
            StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();  
            ServiceRegistry serviceRegistry = serviceRegistryBuilder.applySettings(configuration.getProperties()).build(); 
            sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
        } catch (HibernateException e) {
            e.printStackTrace();
        }
    }

    private HibernateSessionFactory() {
    }

    public static Session getSession() {
        Session session = (Session) threadLocal.get();

        if (session == null || !session.isOpen()) {
            session = (sessionFactory != null) ? sessionFactory.openSession()
                    : null;
            threadLocal.set(session);
        }

        return session;
    }

    public static void closeSesssion() {
        Session session = threadLocal.get();
        threadLocal.set(null);
        if (session != null) {
            session.close();
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void setSessionFactory(SessionFactory sessionFactory) {
        HibernateSessionFactory.sessionFactory = sessionFactory;
    }

    public static Configuration getConfiguration() {
        return configuration;
    }

    public static void setConfiguration(Configuration configuration) {
        HibernateSessionFactory.configuration = configuration;
    }
}

 

posted @ 2015-10-05 14:53  Tom1997  阅读(269)  评论(0编辑  收藏  举报