Spring和Hibernate配置

时间:2010-08-29 12:33:32

标签: hibernate spring

这是我的hibernate配置,但由于某种原因,它无法加载我添加到<property name="hibernateProperties">的那些属性,并且在启动时记录INFO org.hibernate.cfg.Environment - hibernate.properties not found

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.hibernate.dialect.PostgreSQLDialect" />
        <property name="url" value="jdbc:postgresql://localhost:5432/testdatabase" />
        <property name="username" value="postgres" />
        <property name="password" value="password" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan" value="org.test"/>
        <property name="hibernateProperties">
            <value>
                hbm2ddl.auto=create
                hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
                hibernate.show_sql=true             
            </value>
        </property>
    </bean>

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

</beans>

编辑:完整日志:

    2010-08-29 15:27:15,986 [main] INFO  org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.3.1.GA
    2010-08-29 15:27:15,997 [main] INFO  org.hibernate.cfg.Environment - Hibernate 3.2.6
    2010-08-29 15:27:16,000 [main] INFO  org.hibernate.cfg.Environment - hibernate.properties not found
    2010-08-29 15:27:16,002 [main] INFO  org.hibernate.cfg.Environment - Bytecode provider name : cglib
    2010-08-29 15:27:16,005 [main] INFO  org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
    2010-08-29 15:27:16,113 [main] INFO  org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: org.test.TestEntity
    2010-08-29 15:27:16,145 [main] INFO  org.hibernate.cfg.annotations.EntityBinder - Bind entity org.test.TestEntity on table TestEntity
    2010-08-29 15:27:16,188 [main] INFO  org.hibernate.cfg.AnnotationConfiguration - Hibernate Validator not found: ignoring
    2010-08-29 15:27:16,188 [main] INFO  org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean - Building new Hibernate SessionFactory
    2010-08-29 15:27:16,238 [main] INFO  org.hibernate.connection.ConnectionProviderFactory - Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
    2010-08-29 15:27:16,663 [main] INFO  org.hibernate.cfg.SettingsFactory - RDBMS: PostgreSQL, version: 9.0beta4
    2010-08-29 15:27:16,664 [main] INFO  org.hibernate.cfg.SettingsFactory - JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.3 JDBC3 with SSL (build 603)
    2010-08-29 15:27:16,667 [main] INFO  org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.PostgreSQLDialect
    2010-08-29 15:27:16,672 [main] INFO  org.hibernate.transaction.TransactionFactoryFactory - Transaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory
    2010-08-29 15:27:16,674 [main] INFO  org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
    2010-08-29 15:27:16,674 [main] INFO  org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
    2010-08-29 15:27:16,674 [main] INFO  org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
    2010-08-29 15:27:16,674 [main] INFO  org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
    2010-08-29 15:27:16,674 [main] INFO  org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
    2010-08-29 15:27:16,675 [main] INFO  org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
    2010-08-29 15:27:16,675 [main] INFO  org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): disabled
    2010-08-29 15:27:16,675 [main] INFO  org.hibernate.cfg.SettingsFactory - Connection release mode: auto
    2010-08-29 15:27:16,675 [main] INFO  org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
    2010-08-29 15:27:16,675 [main] INFO  org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
    2010-08-29 15:27:16,676 [main] INFO  org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
    2010-08-29 15:27:16,676 [main] INFO  org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
    2010-08-29 15:27:16,676 [main] INFO  org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
    2010-08-29 15:27:16,678 [main] INFO  org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
    2010-08-29 15:27:16,678 [main] INFO  org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
    2010-08-29 15:27:16,678 [main] INFO  org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
    2010-08-29 15:27:16,678 [main] INFO  org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
    2010-08-29 15:27:16,678 [main] INFO  org.hibernate.cfg.SettingsFactory - Query cache: disabled
    2010-08-29 15:27:16,678 [main] INFO  org.hibernate.cfg.SettingsFactory - Cache provider: org.hibernate.cache.NoCacheProvider
    2010-08-29 15:27:16,678 [main] INFO  org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
    2010-08-29 15:27:16,678 [main] INFO  org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
    2010-08-29 15:27:16,683 [main] INFO  org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
    2010-08-29 15:27:16,683 [main] INFO  org.hibernate.cfg.SettingsFactory - Statistics: disabled
    2010-08-29 15:27:16,683 [main] INFO  org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
    2010-08-29 15:27:16,684 [main] INFO  org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
    2010-08-29 15:27:16,684 [main] INFO  org.hibernate.cfg.SettingsFactory - Named query checking : enabled
    2010-08-29 15:27:16,703 [main] INFO  org.hibernate.impl.SessionFactoryImpl - building session factory
    2010-08-29 15:27:16,888 [main] INFO  org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
2010-08-29 15:27:17,096 [main] INFO  org.springframework.orm.hibernate3.HibernateTransactionManager - Using DataSource [org.apache.commons.dbcp.BasicDataSource@15b55bc] of Hibernate SessionFactory for HibernateTransactionManager

2 个答案:

答案 0 :(得分:5)

我认为hibernateProperties设置不正确(除非Spring有一种新的方法来设置我不知道的属性)。通常,属性设置如下:

    <property name="hibernateProperties">
        <props>
            <prop key="hbm2ddl.auto">create</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>

另一种方式(我更喜欢)只是将这些属性粘贴在hibernate.properties文件中,然后将该文件放在类路径的根目录中。这可能是错误消息所指的内容。

答案 1 :(得分:0)

你在driverClassName中设置方言这是错误的从postgre安装文件夹jdbc驱动程序中的lib文件获取jar