简体   繁体   English

JPA实体的集合成员分离或不受管理

[英]JPA entity's collection members get detached or not managed

TGIF guys, but I am still stuck on one of my projects. TGIF伙计们,但我仍然停留在我的一个项目上。 I have two interfaces IMasterOrder and IOrder . 我有两个接口IMasterOrderIOrder One IMasterOrder may have a Collection of IOrder . 一个IMasterOrder可以具有收藏IOrder So there can be several MasterOrder entity classes and Order entity classes who implements the interfaces. 因此,可以有几个实现接口的MasterOrder实体类和Order实体类。

To simplify the coding, I create IMasterOrder and IOrder objects everywhere, but when it needs to specify the concrete type then I just cast IMasterOrder object to the class type. 为了简化编码,我在各处创建了IMasterOrderIOrder对象,但是当需要指定具体类型时,只需将IMasterOrder对象转换为类类型。

The problem is, this makes master class always return null about its orders. 问题是,这使主类始终对其订单返回null。 I am very curious about how JPA works with polymorphism in general? 我对JPA一般如何与多态一起工作感到很好奇?

Update 更新资料

Sorry for the early confusion. 抱歉给您带来的混乱。 Actually the question is much simpler 其实问题要简单得多

Actually the entity class is something like this 实际上,实体类是这样的

public class MasterOrder implements IMasterOrder {

// Relationships

@OneToOne(mappedBy = "masterOrder")
private OrderCustomFields customFields;

@OneToMany(mappedBy = "masterOrder")
private List<OrderLog> logs;

@OneToMany(mappedBy = "masterOrder")
private Collection<Order> orders;

// Fields...

And the finder method to get the Master order entity instance is like this 获取主订单实体实例的finder方法是这样的

 public static MasterOrder findMasterOrder(String id) {
    if (id == null || id.length() == 0) return null;
    return entityManager().find(MasterOrder.class, id);
}

However, the MasterOrder instance from this finder method returns customFields and logs and orders which are all null. 但是,此finder方法中的MasterOrder实例返回customFields以及所有均为null的日志和订单。 So how to fix this? 那么如何解决呢? Thanks in advance. 提前致谢。

When you access logs and orders, is Master still a part of an active persistence context? 当您访问日志和订单时,Master仍然是活动持久性上下文的一部分吗? ie: Has the EntityManager that found the Master Entity been closed or cleared? 即:找到或清除了发现主实体的EntityManager吗? If yes, everything is working as expected. 如果是,则一切正常。

For giggles, you could try changing the fetch attribute on logs and orders to EAGER ... this will help pinpoint if there is something else bad going on. 对于傻笑,您可以尝试将日志和订单的fetch属性更改为EAGER ...这将有助于查明是否还有其他不良情况。

@OneToMany(mappedBy = "masterOrder", fetch = FetchType.LAZY)
private List<OrderLog> logs;

@OneToMany(mappedBy = "masterOrder", fetch = FetchType.LAZY)
private Collection<Order> orders;

Sounds like a problem with your mapping.. I don't think empty collections should be NULL, they should either be an empty list (if initialized), or a proxy that will be initialized when you read from it. 听起来好像是映射有问题。.我不认为空集合应该为NULL,它们应该不是空列表(如果已初始化),或者是从中读取时将初始化的代理。 If you leave the transaction and try to read from the collection, it SHOULD throw a lazy initialization exception. 如果您离开事务并尝试从集合中读取数据,则应该抛出一个延迟的初始化异常。 In either case, you should include all relevant classes in the question to provide further information. 无论哪种情况,都应在问题中包括所有相关的类别,以提供更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM