我可以将派生表中的选择与 JPA 一起使用吗?

Cal*_*cin 5 java hibernate jpa

我有一个表“asset”,它与名为“asset_properties”的表具有 1 到 n 的关系,该表包含该资产的属性列表,并且与表“tags”具有多对多(使用中间表“asset_has_tag”)关系' 包含标签列表。

我需要获取具有一些特定标签和一些属性值的资产列表。

如果我需要具有某些标签或某些属性的资产,我可以简单地将以下 jpa 查询的两个结果添加到 java.util.Set 中。

我可以使用以下查询通过本机 SQL 获得我想要的内容。


本机 SQL 查询:

SELECT a.*
FROM (SELECT ap.* 
    FROM asset ap JOIN asset_property p
    WHERE p.value LIKE "%asd%" OR ap.name LIKE "%asd%" OR ap.description LIKE "%asd%"
) a
JOIN asset_has_tag r, tag h
WHERE a.uuid = r.asset_id AND h.uuid=r.tag_id AND h.category IN ("asd", "qwe", "zxc")
GROUP BY a.uuid
Run Code Online (Sandbox Code Playgroud)

JPA 查询:

String findByAssetAndTagValues =
"select distinct(a) from Asset a join a.Tags h where a.name like :assetname or a.description like :assetdescription and h.name in :tagnames and h.category in :tagcategories and h.uuid=:taguuids"

String findAssetsWithPropertyByValue =
"select distinct(a) from Asset a join a.assetProperties p where p.value like :value"
Run Code Online (Sandbox Code Playgroud)

实体(空构造函数、getter 和 setter 已删除)

@Entity
public class Asset implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "myUUID")
    @GenericGenerator(name="myUUID", strategy="uuid2")
    @Column(unique = true, nullable = false, length = 16)
    private UUID uuid;

    private String description;

    // bi-directional many-to-one association to assetProperty
    @OneToMany(mappedBy = "asset", fetch = FetchType.LAZY)
    private List<AssetProperty> assetProperties;

    // bi-directional many-to-many association to tag
    @ManyToMany(mappedBy = "assets", fetch = FetchType.LAZY)
    private Set<tag> tags;

    @Override
    public boolean equals(Object obj) {
        return (obj != null && obj instanceof Asset && ((Asset)obj).getUuid().equals(uuid));
    }
}

@Entity
public class AssetProperty implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique=true, nullable=false)
    private int id;

    @Column(nullable=false, length=255)
    private String name;

    @Column(length=512)
    private String value;

    //bi-directional many-to-one association to Asset
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="asset_id", nullable=false)
    private Asset asset;
}

@Entity
@Table(name = "hardtag")
public class Hardtag implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @Column(unique = true, nullable = false)
    private UUID uuid;

    @Column(length = 255)
    private String category;

    @Column(nullable = false, length = 255)
    private String name;

    // bi-directional many-to-many association to Asset
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "asset_has_tag", joinColumns = { @JoinColumn(name = "tag_id", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "asset_id", nullable = false) })
    private Set<Asset> assets;

    @Override
    public boolean equals(Object obj) {
        return obj instanceof Hardtag && ((Hardtag) obj).getUuid().equals(uuid);
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:既然 JPA 还不支持,还有其他选择吗?

EDIT2(2023):看来现在这是可能的,尽管我还没有测试过:https://openjpa.apache.org/builds/3.2.2/apache-openjpa/docs/#jpa_langref_subqueries

Ish*_*Ish 4

JPA目前不支持从派生表中进行选择(或在 FROM 子句中使用子查询)。