I've stumbled upon a situation i cannot resolve using Hibernate documentation - i cannot alter an @ElementCollection from within the corresponding entity or even the DAO/Service.
My setup has a hibernate entity, separated into two classes:
Code:
@Entity
@Table(name = "PERSON")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="person")
public class Person implements PersonInterface{
...
@ElementCollection
@Formula("(SELECT groupId FROM PERSON_GROUPS WHERE id = id)")
protected List<Integer> groups = new ArrayList<>();
...
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
@JoinTable(name = "user_roles", joinColumns = {
@JoinColumn(name = "USER_ID", nullable = false, updatable = false)},
inverseJoinColumns = { @JoinColumn(name = "ROLE_ID", nullable = false, updatable = false)})
protected List<Role> roles = new ArrayList<>();
Code:
@Entity
@DiscriminatorValue("mutableperson")
public class MutablePerson extends Person implements MutablePersonInterface{
...
public void excludeFromRole(Integer roleId){
roles.remove(roleId);
}
public void excludeFromGroup(Integer groupId){
groups.remove(groupId);
}
}
The thing is, while excludeFromRole works, excludeFromGroup throws an exception "failed to lazily initialize a collection of role". I thought that had something to do with no open sessions, etc., but even when i explicitly call excludeFromGroup in DAO with an entityManager, it still returns the same exception.
I tried detaching the entity with EntityManager or closing the session in which it was fetched from the DB, but it didn't help.
This page
https://docs.jboss.org/hibernate/orm/5. ... rmula.html says, that Formula represents a read-only state. Does this mean what i'm trying to do is effectively impossible and i have to look at @ColumnTransformer (which i can't get to work properly so far)?