> Thanks for your reply
> 
>   but how to edit the row using this Composite-id, i am not worked on this
> please suggest me...

Sorry,  what do you mean by edit.

To create a new Record (Region in this case).

Region r = new Region();
r.setPeriodRegionPK( new PeriodRegionPK( 1, 2 ));

so I guess to edit it would be

r.getPeriodRegionPK().setRegionId( 50 );


I do know that some ppl add something along the lines of....

Region region;
Period period;

@ManyToOne.....
@Column......
public Region getRegion() { return region; }

public void setRegion(Region region) {
        this.region = region;
        getPeriodRegionPK().setRegionId( region.getId() );
}

...
...
...

to PeriodRegion.java

so that you have the associations that are based of the same table columns as 
the PK class.   You have to add appropriate annotations or else hibernate/jpa 
will have a conflict as (essentually) there are 2 properties trying to create 
the same table column.



> 
> Alistair Bush wrote:
> >> Hi Matt,
> >>
> >>   presently i'm working with appfuse 2.0 (struts2) application. i have
> >> one
> >> problem how to use composite primary key using hibernate in this
> >>  application
> >
> > I would suggest using the @EmeddedId
> >
> > e.g.
> >
> > -- PeriodRegion.java
> >
> > package com.app.model;
> >
> > import java.io.Serializable;
> > import javax.persistence.Basic;
> > import javax.persistence.Column;
> > import javax.persistence.EmbeddedId;
> > import javax.persistence.Entity;
> > import javax.persistence.NamedQueries;
> > import javax.persistence.NamedQuery;
> > import javax.persistence.Table;
> >
> > /**
> >  *
> >  * @author alistair
> >  */
> > @Entity
> > @Table(name = "period_region", catalog = "myproject", schema = "")
> > @NamedQueries({...@namedquery(name = "PeriodRegion.findAll", query = "SELECT
> > p
> > FROM PeriodRegion p"), @NamedQuery(name = "PeriodRegion.findByPersonid",
> > query
> > = "SELECT p FROM PeriodRegion p WHERE p.periodRegionPK.personid =
> >
> > :personid"),
> >
> > @NamedQuery(name = "PeriodRegion.findByRegionid", query = "SELECT p FROM
> > PeriodRegion p WHERE p.periodRegionPK.regionid = :regionid"),
> > @NamedQuery(name
> > = "PeriodRegion.findByPaid", query = "SELECT p FROM PeriodRegion p WHERE
> > p.paid
> > = :paid")})
> > public class PeriodRegion implements Serializable {
> >     private static final long serialVersionUID = 1L;
> >     @EmbeddedId
> >     protected PeriodRegionPK periodRegionPK;
> >     @Basic(optional = false)
> >     @Column(name = "paid", nullable = false)
> >     private boolean paid;
> >
> >     public PeriodRegion() {
> >     }
> >
> >     public PeriodRegion(PeriodRegionPK periodRegionPK) {
> >         this.periodRegionPK = periodRegionPK;
> >     }
> >
> >     public PeriodRegion(PeriodRegionPK periodRegionPK, boolean paid) {
> >         this.periodRegionPK = periodRegionPK;
> >         this.paid = paid;
> >     }
> >
> >     public PeriodRegion(long personid, long regionid) {
> >         this.periodRegionPK = new PeriodRegionPK(personid, regionid);
> >     }
> >
> >     public PeriodRegionPK getPeriodRegionPK() {
> >         return periodRegionPK;
> >     }
> >
> >     public void setPeriodRegionPK(PeriodRegionPK periodRegionPK) {
> >         this.periodRegionPK = periodRegionPK;
> >     }
> >
> >     public boolean getPaid() {
> >         return paid;
> >     }
> >
> >     public void setPaid(boolean paid) {
> >         this.paid = paid;
> >     }
> >
> >     @Override
> >     public int hashCode() {
> >         int hash = 0;
> >         hash += (periodRegionPK != null ? periodRegionPK.hashCode() : 0);
> >         return hash;
> >     }
> >
> >     @Override
> >     public boolean equals(Object object) {
> >         // TODO: Warning - this method won't work in the case the id
> > fields are
> > not set
> >         if (!(object instanceof PeriodRegion)) {
> >             return false;
> >         }
> >         PeriodRegion other = (PeriodRegion) object;
> >         if ((this.periodRegionPK == null && other.periodRegionPK != null)
> >
> > (this.periodRegionPK != null &&
> > !this.periodRegionPK.equals(other.periodRegionPK))) {
> >             return false;
> >         }
> >         return true;
> >     }
> >
> >     @Override
> >     public String toString() {
> >         return "PeriodRegion[periodRegionPK=" + periodRegionPK + "]";
> >     }
> >
> > }
> >
> > -- PeriodRegionPK.java
> >
> > package com.app.model;
> >
> > import java.io.Serializable;
> > import javax.persistence.Basic;
> > import javax.persistence.Column;
> > import javax.persistence.Embeddable;
> >
> > /**
> >  *
> >  * @author alistair
> >  */
> > @Embeddable
> > public class PeriodRegionPK implements Serializable {
> >     @Basic(optional = false)
> >     @Column(name = "personid", nullable = false)
> >     private long personid;
> >     @Basic(optional = false)
> >     @Column(name = "regionid", nullable = false)
> >     private long regionid;
> >
> >     public PeriodRegionPK() {
> >     }
> >
> >     public PeriodRegionPK(long personid, long regionid) {
> >         this.personid = personid;
> >         this.regionid = regionid;
> >     }
> >
> >     public long getPersonid() {
> >         return personid;
> >     }
> >
> >     public void setPersonid(long personid) {
> >         this.personid = personid;
> >     }
> >
> >     public long getRegionid() {
> >         return regionid;
> >     }
> >
> >     public void setRegionid(long regionid) {
> >         this.regionid = regionid;
> >     }
> >
> >     @Override
> >     public int hashCode() {
> >         int hash = 0;
> >         hash += (int) personid;
> >         hash += (int) regionid;
> >         return hash;
> >     }
> >
> >     @Override
> >     public boolean equals(Object object) {
> >         // TODO: Warning - this method won't work in the case the id
> > fields are
> > not set
> >         if (!(object instanceof PeriodRegionPK)) {
> >             return false;
> >         }
> >         PeriodRegionPK other = (PeriodRegionPK) object;
> >         if (this.personid != other.personid) {
> >             return false;
> >         }
> >         if (this.regionid != other.regionid) {
> >             return false;
> >         }
> >         return true;
> >     }
> >
> >     @Override
> >     public String toString() {
> >         return "com.counties.app.model.PeriodRegionPK[personid=" +
> > personid +
> > ", regionid=" + regionid + "]";
> >     }
> >
> > }
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscr...@appfuse.dev.java.net
> > For additional commands, e-mail: users-h...@appfuse.dev.java.net
> 
> -----
> ---
> Sudhakar
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@appfuse.dev.java.net
For additional commands, e-mail: users-h...@appfuse.dev.java.net

Reply via email to