i have convenient relation set in entity has one-to-many relationship another, , has many-to-one another. so, listing has many listing_line_items, , listing_line_items have 1 service_period, service_period has many listing_line_items. have attempted describe relationship using jpa's @jointable follows:
listing
@onetomany @jointable (name = "listing_line_item", joincolumns = @joincolumn (name = "listing_id"), inversejoincolumns = @joincolumn (name = "service_period_id")) set<serviceperiod> serviceperiods;
listing_line_item
@manytoone (fetch = fetchtype.eager) @joincolumn (name = "listing_id", nullable = false) listing listing; @manytoone (fetch = fetchtype.eager, cascade = cascadetype.persist) @joincolumn (name = "service_period_id") serviceperiod serviceperiod;
service_period
@manytoone @jointable (name = "listing_line_item", joincolumns = @joincolumn (name = "service_period_id"), inversejoincolumns = @joincolumn (name = "listing_id")) listing listing;
the obvious goal able obtain list of serviceperiod
s listing
or single listing
serviceperiod
. way set i'm getting exception:
org.hibernate.hibernateexception: more 1 row given identifier found: 361951, class: com.gonfind.entity.serviceperiod
i believe because listing has listinglineitems refer same serviceperiod. i'm sure there way accomplish i'm after don't know is.
you appear have problems there. on technical / jpa side:
you cannot use
listing_line_item
both join table , entity table. there several reasons this, main reason confuse jpa: try use table in different, incompatible ways 2 purposes.in jpa, bidirectional relationship owned 1 side; other side uses
mappedby
attribute of relationship annotation reference owning side.
but have data design problems. constraint line items' service periods restricted 1 of separately associated same listing constitutes either
a functional dependency between non-key fields, if listing id not part of line item key, or otherwise
a functional dependency on subset of key.
in first case, data fail in third normal form; in second case fail in second normal form. trouble modeling jpa arises in part low level of normalization.
normalizing data make things lot easier on multiple levels. that, need remove direct association between listings , line items, , instead associate them through service periods. have:
listing
<-- 1 many --> serviceperiod
<-- 1 many --> lineitem
of course, have implications on structure of application, it's long-term development , maintenance win, , maybe usability win, application aligned natural structure of data that. if wish, put methods on listing
entity allow listinglineitem
s managed extent if belonged directly listing
s, , vise versa.
that data organization this:
listing
@onetomany(mappedby = "listing", fetch = fetchtype.eager, cascade = cascadetype.persist) set<serviceperiod> serviceperiods;
service_period
@manytoone(fetch = fetchtype.eager) @joincolumn(name = "listing_id") listing listing; @onetomany(mappedby = "serviceperiod", fetch = fetchtype.eager, cascade = cascadetype.persist) set<listinglineitem> lineitems;
listing_line_item
@manytoone(fetch = fetchtype.eager) @joincolumn(name = "service_period_id") serviceperiod serviceperiod;
if cannot restructure data more or less way, you're stuck jerry-rigging cannot described jpa. i'm imagining separate join table listing
<-> serviceperiod
, non-jpa fk constraint table entity table line items, and, of course, proper form various bidirectional relationships.
Comments
Post a Comment