PDA

View Full Version : Submitting Entity with Association - Hibernate + SpringMVC



guilherme737
28 Jan 2011, 5:44 AM
Hello everyone.

Doubts simple. Eg. I'm making a record and got the following entity in my Java class:


public class District {

@ Id
@ GeneratedValue (strategy = GenerationType.AUTO)
private Long id;

@ Column
private String description;

@ OneToOne
private City city;

/****** gets & sets ******/

}I am using Hibernate + SpringMVC and am using it to facilitate Ext.data.HttpProxy in my js is that when I have to insert the following code fragment:

var d = new District ({
description: descriptionDistrictTxt.getValue (),
City: [{id: 1}]
});

storeDistrict.insert (0, d);
storeDistrict.save ();and hosted as follows in my controller:

public @ResponseBody Map<String,? extends Object> create(@RequestParam Object data) throws Exception {...}I could do it with a cast on the object using:

JsonObject = JSONObject.fromObject JSONObject (data);
District newDistrict = (District) JSONObject.toBean (jsonObject, District.class);the problem is that the attributes of the district come up correctly, but the association does not City, he is not even being sent in JSON object, the cast happens to the district but the City is NULL. Does anyone have any idea, tip to share? Not found on the web using anything like that to submit an object with associations. If there is a possible solution without DWR better. I hope I have expressed myself well. Thank you all.

Guilherme Mendes