How can I determine database column values โ€‹โ€‹given as read-only fields in JPA and Hibernate?

With MariaDB 10.2 it is possible to define a default value for Datetime, eg. created and lastModified.

How do I access these columns as a read field? Since these values โ€‹โ€‹should only be under the control of the database and should not be changed from code, but I want to make this property readable in code.

+3


source to share


2 answers


It's simple. Just set the attributes to insertable

and updatable

on false

.



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

      

+4


source


You can use:

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

      



Where @Column

used to specify the mapped column for a persistent property or field. In particular, this javax.persistence.Column

.

+4


source







All Articles