You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Mansour Al Akeel <ma...@gmail.com> on 2014/01/30 06:48:04 UTC

Composite Primary Key with constraints

Hello all,

I need to create a primary key in my tables, and set constrains on
them. So I have few tables:

1- Geo
2- GeoAssociation

For the GeoAssociation, I need a primary key GeoAssociationPK. The PK
has a field, that "HAS" to be a geo_id. So here's the definition I
have for GeoAssociation:



@Embeddable
public class GeoAssociationPK implements Serializable {

    private String strId;

    private Geo geo;

    @Column(name = "str_id")
    public String getMessage() {
        return strId;
    }

    public void setMessage(String message) {
        this.strId = message;
    }

    @ManyToOne(optional = false)
    @Column(name = "geo_id")
    public Geo getGeo() {
        return geo;
    }

    public void setGeo(Geo geoFrom) {
        this.geo = geoFrom;
    }

    @Override
    public boolean equals(Object obj) {
        throw new UnsupportedOperationException("Not implemented");
    }

    @Override
    public int hashCode() {
        throw new UnsupportedOperationException("Not implemented");
    }

}



This is how I define the GeoAssociation class:

@Entity
@Access(AccessType.PROPERTY)

public class GeoAssoc {

    @EmbeddedId
    private GeoAssociationPK id = new GeoAssociationPK();

    private GeoAssocType type;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "type")
    public GeoAssocType getType() {
        return type;
    }

    public void setType(GeoAssocType value) {
        this.type = value;
    }

    public Geo getGeoFrom() {
        return this.id.getGeo();
    }

    @Override
    public boolean equals(Object obj) {
        throw new UnsupportedOperationException("Not implemented");
    }

    @Override
    public int hashCode() {
        throw new UnsupportedOperationException("Not implemented");
    }

}


The error I am getting when building with maven enhancer is:

The id class specified by type "class entities.geo.GeoAssoc" does not
match the primary key fields of the class.  Make sure your identity
class has the same primary keys as your persistent type, including pk
field types. Mismatched property: "geo" -> [Help 1]

I don't understand the error message or how to solve it. What I need
is to be able to create and persist entity without having to
explicitly create the PK.

For example :
GeoAssociation gs = new GeoAssociation();
gs.setGeo(myGeo);
gs.setStrId(someString);


My question is how to achieve this, and resolve this error message ?


Thank you in advance.

Re: Composite Primary Key with constraints

Posted by Albert Lee <al...@gmail.com>.
Annotation directs JPA provider to figure out what needs to be generated
for the data model.  If there is no intent (in this particular use case, it
does not make any sense anyway) for the relationship, I suggest these
relationship to be removed.   In some relationship, id may be used as the
key to extract collection.  I am not saying this is the cause of the
current problem but would be a start to remove potential cause for further
problem determination. You never know, it may be the cause.

Albert


On Sat, Feb 1, 2014 at 10:15 AM, Mansour Al Akeel <mansour.alakeel@gmail.com
> wrote:

