我正在尝试将spring与hibernate集成,但在项目启动时捕获异常。
Caused by: org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.domain.Worker"/
我的配置: 从春天开始
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:mysql://localhost/school"/>
<property name="username" value="root"/>
<property name="password" value="toor"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="configLocation" value="/WEB-INF/hib.cfg.xml"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
hib.cfg.xml
<hibernate-configuration>
<session-factory name="java:hibernate/SessionFactory">
<property name="connection.datasource">java:/comp/env/jdbc/MyDB</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">false</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<property name="configClass">org.hibernate.cfg.AnnotationConfiguration</property>
<mapping class="com.domain.Worker"/>
</session-factory>
</hibernate-configuration>
域类
package com.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
@Entity
public class Worker extends DomainObject {
@Column(nullable = false, length = 20)
public String getFirstName() {
return firstName;
}
//...
答案 0 :(得分:4)
如果要将Hibernate API与带注释的实体一起使用,则需要使用AnnotationSessionFactoryBean
而不是LocalSessionFactoryBean
。
答案 1 :(得分:3)
如果您想使用JPA注释,那么您应该using a LocalEntityManagerFactoryBean
而不是LocalSessionFactoryBean
。
前者通过常规JPA方法加载注释和实体,而上面引用的配置尝试直接使用Hibernate SessionFactory。