Hibernate在内存数据库中创建多个

时间:2015-02-17 18:30:21

标签: java spring hibernate tomcat jpa

我有基于Spring / hibernate(带缓存)的应用程序,它在内存数据库中用于测试。我有两个文件,当应用程序启动schema.sql(schema)& test-data.sql(示例数据)。但是,hibernate在内存实例中创建了自己的内容,之前添加的内容不存在。它会丢弃原始数据库或创建第二个实例并忽略spring创建的实例。

小块日志,它显示它创建数据库连接,稍后创建第二个,再一次创建(在日志之外):

2015-02-17 18:1948 DEBUG DataSourceUtils:110 - Fetching JDBC Connection from DataSource
**2015-02-17 18:1948 DEBUG SimpleDriverDataSource:138 - Creating new JDBC Driver Connection to [jdbc:h2:mem:dataSource;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false]**
2015-02-17 18:1948 INFO  ScriptUtils:432 - Executing SQL script from class path resource [dbScripts/schema.sql]
2015-02-17 18:1948 DEBUG ScriptUtils:463 - 0 returned as updateCount for SQL: CREATE table players ( id BIGINT not null, version INTEGER not null, playerid VARCHAR(225) not null, name VARCHAR(225) not null, PRIMARY KEY (id) )
2015-02-17 18:1948 INFO  ScriptUtils:491 - Executed SQL script from class path resource [dbScripts/schema.sql] in 13 ms.
2015-02-17 18:1948 INFO  ScriptUtils:432 - Executing SQL script from class path resource [dbScripts/test-data.sql]
2015-02-17 18:1948 DEBUG ScriptUtils:463 - 1 returned as updateCount for SQL: INSERT INTO players (id, version, playerid, name) VALUES (1, 1, 'bobstone', 'Bob Stone')
2015-02-17 18:1948 DEBUG ScriptUtils:463 - 1 returned as updateCount for SQL: INSERT INTO players (id, version, playerid, name) VALUES (2, 1, 'billbrown', 'Bill Brown')
2015-02-17 18:1948 DEBUG ScriptUtils:463 - 1 returned as updateCount for SQL: INSERT INTO players (id, version, playerid, name) VALUES (3, 1, 'andrewfox', 'Andrew Fox')
2015-02-17 18:1948 DEBUG ScriptUtils:463 - 1 returned as updateCount for SQL: INSERT INTO players (id, version, playerid, name) VALUES (4, 1, 'paulprice', 'Paul Price')
2015-02-17 18:1948 DEBUG ScriptUtils:463 - 1 returned as updateCount for SQL: INSERT INTO players (id, version, playerid, name) VALUES (5, 1, 'annatimes', 'Anna Times')
2015-02-17 18:1948 DEBUG ScriptUtils:463 - 1 returned as updateCount for SQL: INSERT INTO players (id, version, playerid, name) VALUES (6, 1, 'jeffgray', 'Jeff Gray')
2015-02-17 18:1948 DEBUG ScriptUtils:463 - 1 returned as updateCount for SQL: INSERT INTO players (id, version, playerid, name) VALUES (7, 1, 'peterblack', 'Peter Black')
2015-02-17 18:1948 DEBUG ScriptUtils:463 - 1 returned as updateCount for SQL: INSERT INTO players (id, version, playerid, name) VALUES (8, 1, 'colinstewart', 'Colin Stewart')
2015-02-17 18:1948 INFO  ScriptUtils:491 - Executed SQL script from class path resource [dbScripts/test-data.sql] in 7 ms.
2015-02-17 18:1948 DEBUG DataSourceUtils:327 - Returning JDBC Connection to DataSource
2015-02-17 18:1948 DEBUG DefaultListableBeanFactory:248 - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2015-02-17 18:1948 DEBUG DefaultListableBeanFactory:478 - Finished creating instance of bean 'dataSource'
2015-02-17 18:1948 DEBUG DefaultListableBeanFactory:248 - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2015-02-17 18:1948 DEBUG DefaultListableBeanFactory:450 - Creating instance of bean 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#c0d1f74'
2015-02-17 18:1948 DEBUG DefaultListableBeanFactory:248 - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2015-02-17 18:1948 DEBUG DefaultListableBeanFactory:478 - Finished creating instance of bean 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#c0d1f74'
2015-02-17 18:1948 DEBUG DefaultListableBeanFactory:1608 - Invoking afterPropertiesSet() on bean with name 'emf'
2015-02-17 18:1948 DEBUG PathMatchingResourcePatternResolver:536 - Looking for matching resources in jar file [file:/C:/Java/STS/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/players-web/WEB-INF/lib/players-dao-0.0.1-SNAPSHOT.jar]
2015-02-17 18:1948 DEBUG PathMatchingResourcePatternResolver:423 - Resolved location pattern [classpath*:com/test/players/domain/**/*.class] to resources [URL [jar:file:/C:/Java/STS/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/players-web/WEB-INF/lib/players-dao-0.0.1-SNAPSHOT.jar!/com/test/players/domain/Player.class]]
2015-02-17 18:1948 INFO  LocalContainerEntityManagerFactoryBean:339 - Building JPA container EntityManagerFactory for persistence unit 'default'
Feb 17, 2015 6:19:48 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
Feb 17, 2015 6:19:49 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.8.Final}
Feb 17, 2015 6:19:49 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 17, 2015 6:19:49 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Feb 17, 2015 6:19:49 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
**2015-02-17 18:1949 DEBUG SimpleDriverDataSource:138 - Creating new JDBC Driver Connection to [jdbc:h2:mem:dataSource;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false]**
Feb 17, 2015 6:19:49 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Feb 17, 2015 6:19:49 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory

