Java JPQL查询不返回子实体的结果
不幸的是,我的数据库结构超出了我的控制范围。我试图编写一个jpql查询来查询使用父实体但不返回任何值的值列表 班级 这个类可能是问题的根源,因为它有一个标记用法集合,所有子类都应该将其作为一个集合,但数据库正在为所有继承的类型创建一个单独的联接表。发生这种情况是因为每个类都有表吗Java JPQL查询不返回子实体的结果,java,jpa,eclipselink,jpql,Java,Jpa,Eclipselink,Jpql,不幸的是,我的数据库结构超出了我的控制范围。我试图编写一个jpql查询来查询使用父实体但不返回任何值的值列表 班级 这个类可能是问题的根源,因为它有一个标记用法集合,所有子类都应该将其作为一个集合,但数据库正在为所有继承的类型创建一个单独的联接表。发生这种情况是因为每个类都有表吗 @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class Organization extends DomainEnti
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Organization extends DomainEntity implements Serializable {
@ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
private List<TagUsage> tagUsages = new ArrayList<TagUsage>();
public Organization() {
}
...
@Entity
@Table(name = "tag_usage", indexes = {
@Index(columnList = "id"),
@Index(columnList = "name")
})
public class TagUsage extends DomainEntity implements Serializable {
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name = "tagbag_id", referencedColumnName = "id")
private TagBag tagBag;
@ManyToMany(mappedBy = "tagUsages")
private List<Organization> organizations;
...
Postgres组织表列表的输出
\dt
| public | organization | table | |
| public | organization_tag_usage | table | |
| public | orgcustomer | table | |
| public | orgcustomer_tag_usage | table | |
| public | orgdevice | table | |
| public | orgdevice_tag_usage | table | |
| public | orgmasteragent | table | |
| public | orgmasteragent_tag_usage | table | |
| public | orgsite | table | |
| public | orgsite_tag_usage | table | |
| public | orgsystem | table | |
| public | orgsystem_tag_usage | table | |
正如您在这里看到的,organization_tag_usage完全为空,但查询应该使用的实际联接表是orgsystem_tag_usage
select * from organization_tag_usage;
SELECT 0
select * from orgsystem_tag_usage
+----------------+----------------+
| orgsystem_id | tagusages_id |
|----------------+----------------|
| 1 | 10 |
| 1 | 16 |
| 1 | 17 |
| 1 | 18 |
| 3 | 19 |
| 3 | 20 |
| 2 | 23 |
| 1 | 25 |
| 1 | 26 |
| 1 | 27 |
| 2 | 30 |
| 1 | 38 |
| 1 | 39 |
| 1 | 52 |
| 3 | 53 |
| 3 | 54 |
+----------------+----------------+
SELECT 16
这是eclipselink中的一个bug吗?我很惊讶您没有得到异常,因为JPA不允许使用别名连接回迁:刚刚检查过,EL 2.4中添加了支持。你能显示生成的SQL吗?谢谢Chris的回复,是的,我确实在ide中发现了一个错误。但eclipselink在本地支持这一点。我也尝试过在没有连接抓取的情况下执行此操作,但问题仍然存在。我已经使用sql诊断了这个问题,并将在一秒钟内更新该问题。它应该使用organization_tag_usages表,因为这是您告诉u.organizations mapping要使用的。organizations映射指定它由Organization.tagUsages映射进行映射,该映射使用JPA默认的“Organization\u tag\u usages”表。但它也为所有子对象创建*\u tag\u usages表。保存组织时,引用标记用法将其添加到后代联接表中。因此,例如,如果将标记用法添加到从组织继承的OrgSystem,则实际上是在OrgSystem_tag_用法表中添加引用,而不是在组织_tag_用法中添加引用。我还将Organization表转换为抽象表,在删除并重新启动后,数据库重新创建了Organization和Organization_tag_usage表。这似乎很奇怪。它是一个@实体,但它是抽象的。还有一个问题让我相信为什么它要为所有子实体创建多个连接表。为每个表创建一个生成ID的序列是正常的吗。这就解释了为什么它认为它需要创建子联接表,因为它无法将唯一id放入组织标记用法中。如果表有一个生成id的序列,那么它可以使用organization_tag_用法,因为知道从organization继承的每个对象都有一个唯一的id。我没有创建这个数据库结构,所以我不知道思考过程。
\dt
| public | organization | table | |
| public | organization_tag_usage | table | |
| public | orgcustomer | table | |
| public | orgcustomer_tag_usage | table | |
| public | orgdevice | table | |
| public | orgdevice_tag_usage | table | |
| public | orgmasteragent | table | |
| public | orgmasteragent_tag_usage | table | |
| public | orgsite | table | |
| public | orgsite_tag_usage | table | |
| public | orgsystem | table | |
| public | orgsystem_tag_usage | table | |
select * from organization_tag_usage;
SELECT 0
select * from orgsystem_tag_usage
+----------------+----------------+
| orgsystem_id | tagusages_id |
|----------------+----------------|
| 1 | 10 |
| 1 | 16 |
| 1 | 17 |
| 1 | 18 |
| 3 | 19 |
| 3 | 20 |
| 2 | 23 |
| 1 | 25 |
| 1 | 26 |
| 1 | 27 |
| 2 | 30 |
| 1 | 38 |
| 1 | 39 |
| 1 | 52 |
| 3 | 53 |
| 3 | 54 |
+----------------+----------------+
SELECT 16