jpa 在@Param String 中传递保留字
jpa pass reserved words in @Param String
我的目标是编写如下查询:
select * from Book where author = any(select author from book)
and(genre='comedy') order by ( '' )ASC, ( pages )DESC;
其中 'any(select author from book)' 丢失了单引号,所以我可以这样传递它
@Query("select b from Book b where b.author =:author and b.genre =:genre")
List<Book> findAllBySearch(@Param("author") String author,
@Param("genre") String genre);
这样做的原因是因为我有一个包含 5 个输入条件的表单,这些条件可能存在也可能不存在,我不想为每个排列编写单独的查询。我知道一个查询带有 'stringInput' 或 'any(select criteria from book)' 以防在 运行 查询之前插入空字符串或空字符串。
我想使用条件 API 或类似的东西将允许构建动态查询或插入保留的 sql 字,但我不知道如何轻松实现它,因为我正在扩展 Spring 数据 CrudRepository 没有使用 entitymanager...但是....我想我将不得不使用。
有谁知道如何转义字符串输入 @Param 强加的 '' 或者什么方法可以轻松工作...例如条件 API、存储过程、函数?
请原谅我的经验不足...
谢谢!
看起来使用 5 个可能的输入来执行此操作的唯一方法是让运行时策略根据存在的输入从 14 个不同的查询中进行选择,但我试图避免这种情况!!
所以...这是解决方案!
/**
* A JPA-based implementation of the Book Service. Delegates to a JPA entity
* manager to issue data access calls against the backing repository. The
* EntityManager reference is provided by the managing container (Spring)
* automatically.
*/
@Service("bookService")
@Repository
public class JpaBookService implements BookService {
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
public List<Book> searchBooks(String author, String genre, String pages, String year, String rating){
List<Predicate> predList = new LinkedList<Predicate>();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Book> c =cb.createQuery(Book.class);
Root<Book> b = c.from(Book.class);
if(author!="")
predList.add(cb.equal(b.get("author"), author));
}
if(genre!=""){
predList.add(cb.equal(b.get("genre"), genre));
}
if(pages!=""){
predList.add(cb.equal(b.get("pages"), pages));
}
if(year!=""){
predList.add(cb.equal(b.get("year"), year));
}
if(rating!=""){
predList.add(cb.equal(b.get("rating"), rating));
}
Predicate[] predArray = new Predicate[predList.size()];
predList.toArray(predArray);
c.where(predArray);
TypedQuery<Book> q = em.createQuery(c);
List<Book> result = q.getResultList();
return result;
}
}
效果不错!
Johnny O.
我的目标是编写如下查询:
select * from Book where author = any(select author from book)
and(genre='comedy') order by ( '' )ASC, ( pages )DESC;
其中 'any(select author from book)' 丢失了单引号,所以我可以这样传递它
@Query("select b from Book b where b.author =:author and b.genre =:genre")
List<Book> findAllBySearch(@Param("author") String author,
@Param("genre") String genre);
这样做的原因是因为我有一个包含 5 个输入条件的表单,这些条件可能存在也可能不存在,我不想为每个排列编写单独的查询。我知道一个查询带有 'stringInput' 或 'any(select criteria from book)' 以防在 运行 查询之前插入空字符串或空字符串。
我想使用条件 API 或类似的东西将允许构建动态查询或插入保留的 sql 字,但我不知道如何轻松实现它,因为我正在扩展 Spring 数据 CrudRepository 没有使用 entitymanager...但是....我想我将不得不使用。
有谁知道如何转义字符串输入 @Param 强加的 '' 或者什么方法可以轻松工作...例如条件 API、存储过程、函数?
请原谅我的经验不足...
谢谢!
看起来使用 5 个可能的输入来执行此操作的唯一方法是让运行时策略根据存在的输入从 14 个不同的查询中进行选择,但我试图避免这种情况!!
所以...这是解决方案!
/**
* A JPA-based implementation of the Book Service. Delegates to a JPA entity
* manager to issue data access calls against the backing repository. The
* EntityManager reference is provided by the managing container (Spring)
* automatically.
*/
@Service("bookService")
@Repository
public class JpaBookService implements BookService {
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
public List<Book> searchBooks(String author, String genre, String pages, String year, String rating){
List<Predicate> predList = new LinkedList<Predicate>();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Book> c =cb.createQuery(Book.class);
Root<Book> b = c.from(Book.class);
if(author!="")
predList.add(cb.equal(b.get("author"), author));
}
if(genre!=""){
predList.add(cb.equal(b.get("genre"), genre));
}
if(pages!=""){
predList.add(cb.equal(b.get("pages"), pages));
}
if(year!=""){
predList.add(cb.equal(b.get("year"), year));
}
if(rating!=""){
predList.add(cb.equal(b.get("rating"), rating));
}
Predicate[] predArray = new Predicate[predList.size()];
predList.toArray(predArray);
c.where(predArray);
TypedQuery<Book> q = em.createQuery(c);
List<Book> result = q.getResultList();
return result;
}
}
效果不错!
Johnny O.