2015-11-19 10 views
5

私はHibernateとMySQLを使用していて、今日、私はそうの下に、私のテーブルのいずれかで、複合主キーをsetted:複合主キーとデータの打ち切り誤差

DefSelfLearning

そして、これをDefSelfLearning

エンティティはSelfLearningでOneToManyです:

enter image description here

これは私のJavaのエンティティです:

@Entity 
@Table(name = "defselflearning", catalog = "ats") 
public class DefSelfLearning implements java.io.Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    @EmbeddedId 
    private DefSelfLearningKeys defSelfLearningKeys; 
    private Ecu ecu; 
    private String excelColumn; 
    @JsonIgnore 
    private Set<SelfLearning> selfLearnings = new HashSet<SelfLearning>(0); 

    public DefSelfLearning() { 
    } 

    public DefSelfLearning(DefSelfLearningKeys defSelfLearningKeys, Ecu ecu) { 
     this.defSelfLearningKeys = defSelfLearningKeys; 
     this.ecu = ecu; 
    } 

    public DefSelfLearning(Ecu ecu, DefSelfLearningKeys defSelfLearningKeys, String excelColumn, Set<SelfLearning> selfLearnings) { 
     this.ecu = ecu; 
     this.defSelfLearningKeys = defSelfLearningKeys; 
     this.excelColumn = excelColumn; 
     this.selfLearnings = selfLearnings; 
    } 

    @Id 
    public DefSelfLearningKeys getDefSelfLearningKeys() { 
     return this.defSelfLearningKeys; 
    } 

    public void setDefSelfLearningKeys(DefSelfLearningKeys defSelfLearningKeys) { 
     this.defSelfLearningKeys = defSelfLearningKeys; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "id_ecu", nullable = false) 
    public Ecu getEcu() { 
     return this.ecu; 
    } 

    public void setEcu(Ecu ecu) { 
     this.ecu = ecu; 
    } 

    @Column(name = "excelColumn", length = 2) 
    public String getExcelColumn() { 
     return this.excelColumn; 
    } 

    public void setExcelColumn(String excelColumn) { 
     this.excelColumn = excelColumn; 
    } 

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "defSelfLearning") 
    public Set<SelfLearning> getSelfLearnings() { 
     return this.selfLearnings; 
    } 

    public void setSelfLearnings(Set<SelfLearning> selfLearnings) { 
     this.selfLearnings = selfLearnings; 
    } 

} 

複合キーのためのクラス:

@Embeddable 
public class DefSelfLearningKeys implements Serializable { 
    private static final long serialVersionUID = 1L; 
    protected String parName; 
    protected String description; 
    protected String note; 

    public DefSelfLearningKeys() {} 

    public DefSelfLearningKeys(String parName, String description, String note) { 
     this.parName = parName; 
     this.description = description; 
     this.note = note; 
    } 

    @Column(name = "parName", nullable = false, length = 15) 
    public String getParName() { 
     return this.parName; 
    } 

    public void setParName(String parName) { 
     this.parName = parName; 
    } 

    @Column(name = "description", nullable = false, length = 100) 
    public String getDescription() { 
     return this.description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    @Column(name = "note", nullable = false, length = 100) 
    public String getNote() { 
     return this.note; 
    } 

    public void setNote(String note) { 
     this.note = note; 
    } 
} 

とSelfLearningクラス:

@Entity 
@Table(name = "selflearning", catalog = "ats") 
public class SelfLearning implements java.io.Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    private int idSelfLearning; 
    private Acquisition acquisition; 
    private DefSelfLearning defSelfLearning; 
    private String value; 

    public SelfLearning() { 
    } 

    public SelfLearning(int idSelfLearning, Acquisition acquisition, DefSelfLearning defSelfLearning) { 
     this.idSelfLearning = idSelfLearning; 
     this.acquisition = acquisition; 
     this.defSelfLearning = defSelfLearning; 
    } 

    public SelfLearning(int idSelfLearning, Acquisition acquisition, DefSelfLearning defSelfLearning, String value) { 
     this.idSelfLearning = idSelfLearning; 
     this.acquisition = acquisition; 
     this.defSelfLearning = defSelfLearning; 
     this.value = value; 
    } 

    @Id 
    @GeneratedValue(strategy = IDENTITY) 
    @Column(name = "id_selfLearning", unique = true, nullable = false) 
    public int getIdSelfLearning() { 
     return this.idSelfLearning; 
    } 

