java - Classes Relationships with JPA -


i have set of java classes following uml diagram:

enter image description here

 public class invoice {            @id           private long id; ...         }  public class invoicedetail {           @id           private long id;           ...           private string productname;           private int quantity;           private double price;          } 

my purpose using jpa annotations establish different relationships between them. there composition relationship between invoice , invoicedetail, resolved using @embedded , @embeddable annotations invoice , invoicedetail respectively. however, problem appears establishing relationships between invoicedetail, class3 , class4. in these relationships invoicedetail must annotated @entity. however, when class annotated @ same time @entity , @embeddable, corresponding server throw runtime error during deployment. basing on information of website, have written following possible solution:

@entity public class invoice {   @id   private long id;   ...   @elementcollection   @collectiontable(name="invoicedetail", joincolumns=@joincolumn(name="invoice_id"))   private list<invoicedetail> invoicedetails;   ... } 

would right in order resolve problem?

thanks in advance.

although without knowing classes hard tell, suppose have design problem. composition between class1 , class2 says class2 instance exists within lifecycle of corresponding class1 instance. on other hand have class3 instances , class4 instances can / must have relationship class2 instance.

what i'm trying point of view relationship between class1 , class2 should simple association , not composition. following path class2 entity in jpa , should have problem solved.

i use @embeddable classes instances never exist themselfes , @entity class instances can exist without other instances. address example implemented either way not on same system. address @embeddable if don't want link addresses had @entity if want make sure same address isn't saved in more 1 row.


[edit: added after classes 1 , 2 renamed invoice , invoicedetails]

having composition between invoice , invoicedetails makes perfect sense. still think should avoid need of double personality invoicedetails. can think of 2 solutions (both refactorings):

  1. if prefer having invoicedetails @embeddable change associations of class3 , class4 invoice instead of invoicedetails. invoicedetails still traversable via invoice object.
  2. if prefer keeping associations declare invoicedetails entity. still achieve composition cascading delete (see javax.persistence.cascadetype). seems invoicedetails has it's own table, better option.

i checked jpa applications , haven't found occurence of same class being @entity , @embeddable. honestly, doubt if possible @ because official javadoc of @embeddable says:

specifies class instances stored intrinsic part of owning entity , share identity of entity.

as @entity has it's own identity, try declare same object having 2 identities - , can't work.

[/edit]


[edit2: adding code solution proposal #2]

this code should work assumptions (see below). implementation of bi-directional navigation 1:n-relationship.

@entity public class invoice {   @id   private long id;    @onetomany(mappedby="invoice", cascade = cascadetype.all)   private list<invoicedetail> details; }  @entity public class invoicedetails {   @id   private long id;    @manytoone   @joincolumn(name="invoice_id")   private invoice invoice; } 

assumptions: tables named entities, foreign key column invoice_details table named "invoice_id" , both tables have primary key column named "id". note mappedby-value "invoice" refers entity field while name-value "invoice_id" refers database table. cautious when deleting invoice object invoicedetails still referenced class3 or class4 instances - have release these references first.

for information jpa refer these resources:

[/edit]


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -