查询@ElementCollection JPA [英] Query @ElementCollection JPA
问题描述
我有一个实体
交易
如下:
@Entity
类Transaction延伸AbstractEntity< Long> {
private static final long serialVersionUID = 7222139865127600245L;
//其他属性
$ b $ @ElementCollection(fetch = FetchType.EAGER,targetClass = java.lang.String.class)
@CollectionTable(name =transaction_properties,joinColumns = @JoinColumn(name =p_id))
@MapKeyColumn(name =propertyKey)
@Column(name =propertyValue)
私人地图< String,String>性能;
// getters and setters
}
所以,我的数据库表对于
transaction_properties
是
的MySQL> desc transaction_properties;
+ --------------- + -------------- + ------ + ----- + - -------- + ------- +
|字段|类型|空| Key |默认|额外|
+ --------------- + -------------- + ------ + ----- + - -------- + ------- +
| p_id | bigint(20)| NO | PRI | | |
| propertyValue | varchar(255)|是| | NULL | |
| propertyKey | varchar(255)| NO | PRI | | |
+ --------------- + -------------- + ------ + ----- + - -------- + ------- +
3行(0.00秒)
现在,我想用键和值搜索实体 Transaction
。
路径< Map< String,String>> propertiesPath = root.get(properties);
路径< String> propertyKeyPath = propertiesPath。< String>获得( PROPERTYKEY); //其中m出现错误
路径< String> propertyValuePath = propertyKeyPath。< String>获得( 的PropertyValue);
p = cb.and(p,cb.and(cb.like(propertyKeyPath,%+ searchTrxnKey +%),cb.like(propertyValuePath,%+ searchTrxnValue +%))) ;
但是出现 Path< String>错误propertyKeyPath = propertiesPath。< String> get(propertyKey);
如下:
[...]抛出一个意外的异常:org.springframework.dao.InvalidDataAccessApiUsageException:非法尝试取消引用路径源[null];嵌套异常是java.lang.IllegalArgumentException:非法尝试解引用路径源[null]
其中之一我经历的参考是: Spring JPA教程第四部分:JPA标准查询但对我来说没有好运。 解决方案是 .join(properties)
than .get(properties)
。
路径< Map< String,String>> propertiesPath = root.join(properties);
predicate =(predicate!= null)?标准构建器.and(predicate,criteriaBuilder.and(propertiesPath.in(searchTrxnKey),propertiesPath.in(searchTrxnValue)))
:criteriaBuilder.and(propertiesPath.in(searchTrxnKey),propertiesPath.in(searchTrxnValue));
更多 JPA Criteria API - 如何添加JOIN子句(尽可能使用普通语句)
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.
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)
这篇关于查询@ElementCollection JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!