简体   繁体   English

JPA Transient属性和Hibernate Criteria API一起使用

[英]JPA Transient property together Hibernate Criteria API

Is there possible to use @PostLoad with Hibernate Criteria API? 是否可以将@PostLoad与Hibernate Criteria API一起使用? For instance: 例如:

@Entity
public class EntityExample {

  private Integer startYear;

  private Integer startPeriod;

  @Transient
  private String start;

  @PostLoad
  private void init() {
    this.start = this.startYear.toString() + this.startPeriod.toString();
  }

  public String getStart() {
    return this.start;
  }
} 

public class DAOExample {

  public Collection<EntityExample> get(String start) {
    Session session = getSession();
    Criteria criteria = session.createCriteria(EntityExample.class, "entity");
    criteria.add(Restrictions.ne("entity.start", start));
    return criteria.list();
  }
}

When I use @Formula with Hibernate Criteria (for example: @Formula(value = "start_year::text || start_period::text") ), it works fine. 当我将@Formula与休眠条件一起使用时(例如: @Formula(value = "start_year::text || start_period::text") ),它可以正常工作。 But I'm trying to use @PostLoad 'cause if it's needed refactor any envolved property it will not be necessary remember change manually the value of the formula. 但是我尝试使用@PostLoad ',因为如果需要重构任何卷入的属性,则不必记住手动更改公式的value However, using @PostLoad causes: org.hibernate.QueryException: could not resolve property: start of: com.entity.EntityExample 但是,使用@PostLoad会导致: org.hibernate.QueryException: could not resolve property: start of: com.entity.EntityExample

@Transient marks properties as not included in the database table. @Transient属性标记为未包含在数据库表中。 Now you use Criteria API to create a query containing it. 现在,您使用Criteria API创建包含它的查询。 This equals following query: 这等于以下查询:

select * from EntityExample where start != 'start value'

This one will fail as well, because start is no column in that table. 此操作也会失败,因为start在该表中没有列。 You will need to do workaound for this. 您将需要为此进行工作。 Without knowing the use case it is very much impossible to think of a good workaround. 在不知道用例的情况下,很难想到一个好的解决方法。 The easiest one would be to just include start in the table, but I guess you had reasons to not include it right away 最简单的方法是只在表中添加start ,但是我想您有理由不立即添加它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM