Java 复合主键和数据截断错误

Java 复合主键和数据截断错误,java,mysql,hibernate,truncate,composite-primary-key,Java,Mysql,Hibernate,Truncate,Composite Primary Key,我正在使用Hibernate和MySql,今天我在一个表中设置了一个复合主键,如下所示: 自主学习 这个实体是一个具有自我学习能力的实体: 这是我的java实体: @Entity @Table(name = "defselflearning", catalog = "ats") public class DefSelfLearning implements java.io.Serializable { /** * */ private static f

我正在使用Hibernate和MySql,今天我在一个表中设置了一个复合主键,如下所示:

自主学习

这个实体是一个具有自我学习能力的实体:

这是我的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;
    }

}
自学班:

@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时,一切正常,但当我创建一个SelfLearning时,我收到MysqlDataTruncation异常:

Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'id_parName' at row 1
这个错误已经解释得够多了,但我没有发现问题出在哪里,这是用于自学习创建的代码:

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的第一行,代码就是在这里失败的

如果我手动设置此选项,它将起作用:

这是第一个代码的java调试屏幕,失败:


如果试图在实体的“id\u parName”列中插入长度超过15的字符,则必须在字段和getter之间进行选择。并且所有注释都应该在字段上,或者它们都应该在getter上,您不能混合使用这两种方法(除非您使用
@AccessType
注释)。 Hibernate/Jpa将从Id上的注释中获取所使用的方法


将第一个可嵌入实体上的
@Id
更改为
@EmbeddedId
,并确保它位于getter上。

自学习错误地映射了列,Id\u parName=Id\u description,Id\u description=Id\u note和Id\u note=Id\u 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会在自学习之前抛出异常。id_parName的值为1008,表defSelfLearning已正确填充,但执行选项是明确的。请编写一个测试,用一些值设置实体的属性,然后调用save。如果此测试失败,请发布代码。这很困难,因为我们看不到设置为entityYes属性的值,我更新了两行,第二行是通过java代码创建的,就像第一行一样。也许我发现了错误…自学习错误地映射了列,id\u parName=id\u description,id\u description=id\u note和id\u note=id\u parName我已经从DefSelfLearning中删除了'@id'和'@EmbeddedId',并且在public defsellearningkeys getdefsellearningkeys()方法中添加了'@EmbeddedId',但我仍然收到了错误
@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;
}