有没有人实现过这个,或者知道是否难以实现这个/有任何指针?
public static SpatialRelationCriterion IsWithinDistance(string propertyName, object anotherGeometry, double distance)
{
// TODO: Implement
throw new NotImplementedException();
}
Run Code Online (Sandbox Code Playgroud)
来自NHibernate.Spatial.Criterion.SpatialRestrictions
我可以在hql中使用"NHSP.Distance(PROPERTY,:point)".但是想要将此查询与我现有的Criteria查询相结合.
目前我正在创建一个粗糙的多边形,并使用
criteria.Add(SpatialRestrictions.Intersects("PROPERTY", myPolygon));
Run Code Online (Sandbox Code Playgroud)
编辑 通过在SpatialRelationCriterion上重载构造函数来获得原型,添加新的SpatialRelation.Distance
public static SpatialRelationCriterion IsWithinDistance(string propertyName, object anotherGeometry, double distance)
{
return new SpatialRelationCriterion(propertyName, SpatialRelation.Distance, anotherGeometry, distance);
}
Run Code Online (Sandbox Code Playgroud)
为SpatialRelationCriterion添加了一个新字段
private readonly double? distance;
public SpatialRelationCriterion(string propertyName, SpatialRelation relation, object anotherGeometry, double distance)
: this(propertyName, relation, anotherGeometry)
{
this.distance = distance;
}
Run Code Online (Sandbox Code Playgroud)
编辑ToSqlString
object secondGeometry = Parameter.Placeholder;
if (!(this.anotherGeometry is IGeometry))
{
secondGeometry = columns2[i];
}
if …Run Code Online (Sandbox Code Playgroud) 我的Java应用程序使用JPA进行对象持久化.业务域非常简单(只有三个类是持久的,每个类有3-5个属性).查询也很简单.问题是我应该使用哪种方法:JPQL或Criteria API?
我想只选择特定的列(例如SELECT a FROM b).我有一个通用的DAO,我想出的是:
public List<T> getAll(boolean idAndVersionOnly) {
CriteriaBuilder builder = manager.getCriteriaBuilder();
CriteriaQuery<T> criteria = builder.createQuery(entityClazz);
Root<T> root = criteria.from(entityClazz);
if (idAndVersionOnly) {
criteria.select(root.get("ID").get("VERSION")); // HERE IS ERROR
} else {
criteria.select(root);
}
return manager.createQuery(criteria).getResultList();
}
Run Code Online (Sandbox Code Playgroud)
错误是:
The method select(Selection<? extends T>) in the type CriteriaQuery<T> is not applicable for the arguments (Path<Object>).我该怎么改变?我想得到一个T只有ID和VERSION字段的类型对象,而其他所有的都是null.
类型Textends AbstractEntity包含这两个字段.
entityClazz是T.class.
您能帮我解决一下如何将以下代码转换为使用条件构建器的"in"运算符吗?我需要使用"in"使用列表/数组的用户名进行过滤.
我也尝试使用JPA CriteriaBuilder进行搜索 - "in"方法但是找不到好的结果.如果你能给我这个主题的参考网址,我也非常感谢.谢谢.
这是我的代码:
//usersList is a list of User that I need to put inside IN operator
CriteriaBuilder builder = getJpaTemplate().getEntityManagerFactory().getCriteriaBuilder();
CriteriaQuery<ScheduleRequest> criteria = builder.createQuery(ScheduleRequest.class);
Root<ScheduleRequest> scheduleRequest = criteria.from(ScheduleRequest.class);
criteria = criteria.select(scheduleRequest);
List<Predicate> params = new ArrayList<Predicate>();
List<ParameterExpression<String>> usersIdsParamList = new ArrayList<ParameterExpression<String>>();
for (int i = 0; i < usersList.size(); i++) {
ParameterExpression<String> usersIdsParam = builder.parameter(String.class);
params.add(builder.equal(scheduleRequest.get("createdBy"), usersIdsParam) );
usersIdsParamList.add(usersIdsParam);
}
criteria = criteria.where(params.toArray(new Predicate[0]));
TypedQuery<ScheduleRequest> query = getJpaTemplate().getEntityManagerFactory().createEntityManager().createQuery(criteria);
for (int i = 0; i < usersList.size(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的新项目中使用Criteria API:
public List<Employee> findEmps(String name) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
Root<Employee> emp = c.from(Employee.class);
c.select(emp);
c.distinct(emp);
List<Predicate> criteria = new ArrayList<Predicate>();
if (name != null) {
ParameterExpression<String> p = cb.parameter(String.class, "name");
criteria.add(cb.equal(emp.get("name"), p));
}
/* ... */
if (criteria.size() == 0) {
throw new RuntimeException("no criteria");
} else if (criteria.size() == 1) {
c.where(criteria.get(0));
} else {
c.where(cb.and(criteria.toArray(new Predicate[0])));
}
TypedQuery<Employee> q = em.createQuery(c);
if (name != null) {
q.setParameter("name", name);
}
/* ... */ …Run Code Online (Sandbox Code Playgroud) 我需要创建一个"真正的"动态JPA CriteriaBuilder.我得到Map<String, String>了陈述.看起来像:
name : John
surname : Smith
email : email@email.de
...more pairs possible
Run Code Online (Sandbox Code Playgroud)
这是我实现的:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> query = cb.createQuery(User.class);
Root<User> userRoot = query.from(User.class);
query.select(userRoot);
List<Predicate> predicates = new ArrayList<Predicate>();
Iterator<String> column = statements.keySet().iterator();
while (column.hasNext()) {
// get the pairs
String colIndex = column.next();
String colValue = statements.get(colIndex);
// create the statement
Predicate pAnd = cb.conjunction();
pAnd = cb.and(pAnd, cb.equal(userRoot.get(colIndex), colValue));
predicates.add(pAnd);
}
// doesn't work, i don't know how many predicates …Run Code Online (Sandbox Code Playgroud) 将JPA 2与EclipseLink实现一起使用.
我正在尝试构建一个动态查询,它应该会在给定日期之后保留一些记录.
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Event> criteria = builder.createQuery(Event.class);
Root<Event> root = criteria.from(Event.class);
criteria.select(root);
criteria.distinct(true);
List<Predicate> predicates = new ArrayList<Predicate>();
//...
if (dateLimit != null){
ParameterExpression<Date> param = builder.parameter(Date.class, "dateLimit");
predicates.add(builder.lessThanOrEqualTo(root.get("dateCreated"), param));
}
Run Code Online (Sandbox Code Playgroud)
lessThanOrEqualTo()并且le()是仅有的两个方法,在API中看起来像可以帮助我在这种情况下.这个警告是由日食引发的:
Bound mismatch: The generic method lessThanOrEqualTo(Expression<? extends Y>, Expression<? extends Y>)
of type CriteriaBuilder is not applicable for the arguments (Path<Object>, ParameterExpression<Date>).
The inferred type Object is not a valid substitute for the bounded parameter
<Y extends Comparable<? super Y>>
Run Code Online (Sandbox Code Playgroud)
我可以想象我没有采取正确的方法解决这个问题,但我无法找到任何可能解决方案的提示或指示.
我曾尝试多次使用子查询和IN表达式编写查询语句.但我从来没有成功过.
我总是得到异常,"关键字'IN'附近的语法错误",查询语句是这样构建的,
SELECT t0.ID, t0.NAME
FROM EMPLOYEE t0
WHERE IN (SELECT ?
FROM PROJECT t2, EMPLOYEE t1
WHERE ((t2.NAME = ?) AND (t1.ID = t2.project)))
Run Code Online (Sandbox Code Playgroud)
我知道'IN'失败前的这个词.
你有没有写过这样的问题?有什么建议吗?
我遇到了一个简单的问题; 奋力如何调用order by上join编辑实体.基本上我正在努力实现以下目标JPA Criteria:
select distinct d from Department d
left join fetch d.children c
left join fetch c.appointments a
where d.parent is null
order by d.name, c.name
Run Code Online (Sandbox Code Playgroud)
我有以下内容:
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Department> c = cb.createQuery(Department.class);
Root<Department> root = c.from(Department.class);
Fetch<Department, Department> childrenFetch = root.fetch(
Department_.children, JoinType.LEFT);
childrenFetch.fetch(Department_.appointments, JoinType.LEFT);
c.orderBy(cb.asc(root.get(Department_.name)));
c.distinct(true);
c.select(root);
c.where(cb.isNull(root.get(Department_.parent)));
Run Code Online (Sandbox Code Playgroud)
如何实现order by d.name, c.name用Criteria API?我尝试使用Expression,Path但是没有用.任何指针将不胜感激.
我正在尝试使用编写NOT IN约束JPA Criteria.我尝试过这样的事情:
builder.not(builder.in(root.get(property1)));
Run Code Online (Sandbox Code Playgroud)
虽然我知道它不会起作用.在上面的语法中,如何添加property1要检查的集合/列表?
criteria-api ×10
jpa ×8
java ×7
jpa-2.0 ×7
hibernate ×2
.net ×1
date ×1
geospatial ×1
in-subquery ×1
jpql ×1
nhibernate ×1
orm ×1
spatial ×1
sql-order-by ×1
subquery ×1