0

这是我的休眠配置

 <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.h2.Driver"/>
        <property name="url" value="jdbc:h2:mem:/localhost/testDB"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="datasource" />
        <property name="packagesToScan" value="sample.model"/>
        <property name="annotatedPackages">
            <list>
                <value>sample.model</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect"> org.hibernate.dialect.H2Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

我包括了这个依赖

 <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
</dependency>

这是例外

Caused by: java.lang.IllegalStateException: Expected method not found: java.lang.NoSuchMethodException: org.hibernate.cfg.Configuration.addAnnotatedClass(java.lang.Class)
    at org.springframework.util.ClassUtils.getMethod(ClassUtils.java:627)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.<clinit>(LocalSessionFactoryBuilder.java:79)
    ... 39 more

我在这里想念什么?

4

2 回答 2

0

看起来您的休眠注释 jar 文件的版本不正确。

你应该已经有了hibernate-core依赖。hibernate-annotations如果是这样,您可以在 POM 文件中注释掉您的依赖项。

于 2012-08-20T21:00:59.430 回答
0

解决方案是使用hibernate 3并添加这个

<prop key="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</prop>

所以最终的配置是

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="datasource" />
        <property name="packagesToScan" value="sample.model"/>
        <property name="annotatedPackages">
            <list>
                <value>sample.model</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect"> org.hibernate.dialect.H2Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                   <prop key="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</prop>
               </props>
        </property>
    </bean>
于 2012-08-20T22:02:13.227 回答