Hibernate:更新关联实体
以下问题与这些技术相关:
- Hibernate 4 或 3
- Spring 3.1
- Jackson
- Ext JS 4
假设我有两个表,即父表和子表。
父类如下:
class Parent {
private int id;
private String name;
//getter and setter methods
}
子类如下:
class Child {
private int id;
private String name;
@ManyToOne // uni-directional many-to-one association to Parent
@JoinColumn(name = "parent_id")
private Parent parent;
//getter and setter methods
}
现在,数据库上已经有这些数据:
表:parent
id name
===================
1 parent one
2 parent two
表:child
id name parent_id
=============================
1 child one 2
问题是:我可以更新“child”的父类吗?一”从“父二”到“父一” 通过使用这些对象:
Parent newParent = new Parent();
newParent.setId(1);
Child child = new Child();
child.setId(1);
child.setParent(newParent);
session.saveOrUpdate(child);
有关附加信息,子对象由 Spring MVC+Jackson 从 ExtJS 从浏览器发送的 JSON 自动映射。
JSON 格式如下:
{id: 1, name: "child one", parent: {id: 1, name: "parent one"}}
所以,上面的 child 和 newParent 对象实际上是由 Spring MVC+Jackson 隐式创建和设置的。
所以 Spring MVC 控制器内部的实际代码如下:
@RequestMapping(value = "child/update", method = RequestMethod.POST)
@ResponseBody
public Map<String, ?> update(@RequestBody Child child) {
// validation for child object
session.saveOrUpdate(child)
// prepare response
}
这里也很重要,从浏览器发送的 JSON 可能包含新父级的名称
... parent: {id: 1, name: "parent one"} ...
虽然这些“name”属性也可能由 Spring 映射,但它Hibernate 应该省略,因为我只想更新“子一”记录,而不是更新(“父一”记录的)名称。
我知道使用 HQL 可以轻松完成。但由于浏览器发送 JSON,Spring 可以自动将 JSON 映射到对象,因此使用 session.saveOrUpdate() 而不是 HQL 会更容易。
有什么建议如何做所有这些吗?
非常感谢。
The following question is related with these technologies:
- Hibernate 4 or 3
- Spring 3.1
- Jackson
- Ext JS 4
Suppose that I have two tables, say parent and child.
The parent class is as follows:
class Parent {
private int id;
private String name;
//getter and setter methods
}
The child class is as follows:
class Child {
private int id;
private String name;
@ManyToOne // uni-directional many-to-one association to Parent
@JoinColumn(name = "parent_id")
private Parent parent;
//getter and setter methods
}
Now, there are already these data on the database:
Table: parent
id name
===================
1 parent one
2 parent two
Table: child
id name parent_id
=============================
1 child one 2
The question is: Can I update the parent of "child one" from "parent two" to "parent one"
by using these objects:
Parent newParent = new Parent();
newParent.setId(1);
Child child = new Child();
child.setId(1);
child.setParent(newParent);
session.saveOrUpdate(child);
For additional information, the child object is auto mapped by Spring MVC+Jackson from JSON which is sent from browser by ExtJS.
The JSON format is as follows:
{id: 1, name: "child one", parent: {id: 1, name: "parent one"}}
So, the child and newParent objects above is actually created and set implicitly by Spring MVC+Jackson.
So the actual code inside Spring MVC controller is as follows:
@RequestMapping(value = "child/update", method = RequestMethod.POST)
@ResponseBody
public Map<String, ?> update(@RequestBody Child child) {
// validation for child object
session.saveOrUpdate(child)
// prepare response
}
Also important here, the JSON sent from browser may be contains the name of new parent
... parent: {id: 1, name: "parent one"} ...
Although those "name" property might also be mapped by Spring, it should be ommitted by Hibernate, because I just want to update "child one" record, not update the name (of "parent one" record).
I know that it can be done easily by using HQL. But because the browser sends JSON, and Spring can map the JSON to object automatically, so it would be easier to use session.saveOrUpdate() instead of HQL.
Any suggestion how to do all these ?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答:是的。
浏览器发送JSON到服务器; Spring接收请求并将其映射到
update()
; Spring 检查Content-Type
标头并确定响应正文包含application/json
; Spring 检查其messageConverters
以查看是否有任何可以处理 JSON 的转换器; Spring 找到 Jackson 并将 JSON 转换为您的Child
对象;该对象被传递给update()
,您可以在其中对其进行操作,例如,通过saveOrUpdate()
将其保存在 hibernate 中。只要
parent
字段是关系的拥有方(从您给出的模型来看确实如此),“子一”与其父级之间的关系就会更新。为父级提供的名称可能会被提交;这取决于您的级联规则。内部发生的事情是 Jackson 创建新的
Parent
和Child
对象,并且 hibernate 根据 ID 查找现有实体;父 ID 已存在,因此saveOrUpdate()
选择更新。Short answer: yes.
The browser sends JSON to the server; Spring receives the request and maps it to
update()
; Spring examines theContent-Type
header and determines that the response body containsapplication/json
; Spring checks in itsmessageConverters
to see if you have any converter which can handle JSON; Spring finds Jackson and converts the JSON into yourChild
object; this object is passed in toupdate()
where you can do operations on it, for example, saving it with hibernate viasaveOrUpdate()
.As long as the
parent
field is the owning side of the relationship (and it is, from the model you've given), the relationship between "child one" and its parents will be updated. The name provided for the parent might be committed; this depends upon your cascading rules.What happens internally is that Jackson creates new
Parent
andChild
objects and hibernate finds the existing entities based on the IDs; the parent ID already exists, sosaveOrUpdate()
chooses an update.