> Albert,
> No there is no reason. I am just prototyping, and had these annotation
> there.
> I don't understand what does it have to do with generating the ID
> column in the DB.
>
> Thank you.
>
>
> On Fri, Jan 31, 2014 at 9:57 AM, Albert Lee <al...@gmail.com> wrote:
> > Is there a reason why you have a @ManyToOne relationship in
> >
> > public class GeoAssociationPK implements Serializable {
> >     private Geo fromGeo;
> >     private Geo toGeo;
> >
> >     @ManyToOne(optional = false)   <<<<<<
> >     public Geo getFromGeo() {
> >         return fromGeo;
> >     }
> >
> >     @ManyToOne(optional = false)  <<<<<<
> >     public Geo getToGeo() {
> >         return toGeo;
> >     }
> >
> >
> > On Thu, Jan 30, 2014 at 10:41 PM, Mansour Al Akeel <
> > mansour.alakeel@gmail.com> wrote:
> >
> >> Kevin,
> >>
> >> Thank you a lot for your time. In fact I fixed the getters and
> >> setters, and the issue is gone. Now, the issue is with the id field
> >> being generated in the SQL tables.
> >> Here's my classes:
> >>
> >>
> >> @Entity
> >> @Access(AccessType.PROPERTY)
> >> public class GeoAssoc {
> >>
> >>     @EmbeddedId
> >>     private GeoAssociationPK id = new GeoAssociationPK();
> >>
> >>     private GeoAssocType type;
> >>
> >>     @ManyToOne(fetch = FetchType.LAZY)
> >>     @JoinColumn(name = "type")
> >>     public GeoAssocType getType() {
> >>         return type;
> >>     }
> >>
> >>     public void setType(GeoAssocType value) {
> >>         this.type = value;
> >>     }
> >>
> >>     @Column(name = "from_geo_id")
> >>     public Geo getFromGeo() {
> >>         return this.id.getFromGeo();
> >>     }
> >>
> >>     public void setFromGeo(Geo fromGeo) {
> >>         this.id.setFromGeo(fromGeo);
> >>     }
> >>
> >>     @Column(name = "to_geo_id")
> >>     public Geo getToGeo() {
> >>         return this.id.getToGeo();
> >>     }
> >>
> >>     public void setToGeo(Geo toGeo) {
> >>         this.id.setToGeo(toGeo);
> >>     }
> >>
> >>     @Override
> >>     public boolean equals(Object obj) {
> >>         throw new UnsupportedOperationException("Not implemented");
> >>     }
> >>
> >>     @Override
> >>     public int hashCode() {
> >>         throw new UnsupportedOperationException("Not implemented");
> >>     }
> >>
> >> }
> >>
> >>
> >>
> >>
> >> @Embeddable
> >> public class GeoAssociationPK implements Serializable {
> >>
> >>     private Geo fromGeo;
> >>
> >>     private Geo toGeo;
> >>
> >>
> >>     @ManyToOne(optional = false)
> >>     public Geo getFromGeo() {
> >>         return fromGeo;
> >>     }
> >>
> >>     public void setFromGeo(Geo fromGeo) {
> >>         this.fromGeo = fromGeo;
> >>     }
> >>
> >>     @ManyToOne(optional = false)
> >>     public Geo getToGeo() {
> >>         return toGeo;
> >>     }
> >>
> >>     public void setToGeo(Geo toGeo) {
> >>         this.toGeo = toGeo;
> >>     }
> >>
> >>     @Override
> >>     public boolean equals(Object obj) {
> >>         throw new UnsupportedOperationException("Not implemented");
> >>     }
> >>
> >>     @Override
> >>     public int hashCode() {
> >>         throw new UnsupportedOperationException("Not implemented");
> >>     }
> >> }
> >>
> >>
> >> The generated table has 4 fields (type, from_geo_id , to_geo_id , and
> >> id). The last columns "id" is not needed, and don't know how to tell
> >> the persistence classes to ignore it and not to generate it in the DB.
> >> This is why I have PROPERTY access type, but still it's generated. Any
> >> advice about how to do this ??
> >>
> >>
> >> Thank you.
> >>
> >> On Thu, Jan 30, 2014 at 9:53 AM, Kevin Sutter <kw...@gmail.com>
> wrote:
> >> > Hi Mansour,
> >> > I agree that the message is messed up a bit...  This particular
> message
> >> is
> >> > used in a few different locations in the code.  If you have additional
> >> > context about when this is received (call stack, persistent operation,
> >> > etc), it would help with identifying the specific usage and get that
> >> > cleaned up.
> >> >
> >> > Looking at your example, a couple of things jump out...
> >> >
> >> > 1)  In your PK class, your getter/setter methods don't match the strId
> >> > attribute (get/setMessage?).  These names need to follow standard java
> >> bean
> >> > conventions and need to match up.
> >> >
> >> > 2)  In your GeoAssociation class...  It looks like you want the
> ability
> >> to
> >> > setGeo and setStrId, but I don't see those methods defined.  You have
> >> them
> >> > defined (sort of, per my first comment) in your PK class.  But, you
> don't
> >> > have them defined in your GeoAssoc class.
> >> >
> >> > That's all I've got at this point.
> >> > Kevin
> >> >
> >> >
> >> > On Wed, Jan 29, 2014 at 11:48 PM, Mansour Al Akeel <
> >> > mansour.alakeel@gmail.com> wrote:
> >> >
> >> >> Hello all,
> >> >>
> >> >> I need to create a primary key in my tables, and set constrains on
> >> >> them. So I have few tables:
> >> >>
> >> >> 1- Geo
> >> >> 2- GeoAssociation
> >> >>
> >> >> For the GeoAssociation, I need a primary key GeoAssociationPK. The PK
> >> >> has a field, that "HAS" to be a geo_id. So here's the definition I
> >> >> have for GeoAssociation:
> >> >>
> >> >>
> >> >>
> >> >> @Embeddable
> >> >> public class GeoAssociationPK implements Serializable {
> >> >>
> >> >>     private String strId;
> >> >>
> >> >>     private Geo geo;
> >> >>
> >> >>     @Column(name = "str_id")
> >> >>     public String getMessage() {
> >> >>         return strId;
> >> >>     }
> >> >>
> >> >>     public void setMessage(String message) {
> >> >>         this.strId = message;
> >> >>     }
> >> >>
> >> >>     @ManyToOne(optional = false)
> >> >>     @Column(name = "geo_id")
> >> >>     public Geo getGeo() {
> >> >>         return geo;
> >> >>     }
> >> >>
> >> >>     public void setGeo(Geo geoFrom) {
> >> >>         this.geo = geoFrom;
> >> >>     }
> >> >>
> >> >>     @Override
> >> >>     public boolean equals(Object obj) {
> >> >>         throw new UnsupportedOperationException("Not implemented");
> >> >>     }
> >> >>
> >> >>     @Override
> >> >>     public int hashCode() {
> >> >>         throw new UnsupportedOperationException("Not implemented");
> >> >>     }
> >> >>
> >> >> }
> >> >>
> >> >>
> >> >>
> >> >> This is how I define the GeoAssociation class:
> >> >>
> >> >> @Entity
> >> >> @Access(AccessType.PROPERTY)
> >> >>
> >> >> public class GeoAssoc {
> >> >>
> >> >>     @EmbeddedId
> >> >>     private GeoAssociationPK id = new GeoAssociationPK();
> >> >>
> >> >>     private GeoAssocType type;
> >> >>
> >> >>     @ManyToOne(fetch = FetchType.LAZY)
> >> >>     @JoinColumn(name = "type")
> >> >>     public GeoAssocType getType() {
> >> >>         return type;
> >> >>     }
> >> >>
> >> >>     public void setType(GeoAssocType value) {
> >> >>         this.type = value;
> >> >>     }
> >> >>
> >> >>     public Geo getGeoFrom() {
> >> >>         return this.id.getGeo();
> >> >>     }
> >> >>
> >> >>     @Override
> >> >>     public boolean equals(Object obj) {
> >> >>         throw new UnsupportedOperationException("Not implemented");
> >> >>     }
> >> >>
> >> >>     @Override
> >> >>     public int hashCode() {
> >> >>         throw new UnsupportedOperationException("Not implemented");
> >> >>     }
> >> >>
> >> >> }
> >> >>
> >> >>
> >> >> The error I am getting when building with maven enhancer is:
> >> >>
> >> >> The id class specified by type "class entities.geo.GeoAssoc" does not
> >> >> match the primary key fields of the class.  Make sure your identity
> >> >> class has the same primary keys as your persistent type, including pk
> >> >> field types. Mismatched property: "geo" -> [Help 1]
> >> >>
> >> >> I don't understand the error message or how to solve it. What I need
> >> >> is to be able to create and persist entity without having to
> >> >> explicitly create the PK.
> >> >>
> >> >> For example :
> >> >> GeoAssociation gs = new GeoAssociation();
> >> >> gs.setGeo(myGeo);
> >> >> gs.setStrId(someString);
> >> >>
> >> >>
> >> >> My question is how to achieve this, and resolve this error message ?
> >> >>
> >> >>
> >> >> Thank you in advance.
> >> >>
> >>
> >
> >
> >
> > --
> > Albert Lee.
>



