Spring MVC + SpringJPA - 运行时错误

时间:2015-03-13 21:58:58

标签: java spring jpa datasource

我遇到Spring JAP应用程序的运行时问题。我已经定义了数据源。错误消息如下:

Info:   [EL Info]: 2015-03-13 21:31:40.932--ServerSession(1884324141)--EclipseLink, version: Eclipse Persistence Services - 2.5.0.v20130507-3faac2b
Info:   [EL Info]: connection: 2015-03-13 21:31:41.339--ServerSession(1884324141)--file:/C:/Users/smoczyna/glassfish4/glassfish/domains/domain1/lib/classes/_default login successful
Info:   [EL Warning]: metamodel: 2015-03-13 21:31:41.371--The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units.  Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element
Info:   [EL Warning]: metamodel: 2015-03-13 21:31:41.809--The collection of metamodel [ManagedType] types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units.  Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.  The lookup on [class org.squadd.sampledb.entities.Customer] will return null.
Info:   [EL Info]: connection: 2015-03-13 21:31:41.825--ServerSession(1884324141)--file:/C:/Users/smoczyna/glassfish4/glassfish/domains/domain1/lib/classes/_default logout successful
Severe:   Startup of context /SampleDb-WEB failed due to previous errors

问题是我根本没有使用Persistence.xml,我用applicationContext.xml这样定义了数据源:

...
<context:property-placeholder location="classpath:/META-INF/sample-db.properties"/>

    <bean class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" id="dataSource">
        <property name="driverClassName" value="${database.driverClassName}"/>
        <property name="url" value="${database.url}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
        <property name="testWhileIdle" value="true"/>
        <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
        <property name="numTestsPerEvictionRun" value="3"/>
        <property name="minEvictableIdleTimeMillis" value="1800000"/>
    </bean>
...

1 个答案:

答案 0 :(得分:0)

这是完整的配置:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       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.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/util
                           http://www.springframework.org/schema/util/spring-util.xsd
                           http://www.springframework.org/schema/data/jpa
                           http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <tx:annotation-driven/>

    <context:property-placeholder location="classpath:/META-INF/sample-db.properties"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
        <property name="driverClassName" value="${database.driverClassName}"/>
        <property name="url" value="${database.url}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>        
    </bean>

    <context:component-scan base-package="org.squadd.sampledb"/>

    <!-- NOTE: Do NOT use the "PersistenceUnit" property, it will override the "PackagesToScan" -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="org.squadd.sampledb.repositories"/>

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
                <!-- <property name="generateDdl" value="true"/> -->
                <property name="database" value="DERBY"/>
            </bean>
        </property>

        <property name="jpaPropertyMap">
            <map>                
                <entry value="static">
                    <key>
                        <util:constant static-field="org.eclipse.persistence.config.PersistenceUnitProperties.WEAVING"/>
                    </key>
                </entry>
            </map>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <jpa:repositories base-package="org.squadd.sampledb.repositories"/>
</beans>