Hibernate在Spring MVC webapp

时间:2015-07-01 20:37:55

标签: java spring hibernate jdbc

我想知道是否有人可以向我解释为什么Spring和Hibernate webapp在两个环境中运行良好但在另一个环境中运行失败?我将NetBeans 8.0.XTomcat 8.0.3.0Apache Derby 10.X一起使用。

我的申请dispatcherservlet如下:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc" 
   xmlns:tx="http://www.springframework.org/schema/tx"       
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
                       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
                       http://www.springframework.org/schema/context 
                       http://www.springframework.org/schema/context/spring-context-4.0.xsd 
                       http://www.springframework.org/schema/mvc                            
                       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd                           
                       http://www.springframework.org/schema/tx    
                       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!-- Uses annotations in classes for JavaBeans. XML is an alternative. -->
<mvc:annotation-driven />   

<!-- Base package. -->
<context:component-scan base-package="library" />    

<!-- Model. -->
<bean id="person" class="library.model.Person" />
<bean id="book" class="library.model.Book" />        

<!-- Spring Controllers. -->
<bean id="adminController" class="library.controller.admin.AdminController" />    
<bean id="personController" class="library.controller.PersonController" />      
<bean id="bookController" class="library.controller.BookController" /> 
<bean id="exceptionController" class="library.controller.ExceptionController" />     

<!-- Spring Interceptors. -->
<mvc:interceptors>
    <bean id="clientInterceptor" class="library.interceptor.ClientInterceptor" />
</mvc:interceptors>

<!-- Spring Services. -->
<bean id="personServiceImpl" class="library.service.PersonServiceImpl" />    
<bean id="bookServiceImpl" class="library.service.BookServiceImpl" />      

<!-- Spring Repositories. -->
<bean id="personDAOImpl" class="library.dao.PersonDAOImpl" />    
<bean id="bookDAOImpl" class="library.dao.BookDAOImpl" />       

<!-- Spring Validators. -->
<bean id="personValidator" class="library.validator.PersonValidator" />    
<bean id="bookValidator" class="library.validator.BookValidator" />     

<!-- Spring ViewResolver. -->               
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
         <value>/WEB-INF/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>   

<!-- Spring MesssageSource. -->         
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename"> 
        <value>/WEB-INF/classes/messages</value>    
    </property>    
</bean>

<!-- Spring Properties file for Library. -->      
<bean id="propertiesFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location">  
         <value>classpath:library.properties</value>             
    </property>
</bean>      

<!-- Hibernate DataSource. -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
    <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />        
    <!--property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" /-->
    <property name="url" value="jdbc:derby://localhost:1527/Library" />
    <property name="username" value="username" />
    <property name="password" value="password" />            
</bean>   

<!-- Hibernate Interceptors. -->
<bean id="serverInterceptor" class="library.interceptor.ServerInterceptor" />

<!-- Hibernate SessionFactory. -->    
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">    
    <property name="dataSource" ref="dataSource"></property>                  
    <property name="hibernateProperties">
        <props>    
           <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>                     
           <!--prop key="hibernate.dialect">org.hibernate.dialect.DerbyTenSixDialect</prop-->
           <prop key="hibernate.show_sql">false</prop>               

           <!-- What to do with the database schema. -->
           <prop key="hbm2ddl.auto">validate</prop>    
           <!-- validate:    validate the schema, makes no changes to the database.
                update:      update the schema.
                create:      creates the schema, destroying previous data.
                create-drop: drop the schema at the end of the session. -->                 
        </props>            
    </property>                                                                                                                                
    <property name="entityInterceptor">
        <ref bean="serverInterceptor" />            
    </property>                                                                                                                                             
    <property name="packagesToScan">
        <list>
            <value>library.model</value>                
        </list>
    </property>          
</bean>

<!-- Hibernate TransactionManagment. -->
<tx:annotation-driven />                
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
</bean>  

当应用程序在一个环境中构建和失败时给出的错误是:

WARN|01 07 2015|16 26 31|http-nio-8080-exec-73|org.hibernate.engine.jdbc.internal.JdbcServicesImpl| - HHH000342: Could not obtain connection to query metadata : No suitable driver found for jdbc:derby://localhost:1527/Library
INFO|01 07 2015|16 26 31|http-nio-8080-exec-73|org.hibernate.dialect.Dialect| - HHH000400: Using dialect: org.hibernate.dialect.DerbyTenSixDialect
INFO|01 07 2015|16 26 34|http-nio-8080-exec-73|org.hibernate.engine.jdbc.internal.LobCreatorBuilder| - HHH000422: Disabling contextual LOB creation as connection was null
ERROR|01 07 2015|16 26 41|http-nio-8080-exec-73|org.springframework.web.context.ContextLoader| - Context initialization failed

