【问题标题】:Why does hbm2ddl not like my @Temporal annotation on a GregorianCalendar?为什么 hbm2ddl 不喜欢我在 GregorianCalendar 上的 @Temporal 注释?
【发布时间】:2012-10-26 15:30:59
【问题描述】:

这是背景:我有一个带注释的 @Embeddable Java 类,它有一个 GregorianCalendar 字段。我正在尝试使用 hibernate3:hbm2ddl 通过 hibernate3 Maven 插件生成模式,以便我可以持久保存嵌入它的另一个对象,但它遇到了关于使用 @Temporal 的错误。

这是可嵌入类:

@Embeddable
public class OperationalStatus implements Serializable {
        .
        .
        .
    /**
     * Recorded date/time that the status value is valid
     */
    private GregorianCalendar time;

    /**
     * No-argument constructor.
     */
    public OperationalStatus() {}
        .
        .
        .
    /**
     * @return the time
     */
    @Temporal(TemporalType.TIMESTAMP) 
    public GregorianCalendar getTime() {
        return time;
    }

    /**
     * @param time the time to set
     */
    public void setTime(GregorianCalendar time) {
        this.time = time;
    }
}

这是错误读数:

[错误] 无法执行目标 org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm 项目 STRIPES_V2 上的 2ddl (default-cli):执行目标 org.code 的 default-cli haus.mojo:hibernate3-maven-plugin:2.2:hbm2ddl 失败:@Temporal 应该只是 s 等在 java.util.Date 或 java.util.Calendar 属性:stripes.datamodel。 util.OperationalStatus.time

以下是 pom 中的一些摘录:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
    <components>
        <component>
            <name>hbm2ddl</name>
            <implementation>annotationconfiguration</implementation>
        </component>
    </components>
    <componentProperties>
        <drop>false</drop>
        <configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>
        <outputfilename>schema.sql</outputfilename>
    </componentProperties>
</configuration>
<dependencies>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.2-1000.jdbc4</version>
    </dependency>
</dependencies>
</plugin>
.
.
.
<dependency>
    <groupId>org.hibernate</groupId>
    <version>4.1.7.Final</version>
    <artifactId>hibernate-core</artifactId>
</dependency>

我错过了什么? GregorianCalendar 是 Calendar 的具体扩展,有什么问题?

【问题讨论】:

  • 你使用的是什么版本的 Hibernate?
  • 添加了 POM 信息。到问题文本。

标签: java hibernate maven hbm2ddl hibernate3-maven-plugin


【解决方案1】:

规范中的任何内容都没有限制提供者能够出售您想要的任何特定的日历实现,因此如果他们允许您要求它提供特定的日历,这将是一个兼容性问题。 (并不是说任何头脑正常的人都会创建一个新的 Calendar 子类,但这种可能性是存在的。一个只知道如何返回自己的自定义 Calendar 子类的 JPA 实现不会因为规范中的任何内容而真正出错,你不能要求它知道如何使用 GregorianCalendar。)

或者另一方面,如果我要创建 org.affe.MyAwesomeCalendar,期望 JPA 提供者能够为我创建实例是不合理的!

11.1.47 时间注释

必须为 java.util.Date 类型的持久字段或属性指定 Temporal 注释,并且 java.util.日历。只能为字段或属性指定 这些类型。

Hibernate 也不会让你直接选择 java.sql.Date 等。基本上,他们将其解释为由持久性提供者决定使用什么日历实现。

【讨论】:

  • 好吧。关于如何最好地处理我的情况有什么建议吗?
  • 如果您愿意专门使用 hibernate 而不是坚持使用 JPA,那么创建它不会是一个难于创建的用户类型,可能有很多人这样做的例子。如果出于某种原因您真的需要 API 是 GregorianCalendar 而不是 Calendar,您也可以使用瞬态字段来保存您构建的 GregorianCalendar。
猜你喜欢
  • 2012-01-26
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
  • 2015-08-06
  • 1970-01-01
  • 2012-06-25
  • 2021-10-15
  • 2016-11-04
相关资源
最近更新 更多