JPA/JPQL: Fill a collection/map based on related o

2019-06-07 15:55发布

问题:

I have such structure of entities:

Product  1 -> N   ProductCustomField
  1                       1
  |                       |
  M                       M'
Release  1 -> N'  ReleaseCustomField

ProductCustomField is kind of "prototype" - a list of fields which Release may have.

I'd like to select a single Release object with all ReleaseCustomFields whose ProductCustomField are in the Product of which the Release is.

Example:

MySoft has custom field "downloadURL" with default value "". MySoft has release 1.0. This release has no ReleaseCustomFields, but because it is a release of MySoft, I can tell that it may have "downloadURL" custom field.

So I would like to have MySoft 1.0 release with a map where "downloadURL" would be defined (with a default value "").

I know what would I do in SQL, it would be a nice long complex SELECT, but possible.
I don't know though how to grasp it in JPA/Hibernate.

Not sure if this is the right thing to try to achieve in SELECT. Maybe I should rather fill the Release's custom fields with an INSERT and then simply rely on mapping (which already works)?
Or should I create a special property in Release, filled with special query? (I don't mind using org.hibernate.* annotations.)
Or should I do the simplest thing - take release.getProduct().getCustomFields() and do "for each field in ProductCustomField, use what's in ReleaseCustomField, or ProductCustomField#getDefault() otherwise?

回答1:

I have chosen the third approach. Since I use the Wicket framework, I have created a Model which, given a release and release.getProduct.getCustomFields().get("fieldName") is capable of getting either default value, or the actual value, and when written to, updates releases' field values map.

I started getting IllegalArgumentException: object is not an instance of declaring class but that's most likely a different problem.