Hibernate:更新关联实体

发布于 2024-12-27 17:40:48 字数 1983 浏览 0 评论 0原文

以下问题与这些技术相关:

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风念你 2025-01-03 17:40:48

我可以使用这些对象将“子一”的父级从“父二”更新为“父一”吗?

简短的回答:是的。

浏览器发送JSON到服务器; Spring接收请求并将其映射到update(); Spring 检查 Content-Type 标头并确定响应正文包含 application/json; Spring 检查其 messageConverters 以查看是否有任何可以处理 JSON 的转换器; Spring 找到 Jackson 并将 JSON 转换为您的 Child 对象;该对象被传递给 update() ,您可以在其中对其进行操作,例如,通过 saveOrUpdate() 将其保存在 hibernate 中。

只要 parent 字段是关系的拥有方(从您给出的模型来看确实如此),“子一”与其父级之间的关系就会更新。为父级提供的名称可能会被提交;这取决于您的级联规则。

内部发生的事情是 Jackson 创建新的 ParentChild 对象,并且 hibernate 根据 ID 查找现有实体;父 ID 已存在,因此 saveOrUpdate() 选择更新。

Can I update the parent of "child one" from "parent two" to "parent one" by using these objects?

Short answer: yes.

The browser sends JSON to the server; Spring receives the request and maps it to update(); Spring examines the Content-Type header and determines that the response body contains application/json; Spring checks in its messageConverters to see if you have any converter which can handle JSON; Spring finds Jackson and converts the JSON into your Child object; this object is passed in to update() where you can do operations on it, for example, saving it with hibernate via saveOrUpdate().

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 and Child objects and hibernate finds the existing entities based on the IDs; the parent ID already exists, so saveOrUpdate() chooses an update.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文