Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataBinding fails when nested entity implements Serializable [DATACMNS-517] #985

Closed
spring-projects-issues opened this issue Jun 5, 2014 · 6 comments
Assignees
Labels
in: core type: bug

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Jun 5, 2014

Pablo Krause opened DATACMNS-517 and commented

Input:
http://localhost/foo?name=MyFoo&bar.name=MyBar

Expected result:

{
  "name" : "MyFoo",
  "bar" : {
    "name" : "MyBar"
  }
}

Actual result:

Field error in object 'foo' on field 'bar': rejected value [org.sample.core.Bar@0]; codes [typeMismatch.foo.bar,typeMismatch.bar,typeMismatch.org.sample.core.Bar,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [foo.bar,bar]; arguments []; default message [bar]]; default message [Failed to convert property value of type 'org.sample.core.Bar' to required type 'org.sample.core.Bar' for property 'bar'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type org.sample.core.Bar to type @javax.persistence.ManyToOne org.sample.core.Bar for value 'org.sample.core.Bar@0'; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Provided id of the wrong type for class org.sample.core.Bar. Expected: class java.lang.Long, got class org.sample.core.Bar; nested exception is java.lang.IllegalArgumentException: Provided id of the wrong type for class org.sample.core.Bar. Expected: class java.lang.Long, got class org.sample.core.Bar]
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'foo' on field 'bar': rejected value [org.sample.core.Bar@0]; codes [typeMismatch.foo.bar,typeMismatch.bar,typeMismatch.org.sample.core.Bar,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [foo.bar,bar]; arguments []; default message [bar]]; default message [Failed to convert property value of type 'org.sample.core.Bar' to required type 'org.sample.core.Bar' for property 'bar'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type org.sample.core.Bar to type @javax.persistence.ManyToOne org.sample.core.Bar for value 'org.sample.core.Bar@0'; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Provided id of the wrong type for class org.sample.core.Bar. Expected: class java.lang.Long, got class org.sample.core.Bar; nested exception is java.lang.IllegalArgumentException: Provided id of the wrong type for class org.sample.core.Bar. Expected: class java.lang.Long, got class org.sample.core.Bar]
	at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:112) ~[spring-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]

The players:

@Entity public class Foo implements Serializable {
	@Id	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	private String name;
	@ManyToOne	private Bar bar;

	public Long getId() { return id; 	}
	public void setId(Long id) { this.id = id; }
	public Bar getBar() { return bar; }
	public void setBar(Bar bar) { this.bar = bar; }
	public String getName() {	 return name; }
	public void setName(String name) { this.name = name;}
}
@Entity public class Bar *implements Serializable* {
	@Id @GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	private String name;
	public Long getId() { return id; 	}
	public void setId(Long id) { this.id = id; }
	public String getName() { return name; }
	public void setName(String name) { this.name = name; }
}
@Controller @RequestMapping("/") public class SampleController {
    @ResponseBody
    @RequestMapping(value = "/foo", method = RequestMethod.GET)
    public Foo searchFoo(Foo foo) {
        return foo;
    }
}

The victim:
If you remove the "implements Serializable" from Bar you get the correct expected behavior.

Final comments:
In this particular case, I don't wan the DomainClassConverter to kick in, as explained in reference documentation section 1.4. I just want vanilla data binding, but for some reason the DomainClassConverter gets confused.
For example, If I remove spring data's magic by removing the @EnableSpringDataWebSupport annotation in my setup, conversion works as expected.


Affects: 1.5.2, 1.7.2 (Codd SR2), 1.8 GA (Dijkstra)

Attachments:

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jun 5, 2014

Pablo Krause commented

I edited the issue, since further investigating, I noticed that the problem was that the nested bean was Serializable, and not that it inherited from a BaseEntity

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jun 5, 2014

Pablo Krause commented

Sorry for messing up the affected version.... didn't notice that Oliver made changes... I thought I filed it incorrectly

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jun 12, 2014

Thomas Darimont commented

Attached Spring Boot Sample Application

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jun 12, 2014

Thomas Darimont commented

Hello Pablo,

I gave this a spin, but I couldn't reproduce your issue. Would you mind giving the attached example app a try - may be you can reproduce the issue.

Cheers,
Thomas

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jun 26, 2014

Oliver Drotbohm commented

Any feedback so far?

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jul 18, 2014

Oliver Drotbohm commented

Resolving for now. Feel free to re-open if further feedback can be given

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core type: bug
Projects
None yet
Development

No branches or pull requests

2 participants