APP-context.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"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    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/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/data/jpa 
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee.xsd">

    <import resource="classpath:spring/hibernate/hibernate-context.xml"/>

    <tx:annotation-driven transaction-manager="transactionManager" />   

    <context:annotation-config/>

    <beans profile="prod">
        <jee:jndi-lookup expected-type="javax.sql.DataSource"
        id="dataSource" jndi-name="java:comp/env/jdbc/players_db"/>
    </beans>

    <beans profile="test">
        <jdbc:embedded-database id="dataSource" type="H2">
            <jdbc:script location="classpath:dbScripts/schema.sql"/>
            <jdbc:script location="classpath:dbScripts/test-data.sql"/>
        </jdbc:embedded-database>
    </beans>  

</beans>

冬眠-context.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"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/data/jpa 
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.test.players.service.jpa"/>  

    <jpa:repositories base-package="com.test.players.repository"
                      entity-manager-factory-ref="emf"
                      transaction-manager-ref="transactionManager"/>

    <beans profile="hibernate">

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

        <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
            </property>        
            <property name="packagesToScan" value="com.test.players.domain"/>
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.dialect">
                        org.hibernate.dialect.H2Dialect
                    </prop>
                    <prop key="hibernate.max_fetch_depth">
                        3
                    </prop>
                    <prop key="hibernate.jdbc.fetch_size">
                        50
                    </prop>
                    <prop key="hibernate.jdbc.batch_size">
                        10
                    </prop>
                    <prop key="hibernate.show_sql">
                        true
                    </prop> 
                    <prop key="hibernate.hbm2ddl.auto">
                        create
                    </prop>
                    <prop key="hibernate.cache.use_second_level_cache">
                        true
                    </prop>
                    <prop key="hibernate.cache.provider_class">
                        org.hibernate.cache.EhCacheProvider
                    </prop>
                    <prop key="hibernate.cache.region.factory_class">
                        org.hibernate.cache.ehcache.EhCacheRegionFactory
                    </prop>
                    <prop key="hibernate.cache.use_query_cache">
                        true    


          </prop>
                 <prop key="net.sf.ehcache.configurationResourceName">
                    /spring/hibernate/hibernate-cache.xml
                 </prop>
            </props>        
        </property>
    </bean>    

</beans>

冬眠-cache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" 
    updateCheck="true"
    monitoring="autodetect" 
    dynamicConfig="true">

    <diskStore path="java.io.tmpdir\ehcache" />

    <defaultCache 
        maxEntriesLocalHeap="1000" 
        eternal="false"
        timeToIdleSeconds="120" 
        timeToLiveSeconds="120" 
        diskSpoolBufferSizeMB="30"
        maxEntriesLocalDisk="10000" 
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU" 
        statistics="true">
        <persistence strategy="localTempSwap" />
    </defaultCache>

    <cache 
        name="players" 
        maxEntriesLocalHeap="1000" 
        eternal="false"
        timeToIdleSeconds="5" 
        timeToLiveSeconds="10">
        <persistence strategy="localTempSwap" />
    </cache>

    <cache 
        name="org.hibernate.cache.internal.StandardQueryCache"
        maxEntriesLocalHeap="5" 
        eternal="false" 
        timeToLiveSeconds="120">
        <persistence strategy="localTempSwap" />
    </cache>

    <cache 
        name="org.hibernate.cache.spi.UpdateTimestampsCache"
        maxEntriesLocalHeap="5000" 
        eternal="true">
        <persistence strategy="localTempSwap" />
    </cache>
</ehcache>

和显示问题的日志

1 个答案:

答案 0 :(得分:2)

你要求Spring创建并填充数据库,你还要告诉Hibernate创建模式:

<prop key="hibernate.hbm2ddl.auto">create</prop>

这并不合理。 尝试更改以验证:

<prop key="hibernate.hbm2ddl.auto">validate</prop>