2013-11-15 80 views
1

以及我需要幾乎同樣的事情,在這個問題How do you translate Hibernate class mappings to a Spring application context?Hibernate配置

,但我不應該使用註釋,我需要保存XML映射,所以我應該如何指定春天映射配置?對於可能重複

PS抱歉,但我只看到了基於註解的建議

我的當前配置與註釋: 的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 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> 

回答

1

設置你的LocalSessionFactoryBeanmappingLocations屬性:

<property name="mappingLocations"> 
    <list> 
     <value>classpath:/path/to/mapping.hbm.xml</value> 
     ... 
    </list> 
</property> 
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> 

然後添加此文件到你的休眠配置文件ñ

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