2015-11-19 16 views
5

Tôi đang sử dụng Hibernate và MySql và hôm nay tôi setted một phím composite tiểu học tại một trong những bàn của tôi, vì vậy dưới đây:chính composite tiểu học và cắt ngắn dữ liệu lỗi

DefSelfLearning

DefSelfLearning

Và đây thực thể là OneToMany với SelfLearning:

enter image description here

Đây là thực thể java của tôi:

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

} 

lớp cho phím composite:

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

và lớp 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; 
    } 
} 

nhưng khi tôi tạo một defSelfLearning tất cả hoạt động tốt, nhưng khi tôi tạo một SelfLearning tôi nhận được Ngoại lệ MysqlDataTruncation:

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

Lỗi này là đủ explined, nhưng tôi không tìm thấy nơi được vấn đề, đây là đoạn code để tạo 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); 

    } 

Bạn có thấy đâu là vấn đề Cảm ơn

Đây là? hàng đầu tiên của defSelfLearning và đó là nơi mã thất bại

enter image description here

nếu tôi đặt tay này hoạt động:

enter image description here

Đây là một màn hình java debug mã đầu tiên, thất bại:

enter image description here

Trả lời

0

Tự học không đúng ánh xạ các cột, id_parName = id_description, id_description = id_note và id_note = id_parName, nhưng tại sao? Vì vậy, tôi đọc:

Khi chú thích JoinColumns được sử dụng, cả hai tên và các yếu tố referencedColumnName phải được xác định trong mỗi ví dụ JoinColumn chú thích.

Tôi đã thêm yếu tố này vậy:

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

Và nó hoạt động

0

Bạn cố gắng để chèn một char đó là dài hơn 15 trong cột "id_parName"

+0

Nếu vậy nó đã, defSelfLearning sẽ ném ngoại lệ trước khi SelfLearning. Giá trị của id_parName là 1008 và bảng defSelfLearning đã được điền chính xác – luca

+0

Nhưng việc xử lý là rõ ràng. Hãy viết một bài kiểm tra, thiết lập các thuộc tính của các thực thể của bạn với một số giá trị và gọi lưu sau đó. Nếu kiểm tra này không thành công, hãy đăng mã. Rất khó vì chúng tôi không thấy giá trị bạn đặt cho thuộc tính của đối tượng –

+1

Có, tôi đã cập nhật với hai hàng, thứ hai được tạo thông qua mã java, như mã đầu tiên. Có lẽ tôi đã tìm thấy lỗi ... SelfLearning sai ánh xạ các cột, id_parName = id_description, id_description = id_note và id_note = id_parName – luca

0

Trên thực thể của bạn, bạn phải lựa chọn giữa trường và getter. Và tất cả các chú thích phải nằm trên các trường hoặc tất cả các chú thích phải có trên getters, bạn không thể kết hợp cả hai cách tiếp cận (trừ khi bạn sử dụng chú thích @AccessType). Hibernate/Jpa sẽ nhận được sự chấp thuận được sử dụng từ chú thích trên Id.

Thay đổi @Id trên thực thể có thể nhúng đầu tiên thành @EmbeddedId và đảm bảo nó nằm trên bộ thu thập.

+0

Tôi đã xóa '@Id' và '@EmbeddedId' khỏi DefSelfLearning và tôi đã thêm '@EmbeddedId' vào phương thức getDefSelfLearningKeys() công khai nhưng tôi vẫn nhận được lỗi – luca

Các vấn đề liên quan