java 查询@ElementCollection JPA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14730482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:11:04  来源:igfitidea点击:

Query @ElementCollection JPA

javaspringhibernatejpahibernate-criteria

提问by prayagupd

I have an EntityTransactionas following :

我有EntityTransaction以下内容:

@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 Tablefor transaction_propertiesis

所以,我的数据库Tabletransaction_properties

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 Transactionwith key and value.

现在,我想Transaction用键和值搜索实体。

    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 :

但我收到Path<String> propertyKeyPath = propertiesPath.<String> get("propertyKey");如下错误:

[...] 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 Queriesbut no luck for me.

我阅读的参考资料之一是:Spring Data JPA 教程第四部分:JPA Criteria Queries但对我来说没有运气。

回答by prayagupd

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

解决方案是.join("properties").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)

更多在JPA Criteria API - 如何添加 JOIN 子句(尽可能通用的句子)