-- 
Albert Lee.

Re: Composite Primary Key with constraints

Posted by Mansour Al Akeel <ma...@gmail.com>.
Albert,
No there is no reason. I am just prototyping, and had these annotation there.
I don't understand what does it have to do with generating the ID
column in the DB.

Thank you.


On Fri, Jan 31, 2014 at 9:57 AM, Albert Lee <al...@gmail.com> wrote:
> Is there a reason why you have a @ManyToOne relationship in
>
> public class GeoAssociationPK implements Serializable {
>     private Geo fromGeo;
>     private Geo toGeo;
>
>     @ManyToOne(optional = false)   <<<<<<
>     public Geo getFromGeo() {
>         return fromGeo;
>     }
>
>     @ManyToOne(optional = false)  <<<<<<
>     public Geo getToGeo() {
>         return toGeo;
>     }
>
>
> On Thu, Jan 30, 2014 at 10:41 PM, Mansour Al Akeel <
> mansour.alakeel@gmail.com> wrote:
>
>> Kevin,
>>
>> Thank you a lot for your time. In fact I fixed the getters and
>> setters, and the issue is gone. Now, the issue is with the id field
>> being generated in the SQL tables.
>> Here's my classes:
>>
>>
>> @Entity
>> @Access(AccessType.PROPERTY)
>> public class GeoAssoc {
>>
>>     @EmbeddedId
>>     private GeoAssociationPK id = new GeoAssociationPK();
>>
>>     private GeoAssocType type;
>>
>>     @ManyToOne(fetch = FetchType.LAZY)
>>     @JoinColumn(name = "type")
>>     public GeoAssocType getType() {
>>         return type;
>>     }
>>
>>     public void setType(GeoAssocType value) {
>>         this.type = value;
>>     }
>>
>>     @Column(name = "from_geo_id")
>>     public Geo getFromGeo() {
>>         return this.id.getFromGeo();
>>     }
>>
>>     public void setFromGeo(Geo fromGeo) {
>>         this.id.setFromGeo(fromGeo);
>>     }
>>
>>     @Column(name = "to_geo_id")
>>     public Geo getToGeo() {
>>         return this.id.getToGeo();
>>     }
>>
>>     public void setToGeo(Geo toGeo) {
>>         this.id.setToGeo(toGeo);
>>     }
>>
>>     @Override
>>     public boolean equals(Object obj) {
>>         throw new UnsupportedOperationException("Not implemented");
>>     }
>>
>>     @Override
>>     public int hashCode() {
>>         throw new UnsupportedOperationException("Not implemented");
>>     }
>>
>> }
>>
>>
>>
>>
>> @Embeddable
>> public class GeoAssociationPK implements Serializable {
>>
>>     private Geo fromGeo;
>>
>>     private Geo toGeo;
>>
>>
>>     @ManyToOne(optional = false)
>>     public Geo getFromGeo() {
>>         return fromGeo;
>>     }
>>
>>     public void setFromGeo(Geo fromGeo) {
>>         this.fromGeo = fromGeo;
>>     }
>>
>>     @ManyToOne(optional = false)
>>     public Geo getToGeo() {
>>         return toGeo;
>>     }
>>
>>     public void setToGeo(Geo toGeo) {
>>         this.toGeo = toGeo;
>>     }
>>
>>     @Override
>>     public boolean equals(Object obj) {
>>         throw new UnsupportedOperationException("Not implemented");
>>     }
>>
>>     @Override
>>     public int hashCode() {
>>         throw new UnsupportedOperationException("Not implemented");
>>     }
>> }
>>
>>
>> The generated table has 4 fields (type, from_geo_id , to_geo_id , and
>> id). The last columns "id" is not needed, and don't know how to tell
>> the persistence classes to ignore it and not to generate it in the DB.
>> This is why I have PROPERTY access type, but still it's generated. Any
>> advice about how to do this ??
>>
>>
>> Thank you.
>>
>> On Thu, Jan 30, 2014 at 9:53 AM, Kevin Sutter <kw...@gmail.com> wrote:
>> > Hi Mansour,
>> > I agree that the message is messed up a bit...  This particular message
>> is
>> > used in a few different locations in the code.  If you have additional
>> > context about when this is received (call stack, persistent operation,
>> > etc), it would help with identifying the specific usage and get that
>> > cleaned up.
>> >
>> > Looking at your example, a couple of things jump out...
>> >
>> > 1)  In your PK class, your getter/setter methods don't match the strId
>> > attribute (get/setMessage?).  These names need to follow standard java
>> bean
>> > conventions and need to match up.
>> >
>> > 2)  In your GeoAssociation class...  It looks like you want the ability
>> to
>> > setGeo and setStrId, but I don't see those methods defined.  You have
>> them
>> > defined (sort of, per my first comment) in your PK class.  But, you don't
>> > have them defined in your GeoAssoc class.
>> >
>> > That's all I've got at this point.
>> > Kevin
>> >
>> >
>> > On Wed, Jan 29, 2014 at 11:48 PM, Mansour Al Akeel <
>> > mansour.alakeel@gmail.com> wrote:
>> >
>> >> Hello all,
>> >>
>> >> I need to create a primary key in my tables, and set constrains on
>> >> them. So I have few tables:
>> >>
>> >> 1- Geo
>> >> 2- GeoAssociation
>> >>
>> >> For the GeoAssociation, I need a primary key GeoAssociationPK. The PK
>> >> has a field, that "HAS" to be a geo_id. So here's the definition I
>> >> have for GeoAssociation:
>> >>
>> >>
>> >>
>> >> @Embeddable
>> >> public class GeoAssociationPK implements Serializable {
>> >>
>> >>     private String strId;
>> >>
>> >>     private Geo geo;
>> >>
>> >>     @Column(name = "str_id")
>> >>     public String getMessage() {
>> >>         return strId;
>> >>     }
>> >>
>> >>     public void setMessage(String message) {
>> >>         this.strId = message;
>> >>     }
>> >>
>> >>     @ManyToOne(optional = false)
>> >>     @Column(name = "geo_id")
>> >>     public Geo getGeo() {
>> >>         return geo;
>> >>     }
>> >>
>> >>     public void setGeo(Geo geoFrom) {
>> >>         this.geo = geoFrom;
>> >>     }
>> >>
>> >>     @Override
>> >>     public boolean equals(Object obj) {
>> >>         throw new UnsupportedOperationException("Not implemented");
>> >>     }
>> >>
>> >>     @Override
>> >>     public int hashCode() {
>> >>         throw new UnsupportedOperationException("Not implemented");
>> >>     }
>> >>
>> >> }
>> >>
>> >>
>> >>
>> >> This is how I define the GeoAssociation class:
>> >>
>> >> @Entity
>> >> @Access(AccessType.PROPERTY)
>> >>
>> >> public class GeoAssoc {
>> >>
>> >>     @EmbeddedId
>> >>     private GeoAssociationPK id = new GeoAssociationPK();
>> >>
>> >>     private GeoAssocType type;
>> >>
>> >>     @ManyToOne(fetch = FetchType.LAZY)
>> >>     @JoinColumn(name = "type")
>> >>     public GeoAssocType getType() {
>> >>         return type;
>> >>     }
>> >>
>> >>     public void setType(GeoAssocType value) {
>> >>         this.type = value;
>> >>     }
>> >>
>> >>     public Geo getGeoFrom() {
>> >>         return this.id.getGeo();
>> >>     }
>> >>
>> >>     @Override
>> >>     public boolean equals(Object obj) {
>> >>         throw new UnsupportedOperationException("Not implemented");
>> >>     }
>> >>
>> >>     @Override
>> >>     public int hashCode() {
>> >>         throw new UnsupportedOperationException("Not implemented");
>> >>     }
>> >>
>> >> }
>> >>
>> >>
>> >> The error I am getting when building with maven enhancer is:
>> >>
>> >> The id class specified by type "class entities.geo.GeoAssoc" does not
>> >> match the primary key fields of the class.  Make sure your identity
>> >> class has the same primary keys as your persistent type, including pk
>> >> field types. Mismatched property: "geo" -> [Help 1]
>> >>
>> >> I don't understand the error message or how to solve it. What I need
>> >> is to be able to create and persist entity without having to
>> >> explicitly create the PK.
>> >>
>> >> For example :
>> >> GeoAssociation gs = new GeoAssociation();
>> >> gs.setGeo(myGeo);
>> >> gs.setStrId(someString);
>> >>
>> >>
>> >> My question is how to achieve this, and resolve this error message ?
>> >>
>> >>
>> >> Thank you in advance.
>> >>
>>
>
>
>
> --
> Albert Lee.

