How to use @Version in a @JoinTable

48 Views Asked by At

I am having an entity with a @ManyToMany relation:

@Audited(targetAuditMode = NOT_AUDITED)
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
@JoinTable(
        name = "company_relationship",
        joinColumns = @JoinColumn(name = "company_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "relationship_id", referencedColumnName = "id"))
private Set<CompanyRelationshipLookup> companyRelationshipLookups = new HashSet<>();

I find it really handy to use @JoinTable, so the data is stored in my table company_relationship without providing an entity CompanyRelation. The table looks like this:

create table company_relationship
(
    company_id      bigint not null
        constraint company_relationship_company_id_fk
            references company,
    relationship_id bigint not null
        constraint company_relationship___fk
            references company_relationship_lookup,
    version         integer,
    constraint company_relationship_pk
        primary key (company_id, relationship_id)
);

In the db-table you can see the column version. Usually i would set @Version for optimistic locking in the entity, but as I don't want to implement a linking-entity each time and use @ManyToMany with @JoinTable instead.

Is there a way to do it? Or could this maybe be implemented in the future within @Jointable? I am using JPA 3.1.

Thanks for your help!

0

There are 0 best solutions below