Home > database >  when is a @Column annotation required for persistent properties of JPA classes?
when is a @Column annotation required for persistent properties of JPA classes?

Time:09-29

In my JPA model I typically annotate each persistent class with @Entity and each persistent property with an appropriate annotation e.g. @Id, @Column, @ManyToOne, etc. A typical example is

@Entity
@Table(name = "files")
public class StoredFile {

    @Id
    @Type(type = "uuid-char")
    private UUID id;

    @Column(name = "file_name")
    private String fileName;

    // getters and setters omitted
}

I was looking at this example entity class and noticed that only the id field has a JPA annotation, i.e. there are no annotations specified for name or price.

Under what circumstances will a property of an @Entity be persisted if there are no annotations on the field/getter/setter?

CodePudding user response:

You don't need to specify @Column annotation to persist a bean property.

@Column has to be used to specify a name of a table column. So if a naming strategy is used, you don't need to use @Column.

My advice is to always use @Column even if you don't need to specify a name.

@Column
private String fileName;

Also never mix fields and getters annotations.

Everything is primary for Hibernate.

  • Related