在spring配置中使用映射类的hibernate配置

时间:2013-11-15 11:10:01

标签: spring hibernate

我需要和这个问题How do you translate Hibernate class mappings to a Spring application context?

几乎相同的东西

但我不应该使用注释,我需要保存xml映射,那么我应该如何在spring配置中指定映射?

P上。 S.对不起可能重复,但我只看到基于注释的建议

带注释的当前配置: 的hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521/XE</property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.connection.password">pass</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <property name="show_sql">true</property>
        <mapping class="com.foo.domain.News"></mapping>
    </session-factory>
</hibernate-configuration>

applicationContext.xml sessionFactory bean:

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>/WEB-INF/hibernate.cfg.xml</value>
        </property>

        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>


        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.dialect">${DIALECT}</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>

            </props>
        </property>
    </bean>

2 个答案:

答案 0 :(得分:2)

hibernate文档非常好。

这里有一些简单的例子: Hibernate documentation

您必须创建映射xml,如:

Person.hbm.xml  (which maps Person.java)

<class name="Person" table="PERSON">
    <id name="id" column="PERSON_ID">
        <generator class="native"/>
    </id>
    <property name="age"/>
    <property name="firstname"/>
    <property name="lastname"/>
</class>

然后将此文件添加到您的hibernate配置

<mapping resource="Person.hbm.xml"/>

答案 1 :(得分:1)

mappingLocations上设置LocalSessionFactoryBean属性:

<property name="mappingLocations">
    <list>
        <value>classpath:/path/to/mapping.hbm.xml</value>
        ...
    </list>
</property>