问题描述
我有以下休眠代码,
session.createQuery("from Car as car where car.keywordName like :keyword").setString("keyword", keyword ).list();
接受一个带有以下括号%keyword%的关键字,并返回包含该关键字的汽车列表.汽车的关键字是一个长字符串,例如,以逗号分隔.对于汽车,关键字将为豪华,宽敞,敞篷,4人座"和另一条声明
which takes in a keyword with the following parenthesis %keyword% and return the list of cars which has contain the keyword. The keywords of a car is a long string which is comma delimited for e.g. for a car the keywords will be "luxury, spacious, honta, 4seater" and another statement
session.createQuery("from Car as car where car.carId IN (:idList)").setParameterList( "idList", idList ).list();
从ID列表返回汽车列表.我一直试图将像和IN这样的两个子句组合在一起,以从关键字列表中返回一个汽车列表,而不只是一个关键字.
which returns the list of cars from a list of id. I having been trying to combine the two clauses like and IN together to returns a list of cars from a list of keywords instead of just a single keyword.
如果有人能阐明一些想法,将不胜感激.干杯!
Would appreciate if someone can shed some light. Cheers!
推荐答案
您必须动态创建查询才能创建类似
You'll have to dynamically create a query to create something like
select car from Car car
where car.keywordName like :k1
or car.keywordName like :k2
or car.keywordName like :k3
...
做到这一点的最佳工具是标准API (请参阅第三个代码段,使用和解,以或分隔的限制列表的示例).
The best tool to do that is the Criteria API (See the third code snippet, using a disjunction, for an example of a list of restrictions seperated with or
).
这篇关于一起使用IN和LIKE子句进行休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!