7

1 つのシーケンスと 2 つの外部キーで構成される複合主キーを持つテーブルがあります。エンティティ クラスを永続化できますが、シーケンスに従って生成されません。1 つのシーケンスと 2 つの外部キーで構成される複合主キーを持つテーブル、maven の hbm2java は次のエンティティを提供します

本体はこちら


package aop.web.teacher.rmodels;

// Generated Dec 14, 2010 8:45:32 PM by Hibernate Tools 3.2.2.GA

import java.util.Date;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Schoolmaster generated by hbm2java
 */
@Entity
@Table(name = "schoolmaster", schema = "public")
public class Schoolmaster implements java.io.Serializable {

 private SchoolmasterId id;
        ...


 @EmbeddedId
 @AttributeOverrides({
   @AttributeOverride(name = "id", column = @Column(name = "id", nullable = false)),
   @AttributeOverride(name = "districtId", column = @Column(name = "district_id", nullable = false)),
   @AttributeOverride(name = "typeOfSchool", column = @Column(name = "type_of_school", nullable = false)) })
 public SchoolmasterId getId() {
  return this.id;
 }

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

 @ManyToOne(fetch = FetchType.LAZY)
 @JoinColumn(name = "type_of_school", nullable = false, insertable = false, updatable = false)
 public AopTeachersTypeMaster getAopTeachersTypeMaster() {
  return this.aopTeachersTypeMaster;
 }

 public void setAopTeachersTypeMaster(
   AopTeachersTypeMaster aopTeachersTypeMaster) {
  this.aopTeachersTypeMaster = aopTeachersTypeMaster;
 }

 @ManyToOne(fetch = FetchType.LAZY)
 @JoinColumn(name = "school_nature")
 public AopTeachersSchoolNatureMaster getAopTeachersSchoolNatureMaster() {
  return this.aopTeachersSchoolNatureMaster;
 }

 public void setAopTeachersSchoolNatureMaster(
   AopTeachersSchoolNatureMaster aopTeachersSchoolNatureMaster) {
  this.aopTeachersSchoolNatureMaster = aopTeachersSchoolNatureMaster;
 }

 @ManyToOne(fetch = FetchType.EAGER)
 @JoinColumn(name = "district_id", nullable = false, insertable = false, updatable = false)
 public AopTeachersDistrictMaster getAopTeachersDistrictMaster() {
  return this.aopTeachersDistrictMaster;
 }

 public void setAopTeachersDistrictMaster(
   AopTeachersDistrictMaster aopTeachersDistrictMaster) {
  this.aopTeachersDistrictMaster = aopTeachersDistrictMaster;
 }

 @Column(name = "school_name", length = 50)
 public String getSchoolName() {
  return this.schoolName;
 }

 public void setSchoolName(String schoolName) {
  this.schoolName = schoolName;
 }

 @Column(name = "school_address")
 public String getSchoolAddress() {
  return this.schoolAddress;
 }

 public void setSchoolAddress(String schoolAddress) {
  this.schoolAddress = schoolAddress;
 }

 @Column(name = "school_phone_number", length = 12)
 public String getSchoolPhoneNumber() {
  return this.schoolPhoneNumber;
 }

 public void setSchoolPhoneNumber(String schoolPhoneNumber) {
  this.schoolPhoneNumber = schoolPhoneNumber;
 }

 @Temporal(TemporalType.DATE)
 @Column(name = "establishment_date", length = 13)
 public Date getEstablishmentDate() {
  return this.establishmentDate;
 }

 public void setEstablishmentDate(Date establishmentDate) {
  this.establishmentDate = establishmentDate;
 }

 @Column(name = "school_no_of_teachers")
 public Integer getSchoolNoOfTeachers() {
  return this.schoolNoOfTeachers;
 }

 public void setSchoolNoOfTeachers(Integer schoolNoOfTeachers) {
  this.schoolNoOfTeachers = schoolNoOfTeachers;
 }

 @Column(name = "school_no_of_students")
 public Integer getSchoolNoOfStudents() {
  return this.schoolNoOfStudents;
 }

 public void setSchoolNoOfStudents(Integer schoolNoOfStudents) {
  this.schoolNoOfStudents = schoolNoOfStudents;
 }

}


これが組み込み PK クラスです。


/**
 * SchoolmasterId generated by hbm2java
 */
@Embeddable
public class SchoolmasterId  implements java.io.Serializable {


     private long id;
     private long districtId;
     private long typeOfSchool;

    public SchoolmasterId() {
    }

    public SchoolmasterId(long id, long districtId, long typeOfSchool) {
       this.id = id;
       this.districtId = districtId;
       this.typeOfSchool = typeOfSchool;
    }


    @Column(name="id", nullable=false)
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    public long getId() {
        return this.id;
    }

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

    @NaturalId
    @Column(name="district_id", nullable=false)
    public long getDistrictId() {
        return this.districtId;
    }

    public void setDistrictId(long districtId) {
        this.districtId = districtId;
    }
    @NaturalId
    @Column(name="type_of_school", nullable=false)
    public long getTypeOfSchool() {
        return this.typeOfSchool;
    }

    public void setTypeOfSchool(long typeOfSchool) {
        this.typeOfSchool = typeOfSchool;
    }


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

   return (this.getId()==castOther.getId())
 && (this.getDistrictId()==castOther.getDistrictId())
 && (this.getTypeOfSchool()==castOther.getTypeOfSchool());
   }

   public int hashCode() {
         int result = 17;

         result = 37 * result + (int) this.getId();
         result = 37 * result + (int) this.getDistrictId();
         result = 37 * result + (int) this.getTypeOfSchool();
         return result;
   }   


}


ここで、IDが自動生成されることを期待しています...追加しただけです


@NaturalId


@GeneratedValue(strategy=GenerationType.SEQUENCE)

GenerationType.AUTO でも試しましたが、うまくいきませんでした。提案してください。

4

2 に答える 2

7

この問題には 1 つの回避策があります。私は同じ条件に直面しました。複合キーとして 4 つのフィールドがあり、そのうちの 1 つをシーケンスで生成する必要があります。Embedded クラスをまったく作成していません。シーケンスで生成する必要がある @Id フィールドが 1 つだけあります。残りのすべてのフィールド値は単純な列になります。DB で参照整合性が適用され、コード内の残りの 3 つのフィールドの値が null でないことを確認しているためです。

エラーが発生した場合、トランザクションはロールバックされます。

于 2011-08-23T14:20:20.430 に答える