Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query @ElementCollection JPA

I have an Entity Transaction as following :

@Entity
class Transaction extends AbstractEntity<Long>{
        private static final long serialVersionUID = 7222139865127600245L;
        //other attributes

    @ElementCollection(fetch = FetchType.EAGER, targetClass = java.lang.String.class)
    @CollectionTable(name = "transaction_properties", joinColumns = @JoinColumn(name = "p_id"))
    @MapKeyColumn(name = "propertyKey")
    @Column(name = "propertyValue")
    private Map<String, String> properties;

    //getters and setters
}

So, my Database Table for transaction_properties is

mysql> desc transaction_properties;
+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| p_id          | bigint(20)   | NO   | PRI |         |       |
| propertyValue | varchar(255) | YES  |     | NULL    |       |
| propertyKey   | varchar(255) | NO   | PRI |         |       |
+---------------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

Now, I want to search entity Transaction with key and value.

    Path<Map<String, String>> propertiesPath = root.get("properties");
    Path<String> propertyKeyPath = propertiesPath.<String> get("propertyKey"); //where m getting error
    Path<String> propertyValuePath = propertyKeyPath.<String> get("propertyValue");
    p = cb.and(p, cb.and(cb.like(propertyKeyPath, "%" + searchTrxnKey + "%"), cb.like(propertyValuePath, "%" + searchTrxnValue + "%")));

But am getting error for Path<String> propertyKeyPath = propertiesPath.<String> get("propertyKey"); as following :

[...] threw an unexpected exception: org.springframework.dao.InvalidDataAccessApiUsageException: Illegal attempt to dereference path source [null]; nested exception is java.lang.IllegalArgumentException: Illegal attempt to dereference path source [null]

One of the reference I went through was : Spring Data JPA Tutorial Part Four: JPA Criteria Queries but no luck for me.

like image 532
prayagupa Avatar asked Feb 06 '13 13:02

prayagupa


People also ask

What is @ElementCollection in JPA?

JPA 2.0 defines an ElementCollection mapping. It is meant to handle several non-standard relationship mappings. An ElementCollection can be used to define a one-to-many relationship to an Embeddable object, or a Basic value (such as a collection of String s).

What is @ElementCollection in hibernate?

Annotation Type ElementCollectionDefines a collection of instances of a basic type or embeddable class. Must be specified if the collection is to be mapped by means of a collection table. Example: @Entity public class Person { @Id protected String ssn; protected String name; ...

What does@ ElementCollection do?

You can use the @ElementCollection annotation to store a list of values as an entity attribute without needing to model an additional entity.


1 Answers

The solution is .join("properties") than .get("properties").

Path<Map<String, String>> propertiesPath = root.join("properties");
predicate = (predicate != null) ? criteriaBuilder.and(predicate, criteriaBuilder.and(propertiesPath.in(searchTrxnKey), propertiesPath.in(searchTrxnValue)))
                                : criteriaBuilder.and(propertiesPath.in(searchTrxnKey), propertiesPath.in(searchTrxnValue));

More at JPA Criteria API - How to add JOIN clause (as general sentence as possible)

like image 150
prayagupa Avatar answered Sep 27 '22 21:09

prayagupa