persistence icon indicating copy to clipboard operation
persistence copied to clipboard

Clarify requirements on polymorphic associations

Open lukasj opened this issue 12 years ago • 6 comments

The 2.0 specification states in 2.9 that "Relationships are polymorphic", but there exists one case that appears to be completely valid JPA that Hibernate explicitly does not support. The specification should explicitly state whether a provider is required to support this case.

When using a joined or single-table inheritance strategy, it is a common occurrence that an entity (e.g., Order) has a ManyToOne relationship with another entity (e.g., Customer). Since JPA supports class inheritance, it is possible that some of the Order instances are actually instances of a subclass (SpecialOrder), and we might want to have a Collection of just the SpecialOrder s on the Customer entity. (This is especially likely if Order is abstract and we want relationships directly to the subclasses.) An example class hierarchy looks like this:

@Entity
public class Customer {
  @OneToMany(mappedBy = "customer")
  private Set<SpecialOrder> specialOrders;

  @OneToMany(mappedBy = "customer")
  private Set<RegularOrder> regularOrders;
}

@Entity
public abstract class AbstractOrder {
  @ManyToOne
  private Customer customer;
}

@Entity
public class SpecialOrder extends AbstractOrder {
  private Date shippingDate;
}

@Entity
public class RegularOrder extends AbstractOrder {
  private Integer storeNumber;
}

The specification appears to imply but does not explicitly state that this arrangement is valid, while Hibernate explicitly requires that the mappedBy property be physically present on the exact class specified in the relationship mapping, not inherited from a superclass. This mismatch has inspired a surprising number of "how-to-work-around" blog posts and should be explicitly resolved for the next version of the specification.

lukasj avatar Sep 21 '12 18:09 lukasj

  • Issue Imported From: https://github.com/javaee/jpa-spec/issues/39
  • Original Issue Raised By:@glassfishrobot
  • Original Issue Assigned To: @ldemichiel

lukasj avatar Aug 31 '18 16:08 lukasj

@glassfishrobot Commented Reported by w_c_smith

lukasj avatar Sep 21 '12 18:09 lukasj

@glassfishrobot Commented This issue was imported from java.net JIRA JPA_SPEC-39

lukasj avatar May 05 '17 06:05 lukasj

This looks like something that could be easily looked at and resolved. Perhaps it makes sense to triage this for consideration to be worked on during the next release? I would still mark this low priority.

Reza Rahman Jakarta EE Ambassador, Author, Blogger, Speaker

Please note views expressed here are my own as an individual community member and do not reflect the views of my employer.

m-reza-rahman avatar Apr 30 '21 13:04 m-reza-rahman

I don't think the issue description really captures the problem here.

It's not true that Hibernate doesn't let you have an association from SpecialOrder to Customer where the customer field is defined on AbstractOrder.

The actual issue is that here we have two different @OneToMany associations mapped to the same field of Customer.

Now, what happens in this case is dependent on the inheritance mapping strategy declared on AbstractOrder:

  1. For TABLE_PER_CLASS everything works just fine.
  2. For inheritance mappings with a discriminator column, it would in principle be possible to include a list of discriminator values in the join condition. Hibernate currently doesn't do that, but some other JPA implementation might. And perhaps Hibernate could consider just fixing this.
  3. For JOINED inheritance mappings with no discriminator, it's much trickier, but again, in principle possible, I suppose. (Note that in this case it's a vary unnatural mapping from the data model point of view.)

Given the subtleties here, I would argue it's probably OK to leave this vague in the spec.

(Though I guess it would also be OK to add language to the effect that the implementation is not required to support this.)

gavinking avatar Apr 30 '21 13:04 gavinking

This looks like something that could be easily looked at and resolved.

Actually I think this is pretty subtle.

I would still mark this low priority.

Right: I doubt it's a priority at all.

gavinking avatar Apr 30 '21 13:04 gavinking

This has already been fixed in Hibernate 6. I still don't think the spec needs to say anything about this issue.

gavinking avatar Aug 10 '23 23:08 gavinking