I have something like this:
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "my_read_only_view",
joinColumns = {@JoinColumn(name = "entity_id", referencedColumnName = "id", updatable = false, insertable = false)})
@MapKeyColumn(name = "key", updatable = false, insertable = false)
@Column(name = "value", updatable = false, insertable = false)
private Map<Long, MyEnum> mapProperty;
As its name says my_read_only_view
is a read-only database view that "calculates" keys and values automagically using a DB-specific SQL (and I cannot accomplish the same in JPA/Hibernate scope or QLs). However, when the "owner" entity is deleted, Hibernate tries to do what it believes is its duty... and delete the relevant row from that read-only view. The DB then responds with cannot delete from view
error.
Additional details (that I cannot change):
- Hibernate 5.4.14
- Using field access + bytecode enhancement:
enableLazyInitialization = true
,enableDirtyTracking = true
,enableAssociationManagement = false
,enableExtendedEnhancement = false
... andhibernate.bytecode.use_reflection_optimizer = true
- That field is literally only used by a getter. In fact, not even that getter is used. This mapping exists solely so that we can include it in search criteria, isn't even supposed to ever be fetched.
I've dealt with the error by creating a DB rule to ignore attempts to delete... but that still leaves some performance impact. What is the best way to stop Hibernate from attempting to delete this? I was looking at @Formula
but this is a map and it is said that Hibernate will execute it for every fetch, which is definitely not what we need or want.