2015-11-19 78 views
5

我使用Hibernate和MySQL和今天我設置好的我的表中的一個複合主鍵,所以下面:複合主鍵和數據截斷誤差

DefSelfLearning

DefSelfLearning

這實體是一對多與自學習:

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

和自學習類:

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

,但是當我創建一個defSelfLearning所有工作正常,但是當我創建了一個自學習,我收到MysqlDataTruncation例外:

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

此錯誤是不夠explined,但我不覺得哪裏出了問題,這是自學習創作代碼:

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

SelfLearning錯誤的映射列,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; 
} 

和它的作品

0

您嘗試插入一個char比15再列 「id_parName」

+0

如果是這樣它一直,defSelfLearning將自學習之前拋出異常。 id_parName的值爲1008,表defSelfLearning已正確填充 – luca

+0

但是,執行過程很明確。請寫一個測試,用一些值設置你的實體的屬性,然後調用save。如果此測試失敗,請發佈代碼。這很難,因爲我們沒有看到你設置的實體屬性的值 –

+1

是的,我更新了兩行,第二個是通過Java代碼創建的,就像第一個一樣。也許我已經找到了錯誤... SelfLearning錯誤的映射列,id_parName = id_description,id_description = id_note和id_note = id_parName – luca

0

在你的實體上,你必須在字段和getter之間進行選擇。所有註釋都應該放在字段中,或者全部放在吸氣劑上,不能混用兩種方法(除非使用註釋符號@AccessType)。 Hibernate/Jpa將從Id上的註釋中選擇使用的認證。

將第一個Embeddable實體上的@Id更改爲@EmbeddedId並確保它位於getter上。

+0

我已經從DefSelfLearning中刪除了'@Id'和'@EmbeddedId',並且我已經添加了'@EmbeddedId'公共DefSelfLearningKeys getDefSelfLearningKeys()方法,但我仍然收到錯誤 – luca