    public void setIdSelfLearning(int idSelfLearning) { 
     this.idSelfLearning = idSelfLearning; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "id_acquisition", nullable = false) 
    public Acquisition getAcquisition() { 
     return this.acquisition; 
    } 

    public void setAcquisition(Acquisition acquisition) { 
     this.acquisition = acquisition; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumns({ 
      @JoinColumn(name = "id_parName", nullable = false), 
      @JoinColumn(name = "id_description", nullable = false), 
      @JoinColumn(name = "id_note", nullable = false) 
     }) 
    public DefSelfLearning getDefSelfLearning() { 
     return this.defSelfLearning; 
    } 

    public void setDefSelfLearning(DefSelfLearning defSelfLearning) { 
     this.defSelfLearning = defSelfLearning; 
    } 

    @Column(name = "value") 
    public String getValue() { 
     return this.value; 
    } 

    public void setValue(String value) { 
     this.value = value; 
    } 
} 

が、私は正常に動作しますが、私は私が受け取るSelfLearningを作成するときに、すべてのdefSelfLearningを作成しますMysqlDataTruncation例外:

Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'id_parName' at row 1 

このエラーはexplined十分ですが、これはSelfLearningを作成するためのコードの問題でされた場合、私は見つけられません。問題がどこにあるか、あなたが探しください

for (DefSelfLearning defSelfLearning:defSelfLearningList){ 
     SelfLearning selfLearning=new SelfLearning(); 
     String key = defSelfLearning.getExcelColumn()+index; 
     String value = actualRowValues.get(key); 
     selfLearning.setAcquisition(findByCarAndExcelRow(carServices.findById(acquisitionForm.getCar()), index)); 
     selfLearning.setDefSelfLearning(defSelfLearning); 
     selfLearning.setValue(value); 
     System.out.println(selfLearning.getDefSelfLearning().getDefSelfLearningKeys().getParName()); 
     selfLearningServices.create(selfLearning); 

    } 

おかげ

これは?最初defSelfLearningの行とコードが

enter image description here

を失敗したところ、私はこれを手動で設定し、それが動作するかどうか、それはです:

enter image description here

これが失敗したこと、最初のコードのJavaのデバッグの画面です:

enter image description here

答えて

0

セルラ学習の間違ったマッピング、id_parName = id_description、id_description = id_note、id_note = id_parName、なぜですか? だから私は読んで:

をJoinColumns注釈を使用する場合は、名前と referencedColumnNameと要素の両方が、そのような各 JoinColumn注釈で指定する必要があります。

私は、この要素を追加しました:

@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumns({ 
     @JoinColumn(name = "id_parName", referencedColumnName="parName", nullable = false), 
     @JoinColumn(name = "id_description", referencedColumnName="description", nullable = false), 
     @JoinColumn(name = "id_note", referencedColumnName="note", nullable = false) 
    }) 
public DefSelfLearning getDefSelfLearning() { 
    return this.defSelfLearning; 
} 

をそして、それは、defSelfLearningはSelfLearning前に例外をスローしているだろうされていたそうなら、それは

0

あなたが長い列「id_parName」

+0

に動作します。 id_parNameの値は1008であり、テーブルdefSelfLearningには正確に値が設定されています – luca

+0

しかし、実行は明らかです。テストを書いて、エンティティのプロパティをいくつかの値で設定し、その後にsaveを呼び出してください。このテストが失敗した場合は、コードを投稿してください。あなたがエンティティのプロパティに設定した値が表示されないので難しい –

+1

はい、2行で更新しました.2番目はJavaコードで作成されました。たぶん私はエラーを見つけました...間違ったセルフマッピングの列、id_parName = id_description、id_description = id_noteとid_note = id_parName – luca

0
で15以上である文字を挿入しよう

エンティティでフィールドとゲッターのどちらかを選択する必要があります。また、すべての注釈はフィールド上にある必要があります。または両方をゲッターにする必要があります(@AccessType注釈を使用する場合を除いて)両方の方法を混在させることはできません。 Hibernate/JpaはIdの注釈から使用されたapprochを取り上げます。

最初の埋め込みエンティティの@Id@EmbeddedIdに変更し、ゲッター上にあることを確認します。

+0

私はDefSelfLearningから '@Id'と '@EmbeddedId'を削除して、 '@EmbeddedId'を追加しましたパブリックDefSelfLearningKeys getDefSelfLearningKeys()メソッドが、私はまだエラーが表示されます – luca