[英]JPQL one-to-many query
Entity A has a collection of some entities of type B. Type B is not aware (and should not be) of what type A is. 实体A有一些类型B的实体的集合。类型B不知道(也不应该)类型A是什么。 I'd like to query for A which has at least one B with certain property.
我想查询具有至少一个具有某些属性的B的A。
As for the mappings I use simple one-to-many and a join-column inside it at the A mapping side. 至于映射,我在A映射端使用了简单的一对多和一个连接列。
Yet, when I do this using JPQL I cannot really use the JPA-generated column a_id since it's not a field of B - and therefore results in PropetyNotFound - jpa exception. 但是,当我使用JPQL执行此操作时,我不能真正使用JPA生成的列a_id,因为它不是B字段-因此会导致PropetyNotFound-jpa异常。 Is there a method to use that column not having to define a property in the entity itself?
有没有一种方法可以使用该列而不必在实体本身中定义属性?
Entity A mapping: 实体A映射:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
<package>com.example</package>
<entity class="B" access="FIELD">
<attributes>
<id name="seqid">
<generated-value strategy="AUTO" />
</id>
<basic name="identifier" />
<basic name="payload" />
<version name="version" />
</attributes>
</entity>
<entity class="A" access="FIELD">
<attributes>
<id name="seqid">
<generated-value strategy="AUTO" />
</id>
<one-to-many name="bset">
<join-column name="a_id"
referenced-column-name="seqid" />
<cascade>
<cascade-all />
</cascade>
</one-to-many>
</attributes>
</entity>
</entity-mappings>
Thanks! 谢谢!
Just make sure you're using join clause on the set. 只要确保您在集合上使用join子句即可。 Without it query will be constructed with no errors but this will fail with a strange error when executed.
没有它,查询将被构造为没有错误,但是执行时会失败,并出现一个奇怪的错误。 So the correct query would look like
所以正确的查询看起来像
select a from A a join a.setB b where b in (select c from B where B.x = 'x');
this will be successful but 这将是成功的,但是
select a from A a where a.setB in (select c from B where B.x = 'x');
will fail with no apparent reason. 没有明显的原因会失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.