1

I am saving the data to the table in database but it is showing me error like

Resolved [org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.Long' to required type 'com.ashwin.springsecurityangular.model.Letter' for property 'letter'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.Long' to required type 'com.ashwin.springsecurityangular.model.Letter' for property 'letter': no matching editors or conversion strategy found]

Letter.java class

 @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

Here in this table i haven't used a single column which is like @Id type we used in making normal table.Here I have used two primary key as they belong to the foreign key of Letter and Document Class.

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

I am trying to insert the data like using:

At first the data in Letter table gets inserted.Here is no problem as it is successfully inserted:

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

The i tried to insert the data in letter_doc table but it is not happening. document table is simply a look up table.

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

My tables in database looks like this:

enter image description here

ashwin karki
  • 643
  • 5
  • 19
  • 35
  • So, Do you want to use `private Letter letter;` as `@Id` – Shiva Nov 18 '18 at 05:50
  • @secretsuperstar you are back. yes i am getting error like now recently "[org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.Long' to required type 'com.ashwin.springsecurityangular.model.Document' for property 'document'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.Long' to required type 'com.ashwin.springsecurityangular.model.Document' for property 'document': no matching editors or conversion strategy found]" – ashwin karki Nov 18 '18 at 05:52
  • You are trying to use custom generator. Refer the docs to use custom generator.https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-declaration-id – Shiva Nov 18 '18 at 05:57
  • this too should help you.http://learningprogramming.net/java/hibernate5/custom-id-generator-in-hibernate-5/ – Shiva Nov 18 '18 at 06:00
  • no no it is not helping me please look the error i have posted – ashwin karki Nov 18 '18 at 06:53
  • There is lot of unclear things. Not sure why are you mapping `Letter` and `Document` as `@Id`. Not clear on what are you trying to achieve. Can I say you want to define `Document` and `Letter` entity and define `many to many` mapping between `Letter` and `Document` – Shiva Nov 18 '18 at 06:59
  • There a lot of entities that are not listed here to give a clear view, i.e Solution, CKLetter etc. IMHO the "AssignedRoleId" is the problem since IdClass is meant to be used for compound primary keys with simple types in it and not Entities. For a proper use of "@IdClass" see https://stackoverflow.com/questions/212350/which-annotation-should-i-use-idclass-or-embeddedid – garfield Nov 18 '18 at 11:07

0 Answers0