有没有Eclipselink替代Hibernate @where注解?我的Entity类在Hibernate中
@Table(name = "entity_definition")
public class EntityDefinition implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column(name = "\"entity_id\"", nullable = false)
private Integer entityId;
@OneToMany(mappedBy = "entityId", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@Where(clause = "ep_exp_dt >= now()")
private Set<EntityProvider> entityProvider;
或者如何使用CriteriaBuilder来执行此操作
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<EntityDefinition> cq = cb.createQuery(EntityDefinition.class);
Root<EntityDefinition> ed = cq.from(EntityDefinition.class);
ParameterExpression<Date> paramCurrentDate = cb.parameter(Date.class);
final Predicate predicateEDCurrDate = cb.greaterThanOrEqualTo(ed.get("edExpDt"), paramCurrentDate);
ParameterExpression<String> paramEntityName = cb.parameter(String.class);
final Predicate predicateEntityName = cb.equal(ed.get("entityNm"), paramEntityName);
cq.select(ed).where(cb.and(predicateEntityName, predicateEDCurrDate));
TypedQuery<EntityDefinition> tQuery = entityManager.createQuery( cq );
tQuery.setParameter( paramEntityName, entityNm.toUpperCase() );
tQuery.setParameter( paramCurrentDate, Date.valueOf(LocalDate.now()));
return tQuery.getSingleResult();
感谢你的帮助。
拉杰
1条答案
按热度按时间s3fp2yjn1#
你可以使用@org.eclipse.persistence.annotations.
@Entity @AdditionalCriteria(“isActive ='false'”)公共类产品{ //...@列私有布尔值isActive;}
https://www.eclipse.org/eclipselink/documentation/2.7/jpa/extensions/annotations_ref.htm#additionalcriteria
更多详情:https://itecnote.com/tecnote/hibernate-how-to-filter-queries-where-clause-on-an-entity-by-default/