openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pinaki Poddar <ppod...@apache.org>
Subject RE: Collection field part of a compound primary key
Date Thu, 19 Jul 2007 17:30:21 GMT

The domain model requires to define a compound identity for HolidayElement
{HolidayDate, ParentHoliday} -- is that right? 

If this is the case,  OpenJPA supports this feature as it allows a Relation
to be part of compound identity for an Entity. the outline of the model can
be:
==========================================
@Entity
class Holiday {
  @Id
  long hid;

  @OneToMany(mappedBy="parent")
  List<HolidayElement> elements;
}
===========================================
@Entity
@IdClass(HolidayAsPartOfCompoundId.class)
class HolidayElement {
  @Id
  Date date;
  @Id
  @ManyToOne
  Holiday parent;
}
===========================================
public class HolidayAsPartOfCompoundId {
  public Date date;  // same name and type of HolidayElement's first
identity field
  public long  hid;    // same name and type of Holiday's identity

  // Of course, you have to write equals() and hashCode() method of this
compound Id class.
===============================================

Further reading
 
http://openjpa.apache.org/docs/latest/manual/manual.html#ref_guide_pc_oid_entitypk
 
http://openjpa.apache.org/docs/latest/manual/manual.html#jpa_overview_pc_identitycls

  

   

Find attached the Holiday classes. Holiday is formed by several
HolidayElements. A HolidayElement is mainly identified by its date, but
several HolidayElement can share the same date if they are from
diffetent Holidays. Here the primary Key is formed by the date and the
Holiday id (which is a foreign key to Holiday). 

	
-- 
View this message in context: http://www.nabble.com/Collection-field-part-of-a-compound-primary-key-tf4110891.html#a11694049
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Mime
View raw message