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: