这是我的hibernate配置
<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
我在这里缺少什么?
答案 0 :(得分:0)
看起来你的hibernate annotations jar文件是不正确的版本。
您应该已将hibernate-core
作为依赖项。如果是这样,您可以在POM文件中注释掉hibernate-annotations
依赖项。
答案 1 :(得分: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>