Can I use select from derived tables with JPA?

Calabacin

I have a table 'asset' that has a 1 to n relation with a table called 'asset_properties' that contains a list of properties of that asset, and a many to many (using an intermediate table 'asset_has_tag') relation with table 'tags' that contains a list of tags.

I need to get a list of assets that have BOTH some specific tags AND some property values.

If I needed assets that have EITHER some tags OR some properties I could simply add both results of the following jpa queries to a java.util.Set.

I can get what I want with native SQL using the following query.


The Native SQL Query:

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

The JPA Queries:

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"

The Entities (empty constructors, getters and setters removed)

@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);
    }
}

EDIT: Any alternatives since JPA doesn't support it yet?

Ish

Selecting from derived tables (or having subqueries in the FROM clause) is not currently supported by JPA.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can I use one select statement to get multiple rows from 2 tables with FK

From Dev

How can I optimize a query with derived tables?

From Dev

How can I select from different tables in one MySQL query?

From Dev

How can I SELECT a row with MAX(value) from a derived table where the values are all calculated sums?

From Dev

how do i fetch derived/calculated column from database view or Procedure in Spring Boot using JPA/Hibernate and use it along with predefined columns?

From Dev

Why can't I use alias from a base class in a derived class with templates?

From Dev

How can I query specific columns from 2 tables inside my objects using JPA 2.0?

From Dev

How to use derived tables in postgres

From Dev

Can I use CRTP with multiple derived classes, and use them polymorphically?

From Dev

I can "pickle local objects" if I use a derived class?

From Dev

How can I select from items from two tables with one item only have one value

From Dev

How can I use a string list from a select statement in a NOT IN clause

From Dev

How can I use an NSPredicate to select an exact phrase from an array?

From Dev

Can I use JQuery selectors to select from a particular node object?

From Dev

Can I use the result of a select statement from mysql to compare it with a variable?

From Dev

Can I use SELECT from dataframe instead of creating this temp table?

From Dev

How can I use an AngularJS filter to select rows from a range?

From Dev

Can I use JQuery selectors to select from a particular node object?

From Dev

How can I use an NSPredicate to select an exact phrase from an array?

From Dev

Can I use the result of a select statement from mysql to compare it with a variable?

From Dev

Can I use SELECT from dataframe instead of creating this temp table?

From Dev

How can I use a local variable in SELECT FROM?

From Dev

MySQL : Can I use one SELECT ... FOR UPDATE to "protect" multiple tables? ( LOCKING )

From Dev

How can I use Dapper with a SELECT stored procedure containing an INNER JOIN between two tables?

From Dev

MySQL : Can I use one SELECT ... FOR UPDATE to "protect" multiple tables? ( LOCKING )

From Dev

Use SELECT NEW CONSTRUCTOR with parameter not from tables

From Dev

Why would you use a JOIN, if you can select from multiple tables without a JOIN?

From Dev

How can I implement a Singleton class that can be derived from in WPF?

From Dev

How can I implement a Singleton class that can be derived from in WPF?

Related Related

  1. 1

    Can I use one select statement to get multiple rows from 2 tables with FK

  2. 2

    How can I optimize a query with derived tables?

  3. 3

    How can I select from different tables in one MySQL query?

  4. 4

    How can I SELECT a row with MAX(value) from a derived table where the values are all calculated sums?

  5. 5

    how do i fetch derived/calculated column from database view or Procedure in Spring Boot using JPA/Hibernate and use it along with predefined columns?

  6. 6

    Why can't I use alias from a base class in a derived class with templates?

  7. 7

    How can I query specific columns from 2 tables inside my objects using JPA 2.0?

  8. 8

    How to use derived tables in postgres

  9. 9

    Can I use CRTP with multiple derived classes, and use them polymorphically?

  10. 10

    I can "pickle local objects" if I use a derived class?

  11. 11

    How can I select from items from two tables with one item only have one value

  12. 12

    How can I use a string list from a select statement in a NOT IN clause

  13. 13

    How can I use an NSPredicate to select an exact phrase from an array?

  14. 14

    Can I use JQuery selectors to select from a particular node object?

  15. 15

    Can I use the result of a select statement from mysql to compare it with a variable?

  16. 16

    Can I use SELECT from dataframe instead of creating this temp table?

  17. 17

    How can I use an AngularJS filter to select rows from a range?

  18. 18

    Can I use JQuery selectors to select from a particular node object?

  19. 19

    How can I use an NSPredicate to select an exact phrase from an array?

  20. 20

    Can I use the result of a select statement from mysql to compare it with a variable?

  21. 21

    Can I use SELECT from dataframe instead of creating this temp table?

  22. 22

    How can I use a local variable in SELECT FROM?

  23. 23

    MySQL : Can I use one SELECT ... FOR UPDATE to "protect" multiple tables? ( LOCKING )

  24. 24

    How can I use Dapper with a SELECT stored procedure containing an INNER JOIN between two tables?

  25. 25

    MySQL : Can I use one SELECT ... FOR UPDATE to "protect" multiple tables? ( LOCKING )

  26. 26

    Use SELECT NEW CONSTRUCTOR with parameter not from tables

  27. 27

    Why would you use a JOIN, if you can select from multiple tables without a JOIN?

  28. 28

    How can I implement a Singleton class that can be derived from in WPF?

  29. 29

    How can I implement a Singleton class that can be derived from in WPF?

HotTag

Archive