2011-11-24 56 views
0

使用Joda時間與hibernate支持配對。配置如下:在休眠初始化期間Joda類型def加載失敗初始化

有類型定義在org.joda.time.package-info.java

@org.hibernate.annotations.TypeDefs({ 
@org.hibernate.annotations.TypeDef(
         name="localDate", 
         typeClass = 
org.joda.time.contrib.hibernate.PersistentLocalDate.class 
     ) 
}) 
package org.joda.time; 

有與會話工廠配置彈簧背景:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="packagesToScan"> 
      <list> 
       <value>org.joda.time</value> 
      </list> 
     </property> 
     <property name="annotatedClasses"> 
      <list> 
       <value>...</value> 
... 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
       <prop key="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</prop> 
       <prop key="hibernate.show_sql">false</prop> 
       <prop key="hibernate.format_sql">true</prop> 
      </props> 
     </property> 
     <property name="dataSource" ref="dataSource"/> 
    </bean> 

然後是測試用例:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration({"classpath:spring-test.xml"}) 
@Transactional 
public class OperatorDaoTest extends AbstractTransactionalJUnit4SpringContextTests { 
//autowired dao field defined 
... 
@Test 
public void testMethod(){ 
//calls DAO method 
} 
} 

問題是一個異常:

Caused by: org.hibernate.MappingException: Could not determine type for: localDate, at table: TABLE_NAME, for columns: [org.hibernate.mapping.Column(DATE_COLUMN)] 

回答

1

我使用標準的日期映射到數據庫,然後在我的getter/setter我使用喬達時間和執行轉換,如您有避免的問題。 This may be of use

+0

Thanx的答案,但遵循你的建議涉及太多的努力,它更容易使用一個不同的解決方法,如添加類型定義屬性會話工廠。 –

+0

在我看來,這是一個薄弱的解決方法,並不能真正解決手頭的問題。 – rtcarlson

+0

@rtcarlson感謝評論/投票,也許你可以建議一個較弱的答案? – NimChimpsky