I want to create an entity on which inserts or updates of any kind should not be made, I can think of two ways to do this:
Using insertable=false and updatable=false tag on each column
@Entity
@Table(name = users)
class Users {
@Column(name= 'name' insertable=false, updatable=false)
String name;
}
I don't like this although it works because I have to put those labels in all the columns and I would like to do it at the class level, for this I can think of using the @Inmutable annotation of hibernate
@Entity
@Inmutable
@Table(name = users)
class Users {
@Column(name= 'name')
String name;
}
This tag allows me to do a get on the entity but not be able to modify those values later.
Does the immutable tag override hibernate persistence or can it be saved somehow? Is there any other tag that I can use at the class level in an entity that allows me to modify objects of the entity but not insert or update in the database?
In effect, the immutable tag does not allow you to persist the entity in the database.
You can modify the objects of an entity without having to insert them into the database, it is only inserted into the database when you execute the persist method. Therefore, all the modifications you make to the object will be reflected in memory, if you do not want to store the modifications, simply do not persist the object.
Another option is to map a view instead of a table
Suppose we have our User table, so we create the view being a query of our original table
And in our mapping we reference the view
When hibernate creates the query it will simply query with the name of the view that will be valid. But when someone wants to persist or update such an entity they will get an ugly exception and won't be able to do it.
You can add the following lines to the repository code: