Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 3 Composite key one with GeneratedValue

I have this two table on the database revisions and Pagu

in the Pagu model, i have to composite key:

  • id int (autogenerated by database)
  • revision_id (foreign_key to revisions) table

how to implement this on Hibernate 3 ?

this is what i came up with

@Entity
@Table(name="pagu"
    ,schema="dbo"
    ,catalog="dbname"
)
@IdClass(PaguId.class)
public class Pagu  implements java.io.Serializable {

 private int id;
 private int revisiId;
 private Entitas entitas;
 private Revisi revisi;
 ...

 @Id
 @GeneratedValue
 @Column(name="id", unique=true, nullable=false)
 public int getId() {
     return this.id;
 }

 public void setId(int id) {
     this.id = id;
 }

 @Id
 @Column(name="revisi_id", unique=true, nullable=false)
 public int getRevisiId() {
     return this.revisiId;
 }

 public void setRevisiId(int id) {
     this.id = id;
 }

And this is my PaguId class

@Embeddable
public class PaguId  implements java.io.Serializable {


     private int id;
     private int revisiId;

    public PaguId() {
    }

    public PaguId(int id, int revisiId) {
       this.id = id;
       this.revisiId = revisiId;
    }

    @Column(name="id", nullable=false)
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name="revisi_id", nullable=false)
    public int getRevisiId() {
        return this.revisiId;
    }

    public void setRevisiId(int revisiId) {
        this.revisiId = revisiId;
    }


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

         return (this.getId()==castOther.getId() && this.getRevisiId()==castOther.getRevisiId())
 && (this.getRevisiId()==castOther.getRevisiId());
   }

   public int hashCode() {
         int result = 17;

         result = 37 * result + this.getId();
         result = 37 * result + this.getRevisiId();
         return result;
   }   


}

When I try to save this on database I got error :

org.hibernate.NonUniqueObjectException: a different object with the same    identifier value was already associated with the session:

-- UPDATE-- But changing the implementation using EmbeddedId like this

public class Pagu  implements java.io.Serializable {


     private PaguId id;
     ...

     @EmbeddedId
@AttributeOverrides( {
    @AttributeOverride(name="id", column=@Column(name="id", nullable=false) ), 
    @AttributeOverride(name="revisiId", column=@Column(name="revisi_id", nullable=false) ) } )
public PaguId getId() {
    return this.id;
}

public void setId(PaguId id) {
    this.id = id;
}
....

It compiled right, but gave me error when persist the model.

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): id.model.Pagu
like image 477
ahmy Avatar asked Nov 14 '11 09:11

ahmy


People also ask

Can we use composite keys in Hibernate?

Use embeddable objects to join two primary keys into one composite key. Every JPA entity has a primary key, but some entities have more than one value as their primary key. In this case, you need to use a composite key. This Java tip introduces you to using composite keys in JPA and Hibernate.

How do you define a composite key in Hibernate using annotations?

A composite primary key, also called a composite key, is a combination of two or more columns to form a primary key for a table. In JPA, we have two options to define the composite keys: the @IdClass and @EmbeddedId annotations.

How do you create a composite key?

A composite key is made by the combination of two or more columns in a table that can be used to uniquely identify each row in the table when the columns are combined uniqueness of a row is guaranteed, but when it is taken individually it does not guarantee uniqueness, or it can also be understood as a primary key made ...


1 Answers

I do not think it is possible to use GeneratedValue in a composite key, you have to choose either a composite key, or a single GeneratedValue-id.

like image 164
Rasmus Franke Avatar answered Sep 20 '22 22:09

Rasmus Franke