Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to treat a table as read only in hibernate?

I have some tables for which I have no reason to ever update from within the application, so I would like to prevent it from happening even accidentally by a bug somewhere else.

I see the @Immutable annotation, but it looks like that will still allow insertions and deletions. I'd like to full on treat the whole table (not just each entity) as written in stone. Is there an easy way to accomplish this? Or do I misunderstand the documentation on @Immutable?

If an example is needed, lets say that there is a table with MONTH table, and a Month entity, and a APPOINTMENT table with a Appointment entity associated. I would never want to delete or insert a row into month.

like image 430
derekv Avatar asked Mar 30 '12 21:03

derekv


People also ask

Do we need @transactional for read-only?

Generally, we use @Transactional(readOnly = true) for search or retrieval operation to make sure we can only perform the read-only operation. We can override readOnly behavior using @Modifying annotation.

How do you split read-only and read/write transactions with JPA and Hibernate?

READ_WRITE key and the readOnlyDataSource using the DataSourceType. READ_ONLY key. So, when executing a read-write @Transactional method, the readWriteDataSource will be used while when executing a @Transactional(readOnly = true) method, the readOnlyDataSource will be used instead.

What is @transactional readOnly true?

It's good practice to define the @Transactional(readOnly = true) annotation at the class level and only override it for read-write methods. This way, we can make sure that read-only methods are executed by default on the Replica nodes.


1 Answers

Have you tried read only caching strategy:

If your application needs to read, but not modify, instances of a persistent class, a read-only cache can be used. This is the simplest and optimal performing strategy. It is even safe for use in a cluster.

From: 19.2.2. Strategy: read only

like image 96
Tomasz Nurkiewicz Avatar answered Nov 06 '22 12:11

Tomasz Nurkiewicz