然后导致依赖关系的连接失败。

这些消息是什么意思?

应用程序正在使用Spring 4.0.2 hibernate-core-4.3.10.jar。三个环境之间的所有依赖关系都是相同的,其中包含以下列表:

30 Jun 2015  20 26           445,288 antlr-2.7.7.jar
30 Jun 2015  20 26             4,467 aopalliance-1.0.jar
30 Jun 2015  20 26           160,519 commons-dbcp-1.4.jar
30 Jun 2015  20 26            62,050 commons-logging-1.1.3.jar
30 Jun 2015  20 26         2,834,700 derby.jar
30 Jun 2015  20 26           582,639 derbyclient.jar
30 Jun 2015  20 26           313,898 dom4j-1.6.1.jar
30 Jun 2015  20 26            75,311 hibernate-commons-annotations-4.0.4.Final.jar
30 Jun 2015  20 26         5,280,098 hibernate-core-4.3.10.Final.jar
30 Jun 2015  20 27           113,371 hibernate-jpa-2.1-api-1.0.0.Final.jar
30 Jun 2015  20 26            38,605 jackson-annotations-2.4.0.jar
30 Jun 2015  20 26           225,306 jackson-core-2.4.1.jar
30 Jun 2015  20 27           228,552 jackson-core-asl-1.9.7.jar
30 Jun 2015  20 26         1,074,275 jackson-databind-2.4.1.jar
30 Jun 2015  20 26           786,084 jackson-mapper-lgpl-1.9.13.jar
30 Jun 2015  20 26            76,551 jandex-1.1.0.Final.jar
30 Jun 2015  20 26           714,194 javassist-3.18.1-GA.jar
30 Jun 2015  20 26           162,126 javax.persistence-2.1.0.jar
30 Jun 2015  20 26            57,183 jboss-logging-3.1.3.GA.jar
30 Jun 2015  20 26            11,558 jboss-logging-annotations-1.2.0.Beta1.jar
30 Jun 2015  20 27            27,717 jboss-transaction-api_1.2_spec-1.0.0.Final.jar
30 Jun 2015  20 26            20,682 jstl-1.1.2.jar
30 Jun 2015  20 26            15,071 jta-1.1.jar
30 Jun 2015  20 26           367,444 log4j-1.2.14.jar
30 Jun 2015  20 27            52,150 persistence-api-1.0.jar
30 Jun 2015  20 27            36,364 spring-annotation-base-1.0.2.jar
30 Jun 2015  20 26           352,730 spring-aop-4.0.2.RELEASE.jar
30 Jun 2015  20 26           669,044 spring-beans-4.0.2.RELEASE.jar
30 Jun 2015  20 26           974,272 spring-context-4.0.2.RELEASE.jar
30 Jun 2015  20 26           960,994 spring-core-4.0.2.RELEASE.jar
30 Jun 2015  20 26           204,780 spring-expression-4.0.2.RELEASE.jar
30 Jun 2015  20 26           419,614 spring-jdbc-4.0.2.RELEASE.jar
30 Jun 2015  20 26           366,844 spring-orm-4.0.2.RELEASE.jar
30 Jun 2015  20 26           248,204 spring-tx-4.0.2.RELEASE.jar
30 Jun 2015  20 26           665,015 spring-web-4.0.2.RELEASE.jar
30 Jun 2015  20 26           660,329 spring-webmvc-4.0.2.RELEASE.jar
30 Jun 2015  20 26           393,259 standard-1.1.2.jar

2 个答案:

答案 0 :(得分:0)

看起来你的lib中没有derbyclient.jar,所以请在你的库中添加这个jar。从here

下载

答案 1 :(得分:0)

我认为这是关于你的连接字符串。你能这样试试吗?

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
      property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
    <property name="url" value="jdbc:derby:~/Library" />
    <property name="username" value="username" />
    <property name="password" value="password" />            
</bean>

这将在$ USER_HOME上创建一个名为Library的数据库。

更新

默认德比不支持jdbc:derby://localhost/Libarary之类的连接字符串。要通过网络连接德比服务器,请将derbynet.jar添加到类路径中。你可以找到additional info here

然后您可以使用默认数据源定义。

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
    <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />        
    <property name="url" value="jdbc:derby://localhost:1527/Library" />
    <property name="username" value="username" />
    <property name="password" value="password" />            
</bean>