使用GeneratedValue的Hibernate 3 Composite键

ahm*_*hmy 6 java orm hibernate

我有数据库这种双表格revisionsPagu

Pagu模型中,我必须复合键:

  • id int(由数据库自动生成)
  • revision_id(foreign_key to revisions)表

如何在Hibernate 3上实现这个?

这就是我想出来的

@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;
 }
Run Code Online (Sandbox Code Playgroud)

这是我的PaguId课程

@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;
   }   


}
Run Code Online (Sandbox Code Playgroud)

当我尝试将其保存在数据库上时出现错误:

org.hibernate.NonUniqueObjectException: a different object with the same    identifier value was already associated with the session:
Run Code Online (Sandbox Code Playgroud)

- 更新 - 但是使用像这样的EmbeddedId更改实现

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;
}
....
Run Code Online (Sandbox Code Playgroud)

它编译正确,但在坚持模型时给了我错误.

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): id.model.Pagu
Run Code Online (Sandbox Code Playgroud)

Ras*_*nke 3

我认为不可能在组合键中使用GenerateValue,您必须选择组合键或单个GenerateValue-id。