Java – spring MVC with JPA data binding
My problem is to bind the data obtained by spring from the form to the JPA entity Strangely, if I don't look at bindingresults, it works properly Bindingresults says that binding errors occur when an empty string is passed in for field indexing, but I know it does bind them correctly because it updates the database perfectly when I don't check hibernate Is there any way to bypass the binding error triggered by the error without writing logic?
@Entity @Table(name="child") public class Child { @Id @Column(name="id") private Integer childId; @ManyToOne(fetch=FetchType.EAGER ) @JoinColumn(name="house",referencedColumnName="house") private House house; @NotNull() @Past() @Column(name="birthday") private Date birthday; @Column(name="graduation_date") private Date graduationDay; }
I tried the following lines in the Attribute Editor to no avail
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd,yyyy"); registry.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
The following is the method signature of the controller method that processes the request
@Controller @SessionAttributes(value="child") @RequestMapping(value="child") public class ChildModController { @RequestMapping(value="save-child.do",params="update",method = RequestMethod.POST) public @ResponseBody Map<String,?> updateChild( HttpServletRequest request,@Valid @modelattribute(value="child")Child child,BindingResult results) }
This is the message I got from the bindingresult class
09:01:36.006 [http-thread-pool-28081(5)] INFO simple - Found fieldError: graduationDay,Failed to convert property value of type java.lang.String to required type java.util.Date for property graduationDay; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.persistence.Column java.util.Date for value '; nested exception is java.lang.IllegalArgumentException
Solution
Spring automatically binds simple object types, such as string and number, but for things like Java util. For complex objects such as date or types defined by yourself, you need to use so-called property editors or converters, both of which can solve your problem
Spring already has a predefined property editor and converter, such as @ numberformat and @ datetimeformat
You can use them directly on your fields
public class Child { @DateTimeFormat(pattern="dd/MM/yyyy") private Date birthday; @DateTimeFormat(iso=ISO.DATE) private Date graduationDay; @NumberFormat(style = Style.CURRENCY) private Integer myNumber1; @NumberFormat(pattern = "###,###") private Double myNumber2; }
Spring also allows you to define your own type converter, which you must use in conjunction with spring conversionservice
For example, if you have such a color class
public class Color { private String colorString; public Color(String color){ this.colorString = color; } }
You can define a color converter like this
public class StringToColor implements Converter<String,Color> { public Color convert(String source) { if(source.equal("red") { return new Color("red"); } if(source.equal("green") { return new Color("green"); } if(source.equal("blue") { return new Color("blue"); } // etc return null; } }
To see more information about converters, look at this and check this to see the difference between converters and property editors