AuditQuery NonUniqueResultExceptoin for ManyToOne relation

23 hours ago 5
ARTICLE AD BOX

I have a pair of entities, both audited, which look a bit like this:

@Audited EntityA { @Column(name = "some_string") private String someString; @ManyToOne(fetch = FetchType.LAZY, optional = false) @JoinColumn(name = "entity_b_id") @NotNull private EntityB entityB; } EntityB { @OneToMany(mappedBy = "entityB", fetch = FetchType.LAZY) @MapKey(name = "someString") private Map<String, EntityA> entityAs; }

I don't know if the collection side being a Map matters. I'm using ValidityAuditStrategy.

When I query for revisions of EntityA, it eagerly fetches entityB. If entityB has multiple revisions which are valid for the given EntityA I get a NonUniqueResultException.

I tried adding @AuditJoinTable to EntityB.entityAs, but it didn't change the queries AuditReader attempts. For giggles I tried putting @AuditJoinTable on Entity A.entityB, but still no change. I didn't actually create the table and attempt populating it. I just wanted to see the query change (and obviously go boom).

Turning off ValidityAuditStrategy makes the error go away. The query for entityB changes to get the entity at the max revisions at or below EntityA's revision, exactly what I want.

Is there a way to make this work with ValidityAuditStrategy? Without resorting to crafting projection queries rather than just getting the entity as a whole, which I assume I could make work?

Edit: Quarkus 3.20/Hibernate 6.6. A brief foray to 3.27/7.1 didn't show any difference.

Read Entire Article