You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by CASERO Jaime <JC...@covansys.com> on 2007/07/19 15:04:10 UTC

Collection field part of a compound primary key

Hello all,

            I'm wondering if OpenJPA supports the creation of a compound
primary key formed by a basic field, and a collection. I have tried
using an "Id class", and using "unique-constraint", but without any
results.

 

            I think this is a very basic feature (A foreign key is part
of the primary key) used frequently in any relational data model. 

 

            Can anyone confirm this feature support?. Any simple example
would be very appreciated.

 

            Thanks in advance, regards.

 

Jaime Casero
 
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.
-----------------------------

RE: Collection field part of a compound primary key

Posted by CASERO Jaime <JC...@covansys.com>.
Ok guys, i have finally notice my error. The key factor is this
line  Pinaki.

  public long  hid;    // same name and type of Holiday's identity

	You were telling all the time, but you know, sometimes happens
that you read a line a million times without noticing the bug.

	Now everything makes much more sense for me. Indeed, the Id
class is used by the Developer to suggets JPA how to build the foreign
key correctly.

	Just look at the sql statements JPA performs to create the
tables:

9017  rountingenginejpa  TRACE  [main] openjpa.jdbc.SQL - <t 20226877,
conn 13178395> executing stmnt 12191562 CREATE TABLE Screening (scrId
INTEGER NOT NULL, PRIMARY KEY (scrId))
9251  rountingenginejpa  TRACE  [main] openjpa.jdbc.SQL - <t 20226877,
conn 13178395> [234 ms] spent
9251  rountingenginejpa  TRACE  [main] openjpa.jdbc.SQL - <t 20226877,
conn 13327669> executing stmnt 24635060 CREATE TABLE ScreeningElement
(ScrEntry VARCHAR(255) NOT NULL, screening_scrId INTEGER NOT NULL,
PRIMARY KEY (ScrEntry, screening_scrId))
9407  rountingenginejpa  TRACE  [main] openjpa.jdbc.SQL - <t 20226877,
conn 13327669> [156 ms] spent
9407  rountingenginejpa  TRACE  [main] openjpa.jdbc.SQL - <t 20226877,
conn 14919969> executing stmnt 2115134 ALTER TABLE ScreeningElement ADD
FOREIGN KEY (screening_scrId) REFERENCES Screening (scrId)

	Know i can clearly see the primary and the foreign key correctly
created.

	Thanks for all, see you.


-----Original Message-----
From: Pinaki Poddar [mailto:ppoddar@apache.org] 
Sent: Thursday, July 19, 2007 7:30 PM
To: users@openjpa.apache.org
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.
-----------------------------

RE: Collection field part of a compound primary key

Posted by CASERO Jaime <JC...@covansys.com>.
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:ppoddar@apache.org] 
Sent: Thursday, July 19, 2007 7:30 PM
To: users@openjpa.apache.org
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.
-----------------------------

RE: Collection field part of a compound primary key

Posted by Pinaki Poddar <pp...@apache.org>.
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.


Re: Collection field part of a compound primary key

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi,

The attachment was removed by the email protection filter.

Can you just put the .java classes into text in the message itself?

Thanks,

Craig

On Jul 19, 2007, at 9:28 AM, CASERO Jaime wrote:

> 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).
>
> 	I can't even set the "Id" annotation to a collection field (i'm
> using a orm.xml file for metadata).
>
> 	Thnaks in adavance
>
> -----Original Message-----
> From: Craig.Russell@Sun.COM [mailto:Craig.Russell@Sun.COM]
> Sent: Thursday, July 19, 2007 6:15 PM
> To: users@openjpa.apache.org
> Cc: ALONSO Fernando
> Subject: Re: Collection field part of a compound primary key
>
> Hi,
>
> Could you please be more specific as to what you tried? I'm a bit
> confused as to whether you're talking about a ToOne relationship or a
> ToMany relationship being used as part of a primary key.
>
> It would help if you would post your class (just the id fields would
> be fine) and the id class you tried.
>
> THanks,
>
> Craig
>
> On Jul 19, 2007, at 6:04 AM, CASERO Jaime wrote:
>
>> Hello all,
>>
>>             I'm wondering if OpenJPA supports the creation of a
>> compound
>> primary key formed by a basic field, and a collection. I have tried
>> using an "Id class", and using "unique-constraint", but without any
>> results.
>>
>>
>>
>>             I think this is a very basic feature (A foreign key is
>> part
>> of the primary key) used frequently in any relational data model.
>>
>>
>>
>>             Can anyone confirm this feature support?. Any simple
>> example
>> would be very appreciated.
>>
>>
>>
>>             Thanks in advance, regards.
>>
>>
>>
>> Jaime Casero
>>
>> 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.
>> -----------------------------
>
> Craig Russell
> Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
> 408 276-5638 mailto:Craig.Russell@sun.com
> P.S. A good JDO? O, Gasp!
>
> 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.
> -----------------------------

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


RE: Collection field part of a compound primary key

Posted by CASERO Jaime <JC...@covansys.com>.
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). 

	I can't even set the "Id" annotation to a collection field (i'm
using a orm.xml file for metadata).

	Thnaks in adavance 

-----Original Message-----
From: Craig.Russell@Sun.COM [mailto:Craig.Russell@Sun.COM] 
Sent: Thursday, July 19, 2007 6:15 PM
To: users@openjpa.apache.org
Cc: ALONSO Fernando
Subject: Re: Collection field part of a compound primary key

Hi,

Could you please be more specific as to what you tried? I'm a bit  
confused as to whether you're talking about a ToOne relationship or a  
ToMany relationship being used as part of a primary key.

It would help if you would post your class (just the id fields would  
be fine) and the id class you tried.

THanks,

Craig

On Jul 19, 2007, at 6:04 AM, CASERO Jaime wrote:

> Hello all,
>
>             I'm wondering if OpenJPA supports the creation of a  
> compound
> primary key formed by a basic field, and a collection. I have tried
> using an "Id class", and using "unique-constraint", but without any
> results.
>
>
>
>             I think this is a very basic feature (A foreign key is  
> part
> of the primary key) used frequently in any relational data model.
>
>
>
>             Can anyone confirm this feature support?. Any simple  
> example
> would be very appreciated.
>
>
>
>             Thanks in advance, regards.
>
>
>
> Jaime Casero
>
> 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.
> -----------------------------

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!
 
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.
-----------------------------

Re: Collection field part of a compound primary key

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi,

Could you please be more specific as to what you tried? I'm a bit  
confused as to whether you're talking about a ToOne relationship or a  
ToMany relationship being used as part of a primary key.

It would help if you would post your class (just the id fields would  
be fine) and the id class you tried.

THanks,

Craig

On Jul 19, 2007, at 6:04 AM, CASERO Jaime wrote:

> Hello all,
>
>             I'm wondering if OpenJPA supports the creation of a  
> compound
> primary key formed by a basic field, and a collection. I have tried
> using an "Id class", and using "unique-constraint", but without any
> results.
>
>
>
>             I think this is a very basic feature (A foreign key is  
> part
> of the primary key) used frequently in any relational data model.
>
>
>
>             Can anyone confirm this feature support?. Any simple  
> example
> would be very appreciated.
>
>
>
>             Thanks in advance, regards.
>
>
>
> Jaime Casero
>
> 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.
> -----------------------------

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!