gpt4 book ai didi

hibernate - 使用延迟初始化属性分离 JPA 对象

转载 作者:行者123 更新时间:2023-12-04 17:58:28 24 4
gpt4 key购买 nike

有两个 JPA 实体:具有一对多关系的 User 和 Order。

/**
* User DTO
*/
@Entity
@Table(name="user")
public class User implements Serializable {
private static final long serialVersionUID = 8372128484215085291L;

private Long id;
private Set<Order> orders;

public User() {}

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequenceUser")
public Long getId() {
return this.id;
}
private void setId(Long id) {
this.id = id;
}

@OneToMany(mappedBy="user", cascade=CascadeType.PERSIST, fetch=FetchType.LAZY)
@LazyCollection(LazyCollectionOption.EXTRA)
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
}


/**
* Order DTO
*/
@Entity
@Table(name="order")
public class Order implements Serializable {
private static final long serialVersionUID = 84504362507297200L;

private Long id;
private User user;

public Order() {
}

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequenceOrder")
public Long getId() {
return this.id;
}
private void setId(Long id) {
this.id = id;
}

@ManyToOne
@JoinColumn(name="user_id")
public User getUser(){
return user;
}
public void setUser(User user){
this.user = user;
}
}

我在我的服务层类中使用这些实体,其中每个方法都在事务中运行。除了服务层类的方法必须返回这些实体的情况外,一切都很好。
@Transactional(readOnly=true)
public Set<Order> getOrders() {
Set<Order> orders = user.getOrders();

return orders;
}

该方法返回数据良好。但是当我尝试访问接收到的集合元素时,我捕获了异常:“org.hibernate.LazyInitializationException:未能延迟初始化角色集合:package.User.orders,没有 session 或 session 被关闭”。

所以,它被排除在外。我认为分离结果会解决我的问题,但像这样的技巧
@Transactional(readOnly=true)
public Set<Order> getOrders() {
Set<Order> orders = user.getOrders();

for(Order order: orders)
entityManager.detach(order);
return orders;
}

没有改变任何东西:(

对我来说,有关用户是否按订单参加的信息无关紧要。我只想使用这个集合而不打算修改它。

有谁能够帮助我? :)

最佳答案

This method returns data well. But when I try access to received collection elements I catch exception: "org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: package.User.orders, no session or session was closed".



该错误是自我解释的:您正在尝试加载(延迟)加载的集合,但不再有事件 session ,因为 User实例已分离。

So, it was excepted. I thought that detaching result will solve my problem, but trick like this



这不会改变任何事情。 EntityManager#detach(Object) 方法不加载任何东西,它从持久化上下文中删除传递的实体,使其分离。

It doesn't matter for me will info about users attend in set of orders or not. I just want to work with this set and not going to modify it.



您需要建立关联 EAGER(以便在检索用户时加载订单)或在您的服务中检索用户时使用 FETCH JOIN:
SELECT u
FROM User u LEFT JOIN FETCH u.orders
WHERE u.id = :id

Do I understand correctly that hibernate has no standard mechanism for force load all lazy associations of current object



Hibernate 有一个 Hibernate.initialize方法,但这显然不是标准的 JPA(对于可移植代码更喜欢 fetch join)。

I have no way to make hibernate ignore lazy associations of current object (set them as null)



什么?无视是什么意思?你为什么要这样做?

关于hibernate - 使用延迟初始化属性分离 JPA 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3642151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com