Hibernate 初始化期间 Joda 类型 def 加载失败

发布于 2024-12-18 08:37:02 字数 2013 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃素的狼 2024-12-25 08:37:02

我使用标准日期映射到数据库,然后在我的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文