Hibernate: select user0_.id as id1_1_, user0_.address_id as address_5_1_, user0_.add_id as add_id2_1_,
user0_.email as email3_1_, user0_.last_name as last_nam4_1_ from tb_user user0_
where user0_.id>5 order by user0_.id desc limit ?, ?
Hibernate: select count(user0_.id) as col_0_0_ from tb_user user0_ where user0_.id>5
/**
* Create a conjunction of the given boolean expressions.
* @param x boolean expression
* @param y boolean expression
* @return and predicate
*/
Predicate and(Expression x, Expression y);
/**
* Create a conjunction of the given restriction predicates.
* A conjunction of zero predicates is true.
* @param restrictions zero or more restriction predicates
* @return and predicate
*/
Predicate and(Predicate... restrictions);
/**
* Create a disjunction of the given boolean expressions.
* @param x boolean expression
* @param y boolean expression
* @return or predicate
*/
Predicate or(Expression x, Expression y);
/**
* Create a disjunction of the given restriction predicates.
* A disjunction of zero predicates is false.
* @param restrictions zero or more restriction predicates
* @return or predicate
*/
Predicate or(Predicate... restrictions);
Hibernate: select user0_.id as id1_1_, user0_.address_id as address_5_1_, user0_.add_id as add_id2_1_,
user0_.email as email3_1_, user0_.last_name as last_nam4_1_ from tb_user user0_
where user0_.id>5 and user0_.email=? order by user0_.id desc limit ?, ?
Hibernate: select count(user0_.id) as col_0_0_ from tb_user user0_ where user0_.id>5 and user0_.email=?
/**
* Create a conjunction (with zero conjuncts).
* A conjunction with zero conjuncts is true.
* @return and predicate
*/
Predicate conjunction();
/**
* Create a disjunction (with zero disjuncts).
* A disjunction with zero disjuncts is false.
* @return or predicate
*/
Predicate disjunction();
① CriteriaBuilder.conjunction()
修改方法如下所示:
public Page test(){
Sort sort = new Sort(Sort.Direction.DESC,"id");
int page = 1;
int pageSize = 5;
Pageable pageable = PageRequest.of(page,pageSize,sort);
Specification specification = new Specification() {
@Override
public Predicate toPredicate(Root root,
CriteriaQuery> query, CriteriaBuilder cb) {
return cb.conjunction();
}
};
Page userPage = userRepository.findAll(specification, pageable);
return userPage;
}
后台SQL打印如下:
Hibernate: select user0_.id as id1_1_, user0_.address_id as address_5_1_, user0_.add_id as add_id2_1_,
user0_.email as email3_1_, user0_.last_name as last_nam4_1_ from tb_user user0_
where 1=1 order by user0_.id desc limit ?, ?
//这里where子句为 where 1=1
Hibernate: select count(user0_.id) as col_0_0_ from tb_user user0_ where 1=1
② CriteriaBuilder.disjunction()
修改方法如下所示:
public Page test18(){
Sort sort = new Sort(Sort.Direction.DESC,"id");
int page = 1;
int pageSize = 5;
Pageable pageable = PageRequest.of(page,pageSize,sort);
Specification specification = new Specification() {
@Override
public Predicate toPredicate(Root root,
CriteriaQuery> query, CriteriaBuilder cb) {
return cb.disjunction();
// return cb.conjunction();
}
};
Page userPage = userRepository.findAll(specification, pageable);
return userPage;
}
此时SQL打印如下:
Hibernate: select user0_.id as id1_1_, user0_.address_id as address_5_1_, user0_.add_id as add_id2_1_,
user0_.email as email3_1_, user0_.last_name as last_nam4_1_ from tb_user user0_
where 0=1 order by user0_.id desc limit ?, ?
//这里where子句为 where 0=1,肯定不会返回值了
Hibernate: select count(user0_.id) as col_0_0_ from tb_user user0_ where 0=1
【4】Path应用之对象属性为对象
Path是个什么?有三个关键词:对象引用;属性;路径。
源码如下所示:
/**
* Represents a simple or compound attribute path from a
* bound type or collection, and is a "primitive" expression.
*表示来自绑定类型或集合的简单或复合属性路径,并且是“原语”表达式。
* @param the type referenced by the path
*
* @since Java Persistence 2.0
*/
public interface Path extends Expression {
/**
* Return the bindable object that corresponds to the path expression.
*/
Bindable getModel();
/**
* Return the parent "node" in the path or null if no parent.
*/
Path> getParentPath();
/**
* Create a path corresponding to the referenced single-valued attribute.
*/
Path get(SingularAttribute super X, Y> attribute);
/**
* Create a path corresponding to the referenced collection-valued attribute.
*/
> Expression get(PluralAttribute collection);
/**
* Create a path corresponding to the referenced map-valued attribute.
*/
> Expression get(MapAttribute map);
/**
* Create an expression corresponding to the type of the path.
* @return expression corresponding to the type of the path
*/
Expression> type();
//String-based:
/**
* Create a path corresponding to the referenced attribute.
*/
Path get(String attributeName);
}
public Page test(){
Sort sort = new Sort(Sort.Direction.DESC,"id");
int page = 1;
int pageSize = 5;
Pageable pageable = PageRequest.of(page,pageSize,sort);
Specification specification = new Specification() {
@Override
public Predicate toPredicate(Root root,
CriteriaQuery> query, CriteriaBuilder cb) {
Path
此时后台SQL打印如下:
Hibernate: select user0_.id as id1_1_, user0_.address_id as address_5_1_, user0_.add_id as add_id2_1_,
user0_.email as email3_1_, user0_.last_name as last_nam4_1_
from tb_user user0_
where user0_.address_id=1 order by user0_.id desc limit ?, ?
//where 子句 条件为user-address的外键列 address_id
Hibernate: select count(user0_.id) as col_0_0_
from tb_user user0_
where user0_.address_id=1
上面是我们手动处理–先获取addressPath,再获取其idPath,实际应用中通常处理如下:
// nested path translate, 如Task的名为"user.name"的filedName, 转换为Task.user.name属性
String[] names = StringUtils.split("address.id", ".");
Path expression = root.get(names[0]);
for (int i = 1; i < names.length; i++) {
expression = expression.get(names[i]);
}
Hibernate: select user0_.id as id1_1_, user0_.address_id as address_5_1_, user0_.add_id as add_id2_1_,
user0_.email as email3_1_, user0_.last_name as last_nam4_1_
from tb_user user0_
cross join tb_address address1_
where user0_.address_id=address1_.id and address1_.city=? order by user0_.id desc limit ?, ?
Hibernate: select count(user0_.id) as col_0_0_
from tb_user user0_
cross join tb_address address1_
where user0_.address_id=address1_.id and address1_.city=?
Hibernate: select user0_.id as id1_1_, user0_.address_id as address_5_1_, user0_.add_id as add_id2_1_,
user0_.email as email3_1_, user0_.last_name as last_nam4_1_ from tb_user user0_
where user0_.id>5 and user0_.email=?
group by user0_.id
having user0_.id>0
order by user0_.id desc limit ?, ?
// 这里需要注意,排序根据pageable
Hibernate: select count(user0_.id) as col_0_0_ from tb_user user0_
where user0_.id>5 and user0_.email=? group by user0_.id having user0_.id>0
@PersistenceContext
private EntityManager entityManager;
public List test22(){
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
//User指定了查询结果返回至自定义对象
CriteriaQuery query = cb.createQuery(User.class);
Root root = query.from(User.class);
Path id = root.get("id");
List predicates=new ArrayList();
Predicate predicateId = cb.equal(id,1);
predicates.add(predicateId);
Path email = root.get("email");
Predicate predicateEmail = cb.equal(email, "[email protected]");
predicates.add(predicateEmail);
Predicate endPredicate = cb.and(predicates.toArray(new Predicate[predicates.size()]));
//添加where条件
query.where(endPredicate);
//指定查询项,select后面的东西
// query.multiselect(id,email);
//分组
query.groupBy(id);
//排序
query.orderBy(cb.asc(id));
//筛选
query.having(cb.greaterThan(id,0));
TypedQuery q = entityManager.createQuery(query);
List result = q.getResultList();
for (User user : result) {
//打印查询结果
System.out.println(user.toString());
}
return result;
}
后台SQL打印如下:
Hibernate: select user0_.id as id1_1_, user0_.address_id as address_5_1_, user0_.add_id as add_id2_1_,
user0_.email as email3_1_, user0_.last_name as last_nam4_1_ from tb_user user0_
where user0_.id=1 and user0_.email=?
group by user0_.id
having user0_.id>0
order by user0_.id asc
//关联查询Address
Hibernate: select address0_.id as id1_0_0_, address0_.city as city2_0_0_, address0_.province as province3_0_0_
from tb_address address0_ where address0_.id=?
public class OcuppyMoreThanHalf {
/**
* Q74 数组中有一个数字出现的次数超过了数组长度的一半,找出这个数字
* two solutions:
* 1.O(n)
* see <beauty of coding>--每次删除两个不同的数字,不改变数组的特性
* 2.O(nlogn)
* 排序。中间
cygwin很多命令显示command not found的解决办法
修改cygwin.BAT文件如下
@echo off
D:
set CYGWIN=tty notitle glob
set PATH=%PATH%;d:\cygwin\bin;d:\cygwin\sbin;d:\cygwin\usr\bin;d:\cygwin\usr\sbin;d:\cygwin\us