Unable to delete entity with immutable collection

Hello,
Using 4.1.8.Final (I know, it’s old). I have an entity User which belongs to a Company. This user can have a few types of “membership”. Both directly (eg. User Record Type), and thru their associated Company (eg. Company Record Type). The memberships are a tree and so querying them “as is” is very cumbersome because of the rules (eg. if you have membership to something that is 2nd tier, but nothing explicit to a 3rd tier, then you implicity have membership to all 3rd tier items under 2nd tier). Anyhow, to make querying simpler, I created some views to “flatten” this layout and added a @OneToMany from User (ideally, these wouldn’t be views via something like CQRS, but I’m not there yet). In one of the views, the User is linked to a record via Company Id. When I try to delete the User, hibernate creates a CollectionRemoveAction which then tries to set the company id to null. Simply put, I just need hibernate to skip over these collections, but I’m not quite sure how to do that. There’s this CollectionEntry.isDoRemove flag which triggers the action creation. Seems like if that flag were false, then maybe I wouldn’t have this issue.

Here’s one mapping in particular for reference

@OneToMany
@JoinColumn(name = "SUPPLIER_ID", referencedColumnName = "SUPPLIER_ID")
@Immutable
public Set<EnterpriseRecordTypeMembership> getEnterpriseRecordTypeMemberships() {
	return enterpriseRecordTypeMemberships;
}

Please advise. Thanks!

If the attribute in EnterpriseRecordTypeMembership that refers to SUPPLIER_ID is nullable = false, the remove should be skipped. Anyway, this isn’t a good idea IMO. You should use a dedicated DTO projection instead.

I think this is a perfect use case for Blaze-Persistence Entity Views. It’s only tested against Hibernate 4.2, but I think it should work for 4.1 as well.

I created the library to allow easy mapping between JPA models and custom interface or abstract class defined models, something like Spring Data Projections on steroids. The idea is that you define your target structure(domain model) the way you like and map attributes(getters) via JPQL expressions to the entity model.

A DTO model for your use case could look like the following with Blaze-Persistence Entity-Views:

@EntityView(User.class)
public interface UserDto {
    @IdMapping
    Long getId();
    String getName();
    Set<EnterpriseRecordTypeMembership> getEnterpriseRecordTypeMemberships();

    @EntityView(Membership.class)
    interface EnterpriseRecordTypeMembership {
        @IdMapping
        Long getId();
        String getName();
    }
}

Querying is a matter of applying the entity view to a query, the simplest being just a query by id.

UserDto a = entityViewManager.find(entityManager, UserDto.class, id);

The Spring Data integration allows you to use it almost like Spring Data Projections: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features

Oh, I’m well aware that it’s not the best idea. :slight_smile: I needed to fix some critical functionality without having to open a can of worms. Apparently, I just kicked that can down the road. I’ll try the nullable = false suggestion and see where that gets me. Unfortunately, I don’t know how feasible it would be to introduce the framework/library you suggested, but I will check it out and if I can’t do it now, perhaps it will be useful for when I’m able to implement some CQRS. Thanks for the quick response.