English 中文(简体)
如何利用《人民军法》标准重写这一询问?
原标题:How to rewrite this query using JPA Criteria Query?
public class Entity {
private boolean suspended;

private String code;

@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@DateTimeFormat(style = "S-")
private DateTime validFrom;

@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@DateTimeFormat(style = "S-")
private DateTime validTill;
}

Meaning of query: "Is the code unique in given time validity?"

• 如何重写:

select count(o) from Entity o 
where o <> :this and o.code = :code and (
  (o.validFrom between :validFrom and :validTill and not o.suspended) or
  (o.validTill between :validFrom and :validTill and not o.suspended) or 
  (:validFrom between o.validFrom and o.validTill and not o.suspended)
)
最佳回答
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Entity> cQuery = builder.createQuery(Entity.class);
Root<Entity> root = cQuery.from(Entity.class);

Predicate pValid1 = builder.between(root.get("ValidFrom"), validFrom, validTill);
Predicate pValid2 = builder.between(root.get("ValidTill"), validFrom, validTill);
Predicate pValid3 = builder.and(builder.lessThan(root.get("validTill"), balidFrom),builder.greaterThan(root.get("ValidFrom",validfrom)));

Predicate pSuspend = builder.not(root.get("Suspended"),true);

ExecutableQuery qFinal = em.createQuery(cQuery.select(root).where(builder.and(pSuspend,builder.or(pValid1,pvalid2,pvalid3))))

它应当这样做。 应当能够调整元数据目标,尽管我还没有这样做。

问题回答

暂无回答




相关问题
query must begin with SELECT or FROM: delete [delete

I am using JPA in addition to spring(3.0.0.M4). While deleting multiple records using query.executeUpdate() i am getting the following exception. org.springframework.web.util.NestedServletException: ...

Last update timestamp with JPA

I m playing around a bit with JPA(Eclipselink to be specific). The below entity have a timestamp that s supposed to reflect whenever that entity was last updated. What are the strategies for making ...

@IdClass with non primative @Id

I m trying to add a composite primary key to a class and having a bit of trouble. Here are the classes. class User { private long id; ... } class Token { private User user; private ...

Database not dropped in between unit test

Hello good people i came accross a weird behaviour in my test.I m using JPA hibernate annotation with spring. let say i have an Class MyObject and it s property email is marqued @Column(name="EMAIL", ...

Toplink trying to persist null object

I have an object "Instance" with another object "Course" inside. When trying to persist a new Instance object, I get the following error if Course is null: java.lang.IllegalStateException: During ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签