Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible bug: Hibernate 4.3.5 @Lob annotation doesn't work in Postgres when used on a field

In an entity if I use

@Lob
private String data;

public String getData() {
    return data;
}

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

then the field is created in database as varchar(255)

However if I use like this:

private String data;

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

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

then the field is created as text in database.

I think in both cases it should be text in database.

@Target for javax.persistence.Lob is @Target(value={FIELD, METHOD}) so I think it maybe a bug.

Is it a bug or do you know a document that states the difference?


Related pom.xml piece

<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>
like image 357
destan Avatar asked Jan 26 '26 09:01

destan


1 Answers

Most likely annotation is misplaced in the first case. That's why it falls back to default. Mixing persistence annotations in both getters and fields is not supported without advising persistence provider. Annotations in setters are never consulted. If that is really needed, then AccessType is right tool.

Example:

@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 uses location of @Id annotation to determine is access type field or property. According JPA 2.0 specification inconsistent placement of annotation is simply not aloud, so there is no guarantee of how it should behave:

All such classes in the entity hierarchy whose access type is defaulted in this way must be consistent in their placement of annotations on either fields or properties, such that a single, consistent default access type applies within the hierarchy.

like image 200
Mikko Maunu Avatar answered Jan 28 '26 01:01

Mikko Maunu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!