3

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):

  1. Hibernate 5.4.14
  2. Using field access + bytecode enhancement: enableLazyInitialization = true, enableDirtyTracking = true, enableAssociationManagement = false, enableExtendedEnhancement = false ... and hibernate.bytecode.use_reflection_optimizer = true
  3. 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.

Learner
  • 1,215
  • 1
  • 11
  • 26
  • hello! can you change the mapping? if it is a map with key=yourEntityId does not this mean you could map directly its value? what about having an immutable entity "MapEntry" that contains both the current key and the current value of the map for your entity instead of the full Map? – tremendous7 May 22 '21 at 00:04
  • I can adjust the mapping. It isn't that simple, though. What it is, actually, is a view that "pretends" to have a row for each entity (id) and user (id) combination, with an additional column of having the access/permission that user has on that entity. This gives consistency in access control. It is never fetched, as I said, but it participates in the search criteria (user must have sufficient access to the entity). – Learner May 22 '21 at 19:53
  • Have you tried `@Immutable`? – crizzis May 22 '21 at 21:23
  • Hm. No. Trying now. Will have results my next morning.... or sooner ... will see – Learner May 23 '21 at 01:17
  • Didn't help: `[org.hibernate.engine.jdbc.spi.SqlExceptionHelper] ERROR: cannot delete from view ...` – Learner May 23 '21 at 12:04

0 Answers0