Thanks Pinaki for that referente to the manual. I haven't seen
it before, and is totally clarifying for me. At least is clear this is a
supported feature.

        Unfortunately i'm getting the same problem as before. I followed
the manual examples, exactly the same you suggested me. But seems that
JPA is unnable to relate the fields in the Id class, with the ones of
its counterpart class. It keeps alerting the property names doesn't
match.

        This is the message showed when JPA is parsing metadata:

Caused by: <1.0.0-SNAPSHOT-SNAPSHOT fatal user error>
org.apache.openjpa.persistence.ArgumentException: The id class specified
by type "class
com.covansys.routingengine.functionalcore.data.ScreeningElement" does
not match the primary key fields of the class.  Make sure your identity
class has the same primary keys as your persistent type.  Mismatched
property: "screening"

        What about the metadata information for the IdClass, is it
neccesary?. I guess this would not have many sense, since a would be
recursively finding the same problem.

        Any hints here? Am i loosing some special setting? Has anyone
use this before?

        Thanks in advance.



And here is my code. Again screeningelement table should allow the same
screntry if they are owned by different screenings:



//////Screening.java
package com.covansys.routingengine.functionalcore.data;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.regex.Pattern;
import javax.persistence.*;
import com.covansys.routingengine.functionalcore.Branch;
import com.covansys.routingengine.functionalcore.*;
@Entity

public class Screening {

        @Id
        long scrId;
        
        @OneToMany(mappedBy="screening")
        private List<ScreeningElement> whiteList;
        

        
        public Screening() {
                
                //super();
                whiteList = new Vector<ScreeningElement>();
        }
        
        public Screening(String l) {
                //super();
                //setLabel(l);
                whiteList = new Vector<ScreeningElement>();

        }
        
        public void addWhilteListElement(String elem)
        {
                try {
                        ScreeningElement newElem = new
ScreeningElement(elem);
                        whiteList.add(newElem);
                } catch(Exception e) {
                        
                }
        }
        
        public void removeWhiteListElement (String elem)
        {
                try {
                        ScreeningElement newElem = new
ScreeningElement(elem);
                        whiteList.remove(newElem);
                } catch(Exception e) {
                        
                }
        }
        
        public void addBlackListElement(String elem)
        {
                try {
                        ScreeningElement newElem = new
ScreeningElement(elem);
                } catch(Exception e) {
                        
                }
        }
        
        public void removeBlackListElement (String elem)
        {
                try {
                        ScreeningElement newElem = new
ScreeningElement(elem);
                } catch(Exception e) {  
                        
                }
        }
        
        public boolean isInWhiteList (String elem){
                
                boolean itemFound=false;
        
                Iterator<ScreeningElement> itWhiteList =
whiteList.iterator();

                while ( itWhiteList.hasNext() & !(itemFound)){
                        
                        ScreeningElement currScrElem =
itWhiteList.next();
                        itemFound =
Pattern.matches(currScrElem.getElement(), elem);
        
                }       
                
                return itemFound;
        
        }
        

        
        public boolean whiteListIsEmpty (){
                
                return (whiteList.size()==0);
                
        }
        

}

///////////end screeening.java////////



////////////ScreeeningElement.java/////////
package com.covansys.routingengine.functionalcore.data;

import javax.persistence.*;
@Entity
@IdClass(ScreeningElementId.class)
public class ScreeningElement
{
        @Id
        @ManyToOne
        private Screening screening;
        
        @Id
        private String ScrEntry;
        
        public ScreeningElement(){
                
        }
        
        public ScreeningElement(String el){
                ScrEntry = el;
        }
        
        public String getElement(){
                return ScrEntry;
        }
        
    public boolean equals(Object other)
    {
        if (other == this)
            return true;
        if (!(other instanceof ScreeningElementId))
            return false;

        ScreeningElementId mi = (ScreeningElementId) other;
        return (ScrEntry == mi.ScrEntry
            || (ScrEntry != null && ScrEntry.equals(mi.ScrEntry)))
               && (screening == mi.screening
                || (screening != null &&
screening.equals(mi.screening)));
    }
        
        public boolean equals(ScreeningElement o)
        {
        
                return this.ScrEntry.equals(o.getElement());
                
        }
        
    public int hashCode() {
        return ((ScrEntry == null) ? 0 : ScrEntry.hashCode())
         ^ ((screening == null) ? 0 : screening.hashCode());
    }   
        
        
        

}////////end screeningelement////////



////////screeningelementid////////
package com.covansys.routingengine.functionalcore.data;

import javax.persistence.*;

public class ScreeningElementId
{

        public Screening screening;

        public String ScrEntry;

        
    public boolean equals(Object other)
    {
        if (other == this)
            return true;
        if (!(other instanceof ScreeningElementId))
            return false;

        ScreeningElementId mi = (ScreeningElementId) other;
        return (ScrEntry == mi.ScrEntry
            || (ScrEntry != null && ScrEntry.equals(mi.ScrEntry)))
               && (screening == mi.screening
                || (screening != null &&
screening.equals(mi.screening)));
    }
    public int hashCode() {
        return ((ScrEntry == null) ? 0 : ScrEntry.hashCode())
         ^ ((screening == null) ? 0 : screening.hashCode());
    } 
}
////////end screeningelementid

-----Original Message-----
From: Pinaki Poddar [mailto:[EMAIL PROTECTED] 
Sent: Thursday, July 19, 2007 7:30 PM
To: [email protected]
Subject: RE: Collection field part of a compound primary key


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_oi
d_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-tf
4110891.html#a11694049
Sent from the OpenJPA Users mailing list archive at Nabble.com.
 
Confidentiality Statement:
 
This message is intended only for the individual or entity to which it is 
addressed. It may contain privileged, confidential information which is exempt 
from disclosure under applicable laws. If you are not the intended recipient, 
please note that you are strictly prohibited from disseminating or distributing 
this information (other than to the intended recipient) or copying this 
information. If you have received this communication in error, please notify us 
immediately by return email.
-----------------------------

Reply via email to