UnknownTableReferenceException on @ElementCollection of @Embeddable containing a @MayToOne with a @ManyToMany
Description
When “Hibernate.initialize(plan.getTransfers().get(0).getSubPlan().getEvents())” I get a “UnknownTableReferenceException: Unable to determine TableReference (`SubPlan`) for `Plan.transfers.events`”.
Hibernate seems to overlook the intermediate entity “SubPlan“ in the embeddable “Transfer“ here. The path above should be `Plan.transfers.subPlan.events`.
Reduced code:
@Entity
public class Plan {
@Id
public Integer id;
@ElementCollection
@CollectionTable(name = "transfer", joinColumns = @JoinColumn(name = "plan_id"))
public List<Transfer> transfers;
public List<Transfer> getTransfers() {
return transfers;
}
}
@Embeddable
public class Transfer {
@ManyToOne
@JoinColumn(name = "subplan_id")
@Cascade(ALL)
public SubPlan subPlan;
public SubPlan getSubPlan() {
return subPlan;
}
}
@Entity
public class SubPlan {
@Id
public Integer id;
@ManyToMany
@Fetch(SUBSELECT)
@Cascade(ALL)
public List<Event> events = new ArrayList<>();
public List<Event> getEvents() {
return events;
}
}
@Entity
public class Event {
@Id
public Integer id;
public String name;
}
When “Hibernate.initialize(plan.getTransfers().get(0).getSubPlan().getEvents())” I get a “UnknownTableReferenceException: Unable to determine TableReference (`SubPlan`) for `Plan.transfers.events`”.
Hibernate seems to overlook the intermediate entity “SubPlan“ in the embeddable “Transfer“ here. The path above should be `Plan.transfers.subPlan.events`.
Reduced code:
@Entity public class Plan { @Id public Integer id; @ElementCollection @CollectionTable(name = "transfer", joinColumns = @JoinColumn(name = "plan_id")) public List<Transfer> transfers; public List<Transfer> getTransfers() { return transfers; } } @Embeddable public class Transfer { @ManyToOne @JoinColumn(name = "subplan_id") @Cascade(ALL) public SubPlan subPlan; public SubPlan getSubPlan() { return subPlan; } } @Entity public class SubPlan { @Id public Integer id; @ManyToMany @Fetch(SUBSELECT) @Cascade(ALL) public List<Event> events = new ArrayList<>(); public List<Event> getEvents() { return events; } } @Entity public class Event { @Id public Integer id; public String name; }