persistence@glassfish.java.net

Re: "Invalid composite primary key specification"

From: Jeffrey Blattman <jeffrey.blattman_at_gmail.com>
Date: Thu, 02 Aug 2007 16:03:23 -0700

thanks markus,

this gets me further. the complexity is this, i essentially have three
entities

   1. Page
   2. Portlet
   3. Preferenence

simple right? a page contains a portlet contains preferences. a delete /
update / etc of Page should affect all its portlets, and a delete /
update / etc of a portlet should affect its preferences. each of these
has a name.

to model the containment, Portlet's ID is the page name + it's own name
(PortletId). that one is simple. however, when we go a level deeper to
Preference, Preference's ID is the Portlet's ID + it's own name
(PreferenceId).

how do i model that? PortletId is *not* a simple type, so I can't use it
as an @Id field in Preference. how do i define Preference's ID fields?

i tried what you suggested in Preference

/ @Id
    @Column(nullable = false, name="PORTLET_ID")
    private String portletId = null;
   
    @ManyToOne(optional = false, cascade = CascadeType.ALL)
    @Column(insertable=false, updatable=false, nullable=false,
name="PORTLET_ID")
    private Portlet portlet;
/
but i get this:

/Exception Description: Entity class [class
com.sun.portal.pom.Preference] must use a @JoinColumn instead of @Column
to map its relationship attribute [entity].
/
when i just make it a @JoinColumn instead,

/ @ManyToOne(optional = false, cascade = CascadeType.ALL)
    @Column(insertable=false, updatable=false, nullable=false,
name="PORTLET_ID")
    @XmlTransient
    private Portlet portlet;

/i get this ...

/Exception Description: The @JoinColumns on the annotated element
[private com.sun.portal.pom.Entity com.sun.portal.pom.Preference.entity]
from the entity class [class com.sun.portal.pom.Preference] is
incomplete. When the source entity class uses a composite primary key, a
@JoinColumn must be specified for each join column using the
@JoinColumns. Both the name and the referenceColumnName elements must be
specified in each such @JoinColumn.
/
what am i supposed to use for the join columns? since PortletId
encapsulates both the Page name and Portlet name, why does it want to to
specify more than that?

i feel like i must be making this more complicated than it needs to be.
all i want is to model some objects containing other objects where all
operations cascade.

?

Markus Fuchs wrote:
> Hi Jeffrey,
>
> Entities should not use relationship fields as part of the primary
> key. The JPA spec says (please see 2.1.4.):
>
> The primary key (or field or property of a composite primary key)
> should be one of the following types:
> any Java primitive type; any primitive wrapper type; java.lang.String;
> java.util.Date;
> java.sql.Date.
>
> Try changing:
>
> public abstract class Entity implements Serializable {
> @Id
> @Column (nullable = false, name="ENTITY_NAME")
> @XmlAttribute
> private String name;
>
> @Id
> @Column (nullable = false, name="PAGE_ID")
> private String pageId;
>
>
> @Column (nullable = false, name="PAGE_ID")
> @ManyToOne (cascade = CascadeType.ALL, insertable=false, updatable=false)
> private Page page = null;
> ...
> }
>
> public class EntityId implements Serializable {
> private String name;
> private String pageId;
> ...
> }
>
>
> -- markus.
>
> Entity
> Jeffrey Blattman wrote:
>> hi,
>>
>> 2.0-b56, i'm seeing this ...
>>
>> /Exception Description: Invalid composite primary key specification.
>> The names of the primary key fields or properties in the primary key
>> class [com.sun.portal.pom.EntityId] and those of the entity bean
>> class [class com.sun.portal.pom.Entity] must correspond and their
>> types must be the same. Also, ensure that you have specified id
>> elements for the corresponding attributes in XML and/or an @Id on the
>> corresponding fields or properties of the entity class.
>> /
>> i've attached the two classes. Entity has IDs /name/ and /page/, and
>> EntityId has fields /name/ and /page/. same name, same type. i can
>> zip the maven2 project so the problem can be reproduced, if that helps.
>>
>> what am i missing?
>>
>> thanks.