开发者

ORM using Hibernate's annotations with Spring

I'm trying integrate spring with hibernate but catch exception on project start.

Caused by: org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.domain.Worker"/

My config: from spring

<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.M开发者_如何学PythonySQLDialect</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>

domain class

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;
    }
//...


If you want to use Hibernate API with annotated entities, you need to use AnnotationSessionFactoryBean instead of LocalSessionFactoryBean.


If you want to use JPA annotations then you should likely be using a LocalEntityManagerFactoryBean rather than a LocalSessionFactoryBean.

The former loads your annotations and entities through the regular JPA methods, while your configuration quoted above attempts to use the Hibernate SessionFactory directly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