java持久化层实现的例子, 运用jpa EntityManager实体管理器 @PersistenceContext private EntityManager em;@Override public DataFile getDataFile(String batchNo, String fileBatchNo) { StringQuery query = StringQuery.newQuery() .q
@PersistenceContext
private EntityManager em;
@Override
public DataFile getDataFile(String batchNo, String fileBatchNo) {
StringQuery query = StringQuery.newQuery()
.query("from DataFile where ")
.predicateHasText(batchNo)
.query(" batchNo=:batchNo")
.param("batchNo", batchNo)
.predicateHasText(fileBatchNo)
.query(" and fileBatchNo=:fileBatchNo")
.param("fileBatchNo", fileBatchNo)
.build();
List
list = this.find(query);
if (!CollectionUtils.isEmpty(list)) {
return list.get(0);
}
return null;
}
public List
find(StringQuery stringQuery) { //断言 Assert.notNull(stringQuery, "StringQuery must not be null!"); String query = stringQuery.getQuery(); NamedParams params = stringQuery.getParams(); return this.find(query, params); } public List
find(String queryString, NamedParams params) { Assert.notNull(queryString, "Query must not be null!"); Assert.notNull(params, "NamedParams must not be null!"); Query query = this.em.createQuery(queryString); this.setQueryParams(query, params); return query.getResultList(); }
