zeitiger
zeitiger

Reputation: 187

How to define database generated column values as readonly fields in JPA and Hibernate?

With MariaDB 10.2 it's possible to define default value for Datetime e.g. created and lastModified.

How I should access this columns as readonly field? Because this values should only be under control of the database and should not be modified from code, but I want read access to this property in code.

Upvotes: 12

Views: 5113

Answers (2)

Teo
Teo

Reputation: 3213

You can use:

@Column(updatable=false, insertable=false)
private YourType field;

Where the @Column is used to specify the mapped column for a persistent property or field. In particular it is javax.persistence.Column.

Upvotes: 9

Vlad Mihalcea
Vlad Mihalcea

Reputation: 153930

It's simple. Just set the insertable and updatable attributes to false.

@Column(
    name = "created_on", 
    insertable = false, 
    updatable = false
)
private Timestamp createdOn;

Upvotes: 16

Related Questions