【问题标题】:Possible bug: Hibernate 4.3.5 @Lob annotation doesn't work in Postgres when used on a field可能的错误:在字段上使用时,Hibernate 4.3.5 @Lob 注释在 Postgres 中不起作用
【发布时间】:2014-08-02 15:09:29
【问题描述】:

如果我在一个实体中使用

@Lob
private String data;

public String getData() {
    return data;
}

public void setData(String data) {
    this.data = data;
}

然后该字段在数据库中创建为varchar(255)

但是,如果我这样使用:

private String data;

@Lob
public String getData() {
    return data;
}

@Lob
public void setData(String data) {
    this.data = data;
}

然后该字段在数据库中创建为text

我认为在这两种情况下,数据库中都应该是text

@Target for javax.persistence.Lob@Target(value={FIELD, METHOD}) 所以我认为这可能是一个错误。

这是一个错误还是您知道说明差异的文档?


相关 pom.xml 片段

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>4.3.5.Final</version>
</dependency>

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>4.3.5.Final</version>
</dependency>

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>9.2-1004-jdbc41</version>
</dependency>

【问题讨论】:

    标签: hibernate postgresql jakarta-ee jpa postgresql-9.2


    【解决方案1】:

    很可能在第一种情况下注释放错了位置。这就是为什么它会退回到默认值。在没有建议持久性提供者的情况下,不支持在 getter 和字段中混合持久性注释。从不参考 setter 中的注释。如果确实需要,那么AccessType 是正确的工具。

    例子:

    @Entity
    public class Box  {
        @Id
        int id;
        @Lob //here is right place because also 'id' does have annotation in field
        String code;
    
        @Lob //this annotation is ignored because 'id' does have annotation in field 
        //if getId() (instead of field) is one with annotation, 
        //then this is right place
        public String getCode() {
            return code;
        }
    
        @Lob // persistence annotation here is not at all supported
        public void setCode(String code) {
            this.code = code;
        }
    
    }
    

    Hibernate 使用 @Id 注释的位置来确定是访问类型字段还是属性。根据 JPA 2.0 规范,注释的不一致放置根本不会大声,因此无法保证它应该如何表现:

    实体层次结构中所有此类访问类型为 以这种方式默认的必须在它们的位置上保持一致 字段或属性上的注释,例如单个, 一致的默认访问类型适用于层次结构。

    【讨论】:

    • 我猜你误解了这里的情况。在第一个代码示例中,我给出的注释与您声称“正确”的位置完全相同,就在字段上方。所以不会错位。我错过了什么吗?
    • 是的,您错过了我对您的问题中未显示的代码的假设。关键是应该注释字段或属性 - 不支持在两者中混合注释。我的假设是,在您的代码中,其他持久性属性具有用于 getter 方法的持久性注释。如果是这样,这就解释了为什么字段的 @Lob 注释被忽略,但对于 getter 也是如此。
    • 哦,现在我明白了。 JPA 的报价很清楚。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 2020-02-02
    相关资源
    最近更新 更多