Feature request: bidirectional @Any mapping
Description
Attachments
1
- 09 Jan 2023, 02:33 PM
is duplicated by
relates to
Activity
Show:
Vincent Bouthinon April 4, 2024 at 1:18 PMEdited
Pull request proposed (Solution + test) : HHH-15722 : @OneToMany mappedBy with a @Any by boutss · Pull Request #8109 · hibernate/hibernate-orm (github.com)
Vincent Bouthinon January 9, 2023 at 2:34 PM
I took the liberty of adding a test that reproduces (JPAUnitTestCase)
Vincent Bouthinon January 9, 2023 at 1:54 PM
Hello,
I exposed the same problem here: @OneToMany with interface type return variable (role column) @Any - Hibernate ORM - Hibernate
I think there is no need to recreate
I’d like to request support for the inverse side of an @Any mapping. For example a Task containing SubTasks, both of which could exhibit TaskExceptions (I described this example in this Discourse topic). Code would be something like this:
public interface Excepted { ... } @Entity public class TaskException { @Any @AnyDiscriminator(DiscriminatorType.STRING) @AnyDiscriminatorValues({ @AnyDiscriminatorValue(discriminator = "Task", entity = Task.class), @AnyDiscriminatorValue(discriminator = "SubTask", entity = SubTask.class)}) @AnyKeyJavaClass(String.class) @Column(name = "excepted_type") @JoinColumn(name = "excepted_id") private Excepted excepted; ... } @Entity public class Task implements Excepted { @OneToMany(mappedBy = "excepted") private List<TaskException> exceptions = new ArrayList<TaskException>(); ... } @Entity public class SubTask implements Excepted { @OneToMany(mappedBy = "excepted") private List<TaskException> exceptions = new ArrayList<TaskException>(); ... }
Maybe inverse of an @Any would not be a ‘clean’ @OneToMany, perhaps it should be named @AnyToMany or something? Anyway, at the moment the code above doesn’t work, but something like this is will:
@OneToMany @JoinColumn(name = "excepted_id") @Where(clause = "excepted_type = 'Task'") private List<TaskException> exceptions = new ArrayList<TaskException>();
If implementation of a new or expanded annotation is infeasible or undesirable, maybe the use of the @JoinColumn and @Where annotations could be documented?