JPA/JPQL: Fill a collection/map based on related object's collection/map? (prototype-like)

杀马特。学长 韩版系。学妹 提交于 2019-12-23 04:03:51

问题


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.



来源:https://stackoverflow.com/questions/14493642/jpa-jpql-fill-a-collection-map-based-on-related-objects-collection-map-proto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!