简体   繁体   English

如何在JPA中查询组合对象?

[英]How do I query an composed Object in JPA?

I want to now if can JPQL retrieve the composed object. 我现在想知道JPQL是否可以检索组成的对象。 ObjectA has ObjectB (Aggregation). ObjectA具有ObjectB(聚合)。 Can I query ObjectB where there is not link with ObjectA. 我可以查询没有与ObjectA链接的ObjectB吗? NAtive Query would be like this: 原始查询将是这样的:

SELECT * FROM OBJECTB WHERE  ID_OBJECTA = NULL

Sometimes JPA creates an associative entity, but I assumed here just added ID_OBJECTA as the reference to his aggregater. 有时,JPA创建一个关联实体,但是我假设在这里只是添加ID_OBJECTA作为对其聚合器的引用。

Thanks for your help. 谢谢你的帮助。

Class ClassA{
public ClassB b;
}

ClassB{
public String data;

} }

I know I can query like this: 我知道我可以这样查询:

Select a from ClassA a join a.b bb//Given a retrieve b

I want to do this: 我想做这个:

Select b from ClassB b join ...// ClassB doenst have an a Attribute

How can i query ClassA from ClassB 如何从ClassB查询ClassA

What you want is a bi-directional One-to-One association. 您需要一个双向的一对一关联。 You have two options in this case Lazily or Eagerly loaded. 在这种情况下,您有两个选项:懒惰或急切加载。 Neither require an explicit join in your JQL unless you want to override lazy loading with eager loading. 除非您要用急切加载覆盖延迟加载,否则都不需要在JQL中进行显式联接。 This is of course predicated on the fact that your annotations/xml defining the relationship between these objects is setup correctly. 这当然是基于以下事实:正确定义了定义这些对象之间关系的批注/ xml。

You will need a property in ClassB for type ClassA to persist changes in ClassB's relationship independent of ClassA. 您将在ClassB中需要一个属性,用于ClassA类型,以保持与ClassA无关的ClassB关系中的更改。 This property will also allow child queries on ClassB to populate the parent ClassA member with data(eager) or query stubs(lazy) 此属性还将允许在ClassB上的子查询向父ClassA成员填充数据(渴望)或查询存根(惰性)

public ClassA{
   ClassB Child;
}

public ClassB{
   ClassA parent;
   String data;
}

Check out this wiki books entry it walks you through setting this up. 查看此Wiki图书条目,它会引导您完成设置。 One To One It oulines a very similiar scenario in dealing with a Person with an Address and laying how to get the person by querying the Address. 一对一它在与具有AddressPerson打交道并提出如何通过查询地址来获得该人的过程中提供了一个非常相似的场景。

Note: I just read that you are trying to do this without an association. 注意:我刚刚读到您正尝试在没有关联的情况下执行此操作。 Like other posters have noted its trivial to add this relationship, If its lazy it adds almost no overhead. 就像其他张贴者指出的那样,添加这种关系是微不足道的。如果它比较懒,则几乎不会增加任何开销。 The only reason I can think of not to do this is some misguided attempt to avoid polluting classB with references to ClassA. 我能想到的不这样做的唯一原因是为了避免对ClassA的引用污染classB而进行了一些误导的尝试。 Remember "Worse Is Better" Simplicity -> Correctness --> Consistency -> Completeness. 记住“越差越好”的简单性->正确性->一致性->完整性。 Do it the simplest way that solves you're problem and no simpler. 以最简单的方式解决您的问题,而不是更简单。

Your first query, AFAIK, is not possible to implement without the association from B to A. The fact that you have such a query is an indication that this association should exist. 没有从B到A的关联,就无法实现您的第一个查询AFAIK。您具有这样的查询的事实表明该关联应该存在。

The ugly workaround is to use this query: 丑陋的解决方法是使用以下查询:

select b from B b where b.id not in (select b.id from A a join a.b b)

Other queries than the one you want are possible though: 但是,除了您想要的查询之外,还可以进行其他查询:

select b from A a join a.b b where ...

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

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