将数据插入数据库时​​,春季出现“ org.springframework.beans.ConversionNotSupportedException”错误

时间:2018-11-18 05:34:12

标签: java mysql spring hibernate jpa

我正在将数据保存到数据库中的表中,但它向我显示了类似的错误

  

已解决的[org.springframework.beans.ConversionNotSupportedException:无法将类型“ java.lang.Long”的属性值转换为属性“ letter”的必需类型“ com.ashwin.springsecurityangular.model.Letter”;嵌套异常为java.lang.IllegalStateException:无法将属性“字母”的类型“ java.lang.Long”的值转换为所需的类型“ com.ashwin.springsecurityangular.model.Letter”:未找到匹配的编辑器或转换策略] < / p>

Letter.java类

 @Entity
    @Table(name = "letter")
    public class Letter implements Serializable {


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


        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long letterNo;

        @ManyToOne(fetch = FetchType.EAGER, optional = false,cascade=CascadeType.PERSIST)
        @JoinColumn(name = "letterId")
        @JsonIgnore
        private ClkLetter clkletter;

        private String inOut;

        private String inOutNo;

        private String inOutDate;

        private String letterIssuedSubBy;

        private String letterFile;

        private String representativeName;

        @ManyToOne(fetch = FetchType.EAGER, optional = false,cascade=CascadeType.PERSIST)
        @JoinColumn(name = "selectionId")
        @JsonIgnore
        private Selection selection;

        @ManyToOne(fetch = FetchType.EAGER, optional = false,cascade=CascadeType.PERSIST)
        @JoinColumn(name = "assessmentNo")
        @JsonIgnore
        private Assessment assessment;


        public Letter() {

        }
//i omitted all getters and setters
    }

LetterDoc.java

在此表中,我没有使用过像创建普通表时使用的@Id类型的单列。在这里,我使用了两个主键,因为它们属于 Letter 的外键>和 Document 类。

@Entity
@Table(name = "letter_doc")
@IdClass(AssignedRoleId.class)
public class LetterDoc implements Serializable {


    private static final long serialVersionUID = 1L;

    @Id
    @ManyToOne(fetch = FetchType.EAGER, optional = false,cascade=CascadeType.PERSIST)
    @JoinColumn(name = "letterNo",unique=true)
    @JsonIgnore
    private Letter letter;

    @Id
    @ManyToOne(fetch = FetchType.EAGER, optional = false,cascade=CascadeType.PERSIST)
    @JoinColumn(name = "documentId",unique=true)
    @JsonIgnore
    private Document document;

    private String docFile;

    public LetterDoc(Letter letter, Document document, String docFile) {

        this.letter = letter;
        this.document = document;
        this.docFile = docFile;
    }

    public Letter getLetter() {
        return letter;
    }

    public void setLetter(Letter letter) {
        this.letter = letter;
    }

    public Document getDocument() {
        return document;
    }

    public void setDocument(Document document) {
        this.document = document;
    }

    public String getDocFile() {
        return docFile;
    }

    public void setDocFile(String docFile) {
        this.docFile = docFile;
    }

    public LetterDoc() {

    }



     }

AssignedRoleId.java

public class AssignedRoleId implements Serializable {

    private Letter letter;
    private Document document;

    public AssignedRoleId(Letter letter, Document document) {

        this.letter = letter;
        this.document = document;
    }

    public AssignedRoleId() {}

    @Override
    public boolean equals(Object o) {
         if (o == this) {
             return true;
         }
         if (!(o instanceof LetterDoc)) {
             return false;
         }
         LetterDoc assignedRole = (LetterDoc) o;
         return Objects.equals(letter, assignedRole.getLetter()) &&
                 Objects.equals(document, assignedRole.getDocument());

    }

    @Override
    public int hashCode() {
        return Objects.hash(letter, document);
    }

    public Letter getLetter() {
        return letter;
    }

    public void setLetter(Letter letter) {
        this.letter = letter;
    }

    public Document getDocument() {
        return document;
    }

    public void setDocument(Document document) {
        this.document = document;
    }

我正在尝试插入数据,例如:

首先插入Letter表中的数据。由于已成功插入,所以这里没有问题:

Letter letter=letterRepository.save(new Letter(clkLetter,sldto.getLetterDto().getInOut(),sldto.getLetterDto().getInOutNo(),sldto.getLetterDto().getInOutDate(),sldto.getLetterDto().getLetterIssuedSubBy(),sldto.getLetterDto().getLetterFile(),sldto.getLetterDto().getRepresentativeName()
                ,selection,assessment));

我试图将数据插入 letter_doc 表中,但是没有发生。 document 表只是一个查找表。

Document dox=documentRepository.findById((long)documentDto.getDocId()).get();
                 letterDocRepository.save(new LetterDoc(letter,dox,"a"));

我数据库中的表如下:

enter image description here

0 个答案:

没有答案