【问题标题】:Hibernate timestamp version controlled by database.由数据库控制的休眠时间戳版本。
【发布时间】:2012-10-03 14:37:00
【问题描述】:

我同时使用 Hibernate 注释和 Sybase。我正在寻找设置版本列以防止锁定。数据库需要管理时间戳而不是应用程序。我想使用注释而不是 hbm.xml 来完成此操作。

我尝试了以下但没有成功,

我在 jboss.org 上阅读以使用

@org.hibernate.annotations.SourceType.DB
@org.hibernate.annotations.Generated(GenerationTime.ALWAYS)

但是,我收到了 DB 的 IDE 编译错误, “找不到标志 符号:DB类 位置:类 SourceType"

以及 rowVersion 的编译错误,

版本字段或属性不是受支持的类型之一。确保它是以下类型之一:int、Integer、short、Short、long、Long、java.sql.Timestamp。

必须使用@Temporal 注释标记时间属性。

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html#d0e5785

5.1.3.2。时间戳

示例代码

@Version
@org.hibernate.annotations.SourceType.DB
@org.hibernate.annotations.Generated(GenerationTime.ALWAYS)
@Column(name = "row_version")
private Date rowVersion;

public Date getRowVersion() {
    return rowVersion;
}

public void setRowVersion(Date rowVersion) {
    this.rowVersion = rowVersion;
}

谁能告诉我我错过了什么?

【问题讨论】:

    标签: java sql hibernate sybase


    【解决方案1】:

    这不是注解,而是枚举的一个字段:

    @org.hibernate.annotations.SourceType.DB
    

    你的领域需要这个:

    @Version
    @org.hibernate.annotations.Source(SourceType.DB)
    @org.hibernate.annotations.Generated(GenerationTime.ALWAYS)
    @Column(name = "row_version") //maybe unnecessary, because this annotation
                                  //is only needed, if the column name does not
                                  //match hibernate's default naming strategy
                                  //(or custom strategy).
    @Temporal(TemporalType.TIMESTAMP)
    private Date rowVersion;
    

    【讨论】:

    • 谢谢,最后一期,您如何处理 Date 数据类型。 IDE 要我使用一个 temporal 属性必须用 Temporal 注释标记
    • 我应该使用什么日期时间?
    • 只需将 Temporal 注释添加到该字段。 (例如版本)
    • Markus 我认为应该是@Temporal(TemporalType.TIMESTAMP)
    • @Markus 是否有 JPA 替代品?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 2023-03-28
    • 2010-11-20
    • 2011-06-18
    相关资源
    最近更新 更多