Hibernate 初始化期间 Joda 类型 def 加载失败
Joda 时间与休眠支持配对使用。配置如下:
org.joda.time.package-info.java中有typedef:
@org.hibernate.annotations.TypeDefs({
@org.hibernate.annotations.TypeDef(
name="localDate",
typeClass =
org.joda.time.contrib.hibernate.PersistentLocalDate.class
)
})
package org.joda.time;
有一个带有会话工厂配置的spring上下文:
<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)]
Joda time in pair with hibernate support is used. Configuration is as follows:
There's typedefs in 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;
There's a spring context with session factory config:
<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>
Then there's test case:
@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
}
}
Problem is in next exception:
Caused by: org.hibernate.MappingException: Could not determine type for: localDate, at table: TABLE_NAME, for columns: [org.hibernate.mapping.Column(DATE_COLUMN)]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用标准日期映射到数据库,然后在我的 getter/setter 中使用 joda-time 并执行转换,以避免像您遇到的问题。 这可能有用。
I use standard Date to map to database, and then in my getter/setter I use joda-time and perform the conversion, to avoid issues like you are having. This may be of use.