在Java中的交易中检索新实体(未代理)

发布于 2025-01-30 22:45:24 字数 1043 浏览 2 评论 0原文

在Java中使用Hibernate在Java中的交易中检索新的(未代理)实体

,可以在不检索特定实体的新实例的情况下而不检索冬眠代理的新实例吗?

假设我们有一种类似的方法

@Transaction 
public void parentMethod(String id){
    childMethod(id); //perform operation on entity associated with id
    repository.findById(id);//this needs to retrieve entity from new session
}

,而孩子的嵌套方法:

public void childMethod(String id){
    //retrieves and perform save operation on entity associated with the id
    EntityTest test=repository.findById(id);
    .........update operations..............
    repository.save(test);
}

认为我需要在 parentmethod 的结尾执行findbyid ,以订购实体内部的集合(用@Orderby)注释)

如果我没有错,则当孩子表演(例如)在实体上保存操作时,也可以在会话中代表结果,如果我们(在parendMethod中)执行FindbyId,则它会检索该实体而不是一个新的实体,因此:

  1. 用findbyid(订购嵌套收藏)检索实体 - >代理结果
  2. 带有保存的儿童用途更新实体(未订购嵌套集合) - >用结果
  3. parendMethod执行FindbyId(未订购嵌套集合) - >退休代理实体

如何在会话外检索新实例以确保收集订单?考虑到我们在交易中,如果上次Findbyid出现问题,我需要保存操作是回滚吗?

谢谢!

Retrieve NEW (not proxied) entity inside a transaction in Java with Hibernate

In java, it's possible to retrieve a new instance of a specific entity without retrieving the ones that hibernate proxies ?

Suppose that we have a method like this

@Transaction 
public void parentMethod(String id){
    childMethod(id); //perform operation on entity associated with id
    repository.findById(id);//this needs to retrieve entity from new session
}

And the child nested method like this:

public void childMethod(String id){
    //retrieves and perform save operation on entity associated with the id
    EntityTest test=repository.findById(id);
    .........update operations..............
    repository.save(test);
}

Consider that i NEED to perform the findById, at the end of the parentMethod, to order the collections inside the entity (annotated with @OrderBy)

If i'm not wrong, when childMethod perform (for example) save operation on the entity, also proxies the result in the session, and if we (in the parentMethod) perform the findById it retrieves that entity and not a new one, so:

  1. childMethod retrieve entity with findById (nested collections are ordered)--> proxies result
  2. childMethod update entity with save (nested collections are not ordered)--> update the proxy with the result
  3. parentMethod perform a findById (nested collections are not ordered)--> retireves the proxy entity

How can i retrieve a new instance outside the session to ensure the collections order? considering that we are inside a transaction and if something goes wrong with the last findById i need the save operations to be rollback?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文