Re: Composite Primary Key with constraints

Posted by Albert Lee <al...@gmail.com>.
Is there a reason why you have a @ManyToOne relationship in

public class GeoAssociationPK implements Serializable {
    private Geo fromGeo;
    private Geo toGeo;

    @ManyToOne(optional = false)   <<<<<<
    public Geo getFromGeo() {
        return fromGeo;
    }

    @ManyToOne(optional = false)  <<<<<<
    public Geo getToGeo() {
        return toGeo;
    }


On Thu, Jan 30, 2014 at 10:41 PM, Mansour Al Akeel <
mansour.alakeel@gmail.com> wrote:

> Kevin,
>
> Thank you a lot for your time. In fact I fixed the getters and
> setters, and the issue is gone. Now, the issue is with the id field
> being generated in the SQL tables.
> Here's my classes:
>
>
> @Entity
> @Access(AccessType.PROPERTY)
> public class GeoAssoc {
>
>     @EmbeddedId
>     private GeoAssociationPK id = new GeoAssociationPK();
>
>     private GeoAssocType type;
>
>     @ManyToOne(fetch = FetchType.LAZY)
>     @JoinColumn(name = "type")
>     public GeoAssocType getType() {
>         return type;
>     }
>
>     public void setType(GeoAssocType value) {
>         this.type = value;
>     }
>
>     @Column(name = "from_geo_id")
>     public Geo getFromGeo() {
>         return this.id.getFromGeo();
>     }
>
>     public void setFromGeo(Geo fromGeo) {
>         this.id.setFromGeo(fromGeo);
>     }
>
>     @Column(name = "to_geo_id")
>     public Geo getToGeo() {
>         return this.id.getToGeo();
>     }
>
>     public void setToGeo(Geo toGeo) {
>         this.id.setToGeo(toGeo);
>     }
>
>     @Override
>     public boolean equals(Object obj) {
>         throw new UnsupportedOperationException("Not implemented");
>     }
>
>     @Override
>     public int hashCode() {
>         throw new UnsupportedOperationException("Not implemented");
>     }
>
> }
>
>
>
>
> @Embeddable
> public class GeoAssociationPK implements Serializable {
>
>     private Geo fromGeo;
>
>     private Geo toGeo;
>
>
>     @ManyToOne(optional = false)
>     public Geo getFromGeo() {
>         return fromGeo;
>     }
>
>     public void setFromGeo(Geo fromGeo) {
>         this.fromGeo = fromGeo;
>     }
>
>     @ManyToOne(optional = false)
>     public Geo getToGeo() {
>         return toGeo;
>     }
>
>     public void setToGeo(Geo toGeo) {
>         this.toGeo = toGeo;
>     }
>
>     @Override
>     public boolean equals(Object obj) {
>         throw new UnsupportedOperationException("Not implemented");
>     }
>
>     @Override
>     public int hashCode() {
>         throw new UnsupportedOperationException("Not implemented");
>     }
> }
>
>
> The generated table has 4 fields (type, from_geo_id , to_geo_id , and
> id). The last columns "id" is not needed, and don't know how to tell
> the persistence classes to ignore it and not to generate it in the DB.
> This is why I have PROPERTY access type, but still it's generated. Any
> advice about how to do this ??
>
>
> Thank you.
>
> On Thu, Jan 30, 2014 at 9:53 AM, Kevin Sutter <kw...@gmail.com> wrote:
> > Hi Mansour,
> > I agree that the message is messed up a bit...  This particular message
> is
> > used in a few different locations in the code.  If you have additional
> > context about when this is received (call stack, persistent operation,
> > etc), it would help with identifying the specific usage and get that
> > cleaned up.
> >
> > Looking at your example, a couple of things jump out...
> >
> > 1)  In your PK class, your getter/setter methods don't match the strId
> > attribute (get/setMessage?).  These names need to follow standard java
> bean
> > conventions and need to match up.
> >
> > 2)  In your GeoAssociation class...  It looks like you want the ability
> to
> > setGeo and setStrId, but I don't see those methods defined.  You have
> them
> > defined (sort of, per my first comment) in your PK class.  But, you don't
> > have them defined in your GeoAssoc class.
> >
> > That's all I've got at this point.
> > Kevin
> >
> >
> > On Wed, Jan 29, 2014 at 11:48 PM, Mansour Al Akeel <
> > mansour.alakeel@gmail.com> wrote:
> >
> >> Hello all,
> >>
> >> I need to create a primary key in my tables, and set constrains on
> >> them. So I have few tables:
> >>
> >> 1- Geo
> >> 2- GeoAssociation
> >>
> >> For the GeoAssociation, I need a primary key GeoAssociationPK. The PK
> >> has a field, that "HAS" to be a geo_id. So here's the definition I
> >> have for GeoAssociation:
> >>
> >>
> >>
> >> @Embeddable
> >> public class GeoAssociationPK implements Serializable {
> >>
> >>     private String strId;
> >>
> >>     private Geo geo;
> >>
> >>     @Column(name = "str_id")
> >>     public String getMessage() {
> >>         return strId;
> >>     }
> >>
> >>     public void setMessage(String message) {
> >>         this.strId = message;
> >>     }
> >>
> >>     @ManyToOne(optional = false)
> >>     @Column(name = "geo_id")
> >>     public Geo getGeo() {
> >>         return geo;
> >>     }
> >>
> >>     public void setGeo(Geo geoFrom) {
> >>         this.geo = geoFrom;
> >>     }
> >>
> >>     @Override
> >>     public boolean equals(Object obj) {
> >>         throw new UnsupportedOperationException("Not implemented");
> >>     }
> >>
> >>     @Override
> >>     public int hashCode() {
> >>         throw new UnsupportedOperationException("Not implemented");
> >>     }
> >>
> >> }
> >>
> >>
> >>
> >> This is how I define the GeoAssociation class:
> >>
> >> @Entity
> >> @Access(AccessType.PROPERTY)
> >>
> >> public class GeoAssoc {
> >>
> >>     @EmbeddedId
> >>     private GeoAssociationPK id = new GeoAssociationPK();
> >>
> >>     private GeoAssocType type;
> >>
> >>     @ManyToOne(fetch = FetchType.LAZY)
> >>     @JoinColumn(name = "type")
> >>     public GeoAssocType getType() {
> >>         return type;
> >>     }
> >>
> >>     public void setType(GeoAssocType value) {
> >>         this.type = value;
> >>     }
> >>
> >>     public Geo getGeoFrom() {
> >>         return this.id.getGeo();
> >>     }
> >>
> >>     @Override
> >>     public boolean equals(Object obj) {
> >>         throw new UnsupportedOperationException("Not implemented");
> >>     }
> >>
> >>     @Override
> >>     public int hashCode() {
> >>         throw new UnsupportedOperationException("Not implemented");
> >>     }
> >>
> >> }
> >>
> >>
> >> The error I am getting when building with maven enhancer is:
> >>
> >> The id class specified by type "class entities.geo.GeoAssoc" does not
> >> match the primary key fields of the class.  Make sure your identity
> >> class has the same primary keys as your persistent type, including pk
> >> field types. Mismatched property: "geo" -> [Help 1]
> >>
> >> I don't understand the error message or how to solve it. What I need
> >> is to be able to create and persist entity without having to
> >> explicitly create the PK.
> >>
> >> For example :
> >> GeoAssociation gs = new GeoAssociation();
> >> gs.setGeo(myGeo);
> >> gs.setStrId(someString);
> >>
> >>
> >> My question is how to achieve this, and resolve this error message ?
> >>
> >>
> >> Thank you in advance.
> >>
>



-- 
Albert Lee.

Re: Composite Primary Key with constraints

Posted by Mansour Al Akeel <ma...@gmail.com>.
Kevin,

Thank you a lot for your time. In fact I fixed the getters and
setters, and the issue is gone. Now, the issue is with the id field
being generated in the SQL tables.
Here's my classes:


@Entity
@Access(AccessType.PROPERTY)
public class GeoAssoc {

    @EmbeddedId
    private GeoAssociationPK id = new GeoAssociationPK();

    private GeoAssocType type;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "type")
    public GeoAssocType getType() {
        return type;
    }

    public void setType(GeoAssocType value) {
        this.type = value;
    }

    @Column(name = "from_geo_id")
    public Geo getFromGeo() {
        return this.id.getFromGeo();
    }

    public void setFromGeo(Geo fromGeo) {
        this.id.setFromGeo(fromGeo);
    }

    @Column(name = "to_geo_id")
    public Geo getToGeo() {
        return this.id.getToGeo();
    }

    public void setToGeo(Geo toGeo) {
        this.id.setToGeo(toGeo);
    }

    @Override
    public boolean equals(Object obj) {
        throw new UnsupportedOperationException("Not implemented");
    }

    @Override
    public int hashCode() {
        throw new UnsupportedOperationException("Not implemented");
    }

}




