【问题标题】:Hibernate: Defining type mapping project-wideHibernate:在项目范围内定义类型映射
【发布时间】:2012-01-17 21:48:19
【问题描述】:

我正在使用 Hibernate 和 Joda Time,并得到 UserType 库的支持。

我可以使用以下方法保留我的日期:

@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column
private DateTime startDate;

但是,我想知道是否可以在某处定义此映射一次,而不是在 DateTime 类型的每个实例上。

在从 Hibernate 3.4 -> 4.0 迁移时不得不从 JodaTime Hibernate 换出到 UserType,我不愿意在整个域模型中散布类型声明。

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

    使用 Usertype 的 3.0.0.CR1 版本,您可以利用以下类型的自动注册:

    PersistentBigMoneyAmount
    PersistentMoneyAmount
    PersistentCurrency
    PersistentCurrencyUnit
    PersistentDateTime
    PersistentDurationAsString
    PersistentInstantAsTimestamp
    PersistentLocalDate
    PersistentLocalTime
    PersistentMonthDayAsString
    PersistentPeriodAsString
    PersistentTimeOfDay
    PersistentYearMonthDay
    PersistentYears
    

    要启用此功能,您需要将 JPA 或 Hibernate 属性“jadira.usertype.autoRegisterUserTypes”设置为“true”。

    升级 usertype.joda 时,Maven 工件现在称为“usertype.core”。

    更多信息在这里http://blog.jadira.co.uk/blog/2012/1/19/release-jadira-usertype-300cr1-with-support-for-joda-money-0.html

    【讨论】:

      【解决方案2】:

      您可以实现BasicType,并在构建SessionFactory 时注册它。 hibernate 参考手册在section 6.4.1 中对此进行了解释。

      【讨论】:

        猜你喜欢
        • 2012-12-23
        • 1970-01-01
        • 2017-10-11
        • 1970-01-01
        • 1970-01-01
        • 2021-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多