我可以使用JPA派生表中的select吗?

时间:2015-10-09 12:41:42

标签: java hibernate jpa

我有一张桌子资产'与名为" asset_properties'的表有1到n的关系其中包含该资产的属性列表,以及多个对象(使用中间表" 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

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"

实体(删除了空构造函数,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);
    }
}
编辑:任何替代方案,因为JPA还不支持它吗?

1 个答案:

答案 0 :(得分:4)

JPA目前不支持从派生表中选择(或在FROM子句中包含子查询)。