The nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to the required type

When I try to submit my form, I get the following validation error. In the drop-down list filled with values ​​from Document.java, the following error appears:

Failed to convert property value of type java.lang.String to required type testapp.domain.Document for property document_number; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [testapp.domain.Document] for property document_number: no matching editors or conversion strategy found 

Is there something wrong with my image?

Display Document.java

  @OneToMany(mappedBy="document_number", cascade = CascadeType.ALL) private Set<DocumentRevision> documentRevisions; public void setDocumentRevisions(Set<DocumentRevision> documentRevisions){ this.documentRevisions = documentRevisions; } 

Display DocumentRevision.java

  @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name="DOCUMENT_NUMBER") private Document document_number; public void setDocument_number(Document document_number){ this.document_number = document_number; } public Document getDocument_number(){ return document_number; } 

The relationship between the two tables is that multiple DocumentRevisions can have the same DocumentNumber, but one DocumentRevision can have only one DocumentNumber

thanks for the help

/ D

Edit

I am using spring 3.0 and hibernate 3. Here is a recent thread that includes a controller page and jsp.

Edit 2

Here is a DAO implementation method that should save document_number in DocumentRevision

  public void saveDocumentRevision(DocumentRevision documentRevision) { this.sessionFactory.getCurrentSession().save(documentRevision); } 

Edit 3

I believe this is the part of the code in which document_number is written. Should I use "documentNumberList" somewhere in the .POST method? How can I do this in this case?

controller part:

 @RequestMapping(value="/add", method=RequestMethod.GET) public String getDocumentRevision(Model model) { DocumentRevision documentRevision = new DocumentRevision(); model.addAttribute("documentRevisionAttribute", documentRevision); model.addAttribute("documentNumberList", documentService.retrieveAllDocumentNumbers()); return "new-documnent-revision"; } @RequestMapping(value="/add", method=RequestMethod.POST) public String postDocumentRevision(@ModelAttribute("documentRevisionAttribute") @Valid DocumentRevision documentRevision, BindingResult result) { if(result.hasErrors()){ return "new-document-revision"; } documentRevisionService.createDocumentRevision(documentRevision); return "redirect:/testapp/document-revision/list"; } 

Update

I added the following to the controller:

  /** * Property editor to map Documents from documents IDs. */ class DocumentPropertyEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { Document value = documentService.retrieveDocumentNumber(text); setValue(value); } @Override public String getAsText() { Document d = (Document) getValue(); return d != null ? String.valueOf(d.getDocumentNumber()) : ""; } } @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Document.class, new DocumentPropertyEditor()); } 

Now I get a NullPointerException:

 java.lang.NullPointerException testapp.controller.DocumentRevisionController$DocumentPropertyEditor.getAsText(DocumentRevisionController.java:59) 

Why is there no value in the receiver PropertyEditor?

Change 4

retrieveDocumentNumber method (text):

  public Document retrieveDocumentNumber(String text){ return (Document) this.sessionFactory.getCurrentSession().get(Document.class, text); } 
+4
source share
1 answer

Spring WebDataBinder cannot convert a String document_number to a document instance when the modelRevision attribute is populated. You have two options:

  • Initialize WebDataBinder with a PropertyEditor that can handle the conversion. Most direct, but only work for this controller.

  • Register Converter<String, Document>

if you select the first, annotate the controller method with the annotation @InitBinder and the register property editor. It looks like you only need to retrieve the document from the database by document number. See Configuring WebDataBinder initialization in the reference documentation.

Edit

Example property editor for matching documents to / from identifier strings.

 /** * Property editor to map Documents from documents IDs. */ class DocumentPropertyEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { Long id = Long.parseLong(text); Document value = id == 0 ? null : documentService.get(id); setValue(value); } @Override public String getAsText() { Document d = (Document) getValue(); return d != null ? String.valueOf(d.getId()) : ""; } } 

For a second approach, check out Validation, Data Binding, and Type Conversion to see how to create and register a Converter in Spring ConversionService .

+3
source

Source: https://habr.com/ru/post/1411326/


All Articles