I am initializing a WebDataBinder with a PropertyEditor to handle the conversion of a String to a Document object so that it can be saved to the database as such. (My Hibernate mapping requires it). This works fine.
My issue is when I display the DocumentRevision table, where the documentNumber is saved as a Document type, it displays something like the following: testapp.domain.document@1f93466
- Do I have to make a new PropertyEditor which converts a Document type
into a String? - How would I do that? (An example would be appreciated since I have only ever see the other way around, String –> Object)
I am using Spring 3.0 and Hibernate 3
Code:
Document.java mapping
@OneToMany(mappedBy="document_number", cascade = CascadeType.ALL)
private Set<DocumentRevision> documentRevisions;
public void setDocumentRevisions(Set<DocumentRevision> documentRevisions){
this.documentRevisions = documentRevisions;
}
DocumentRevision.java mapping
@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 several DocumentRevisions can have the same DocumentNumber but a single DocumentRevision can only have one DocumentNumber
Part of the Controller:
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());
}
@RequestMapping(value="/list", method=RequestMethod.GET)
public String getDocumentRevisionList(Model model) {
List<DocumentRevision> documentRevisions = documentRevisionService.retrieveAllDocumentRevisions();
model.addAttribute("documentRevisions", documentRevisions);
return "document-revision";
@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";
}
the retrieveDocumentNumber(text) method:
public Document retrieveDocumentNumber(String text){
return (Document) this.sessionFactory.getCurrentSession().get(Document.class, text);
}
part of jsp page where DocumentRevision list is displayed:
<c:forEach items="${documentRevisions}" var="documentRevision">
<tr>
<td><c:out value="${documentRevision.documentId}"/></td>
<td><c:out value="${documentRevision.documentNumber}" /></td>
<td><c:out value="${documentRevision.documentRState}" /></td>
<td><a href="${addUrl}">Add</a></td>
</tr>
</c:forEach>
Thank you for your time and help!
/D
Look likes it calls default toString() method. You can redefine this method in document class. In this case it will output what you want.