@Embeddable
public class GeoAssociationPK implements Serializable {

    private Geo fromGeo;

    private Geo toGeo;


    @ManyToOne(optional = false)
    public Geo getFromGeo() {
        return fromGeo;
    }

    public void setFromGeo(Geo fromGeo) {
        this.fromGeo = fromGeo;
    }

    @ManyToOne(optional = false)
    public Geo getToGeo() {
        return toGeo;
    }

    public void setToGeo(Geo toGeo) {
        this.toGeo = toGeo;
    }

    @Override
    public boolean equals(Object obj) {
        throw new UnsupportedOperationException("Not implemented");
    }

    @Override
    public int hashCode() {
        throw new UnsupportedOperationException("Not implemented");
    }
}


The generated table has 4 fields (type, from_geo_id , to_geo_id , and
id). The last columns "id" is not needed, and don't know how to tell
the persistence classes to ignore it and not to generate it in the DB.
This is why I have PROPERTY access type, but still it's generated. Any
advice about how to do this ??


Thank you.

On Thu, Jan 30, 2014 at 9:53 AM, Kevin Sutter <kw...@gmail.com> wrote:
> Hi Mansour,
> I agree that the message is messed up a bit...  This particular message is
> used in a few different locations in the code.  If you have additional
> context about when this is received (call stack, persistent operation,
> etc), it would help with identifying the specific usage and get that
> cleaned up.
>
> Looking at your example, a couple of things jump out...
>
> 1)  In your PK class, your getter/setter methods don't match the strId
> attribute (get/setMessage?).  These names need to follow standard java bean
> conventions and need to match up.
>
> 2)  In your GeoAssociation class...  It looks like you want the ability to
> setGeo and setStrId, but I don't see those methods defined.  You have them
> defined (sort of, per my first comment) in your PK class.  But, you don't
> have them defined in your GeoAssoc class.
>
> That's all I've got at this point.
> Kevin
>
>
> On Wed, Jan 29, 2014 at 11:48 PM, Mansour Al Akeel <
> mansour.alakeel@gmail.com> wrote:
>
>> Hello all,
>>
>> I need to create a primary key in my tables, and set constrains on
>> them. So I have few tables:
>>
>> 1- Geo
>> 2- GeoAssociation
>>
>> For the GeoAssociation, I need a primary key GeoAssociationPK. The PK
>> has a field, that "HAS" to be a geo_id. So here's the definition I
>> have for GeoAssociation:
>>
>>
>>
>> @Embeddable
>> public class GeoAssociationPK implements Serializable {
>>
>>     private String strId;
>>
>>     private Geo geo;
>>
>>     @Column(name = "str_id")
>>     public String getMessage() {
>>         return strId;
>>     }
>>
>>     public void setMessage(String message) {
>>         this.strId = message;
>>     }
>>
>>     @ManyToOne(optional = false)
>>     @Column(name = "geo_id")
>>     public Geo getGeo() {
>>         return geo;
>>     }
>>
>>     public void setGeo(Geo geoFrom) {
>>         this.geo = geoFrom;
>>     }
>>
>>     @Override
>>     public boolean equals(Object obj) {
>>         throw new UnsupportedOperationException("Not implemented");
>>     }
>>
>>     @Override
>>     public int hashCode() {
>>         throw new UnsupportedOperationException("Not implemented");
>>     }
>>
>> }
>>
>>
>>
>> This is how I define the GeoAssociation class:
>>
>> @Entity
>> @Access(AccessType.PROPERTY)
>>
>> public class GeoAssoc {
>>
>>     @EmbeddedId
>>     private GeoAssociationPK id = new GeoAssociationPK();
>>
>>     private GeoAssocType type;
>>
>>     @ManyToOne(fetch = FetchType.LAZY)
>>     @JoinColumn(name = "type")
>>     public GeoAssocType getType() {
>>         return type;
>>     }
>>
>>     public void setType(GeoAssocType value) {
>>         this.type = value;
>>     }
>>
>>     public Geo getGeoFrom() {
>>         return this.id.getGeo();
>>     }
>>
>>     @Override
>>     public boolean equals(Object obj) {
>>         throw new UnsupportedOperationException("Not implemented");
>>     }
>>
>>     @Override
>>     public int hashCode() {
>>         throw new UnsupportedOperationException("Not implemented");
>>     }
>>
>> }
>>
>>
>> The error I am getting when building with maven enhancer is:
>>
>> The id class specified by type "class entities.geo.GeoAssoc" does not
>> match the primary key fields of the class.  Make sure your identity
>> class has the same primary keys as your persistent type, including pk
>> field types. Mismatched property: "geo" -> [Help 1]
>>
>> I don't understand the error message or how to solve it. What I need
>> is to be able to create and persist entity without having to
>> explicitly create the PK.
>>
>> For example :
>> GeoAssociation gs = new GeoAssociation();
>> gs.setGeo(myGeo);
>> gs.setStrId(someString);
>>
>>
>> My question is how to achieve this, and resolve this error message ?
>>
>>
>> Thank you in advance.
>>

Re: Composite Primary Key with constraints

Posted by Kevin Sutter <kw...@gmail.com>.
Hi Mansour,
I agree that the message is messed up a bit...  This particular message is
used in a few different locations in the code.  If you have additional
context about when this is received (call stack, persistent operation,
etc), it would help with identifying the specific usage and get that
cleaned up.

Looking at your example, a couple of things jump out...

1)  In your PK class, your getter/setter methods don't match the strId
attribute (get/setMessage?).  These names need to follow standard java bean
conventions and need to match up.

2)  In your GeoAssociation class...  It looks like you want the ability to
setGeo and setStrId, but I don't see those methods defined.  You have them
defined (sort of, per my first comment) in your PK class.  But, you don't
have them defined in your GeoAssoc class.

That's all I've got at this point.
Kevin


On Wed, Jan 29, 2014 at 11:48 PM, Mansour Al Akeel <
mansour.alakeel@gmail.com> wrote:

> Hello all,
>
> I need to create a primary key in my tables, and set constrains on
> them. So I have few tables:
>
> 1- Geo
> 2- GeoAssociation
>
> For the GeoAssociation, I need a primary key GeoAssociationPK. The PK
> has a field, that "HAS" to be a geo_id. So here's the definition I
> have for GeoAssociation:
>
>
>
> @Embeddable
> public class GeoAssociationPK implements Serializable {
>
>     private String strId;
>
>     private Geo geo;
>
>     @Column(name = "str_id")
>     public String getMessage() {
>         return strId;
>     }
>
>     public void setMessage(String message) {
>         this.strId = message;
>     }
>
>     @ManyToOne(optional = false)
>     @Column(name = "geo_id")
>     public Geo getGeo() {
>         return geo;
>     }
>
>     public void setGeo(Geo geoFrom) {
>         this.geo = geoFrom;
>     }
>
>     @Override
>     public boolean equals(Object obj) {
>         throw new UnsupportedOperationException("Not implemented");
>     }
>
>     @Override
>     public int hashCode() {
>         throw new UnsupportedOperationException("Not implemented");
>     }
>
> }
>
>
>
> This is how I define the GeoAssociation class:
>
> @Entity
> @Access(AccessType.PROPERTY)
>
> public class GeoAssoc {
>
>     @EmbeddedId
>     private GeoAssociationPK id = new GeoAssociationPK();
>
>     private GeoAssocType type;
>
>     @ManyToOne(fetch = FetchType.LAZY)
>     @JoinColumn(name = "type")
>     public GeoAssocType getType() {
>         return type;
>     }
>
>     public void setType(GeoAssocType value) {
>         this.type = value;
>     }
>
>     public Geo getGeoFrom() {
>         return this.id.getGeo();
>     }
>
>     @Override
>     public boolean equals(Object obj) {
>         throw new UnsupportedOperationException("Not implemented");
>     }
>
>     @Override
>     public int hashCode() {
>         throw new UnsupportedOperationException("Not implemented");
>     }
>
> }
>
>
> The error I am getting when building with maven enhancer is:
>
> The id class specified by type "class entities.geo.GeoAssoc" does not
> match the primary key fields of the class.  Make sure your identity
> class has the same primary keys as your persistent type, including pk
> field types. Mismatched property: "geo" -> [Help 1]
>
> I don't understand the error message or how to solve it. What I need
> is to be able to create and persist entity without having to
> explicitly create the PK.
>
> For example :
> GeoAssociation gs = new GeoAssociation();
> gs.setGeo(myGeo);
> gs.setStrId(someString);
>
>
> My question is how to achieve this, and resolve this error message ?
>
>
> Thank you in advance.
>