java - 如何在 Hibernate/JPA 中使用@Formula

标签 java hibernate

我正在尝试使用下面的一段简单代码了解 @Formula 注释是如何工作的。

我能够打印出 description 和 bidAmount 列的值,但是用 @Formula 注释的字段,即 shortDescription 和 averageBidAmount 返回 null。

谁能帮忙指出这里的代码有什么问题吗?

我在 Mac OSX 上使用 Hibernate 5.0、postgresql-9.3-1102-jdbc41 和 TestNG

    import java.math.BigDecimal;
    import java.util.List;
    import javax.persistence.Entity;
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Persistence;
    import javax.transaction.UserTransaction;
    import org.testng.annotations.Test;
    import com.my.hibernate.env.TransactionManagerTest;

    public class DerivedPropertyDemo extends TransactionManagerTest {

      @Test
      public void storeLoadMessage() throws Exception {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("HelloWorldPU");
        try {
          {
            UserTransaction tx = TM.getUserTransaction();
            tx.begin();
            EntityManager em = emf.createEntityManager();

            DerivedProperty derivedProperty1 = new DerivedProperty();
            derivedProperty1.description = "Description is freaking good!!!";
            derivedProperty1.bidAmount = BigDecimal.valueOf(100D);

            DerivedProperty derivedProperty2 = new DerivedProperty();
            derivedProperty2.description = "Description is freaking bad!!!";
            derivedProperty2.bidAmount = BigDecimal.valueOf(200D);

            DerivedProperty derivedProperty3 = new DerivedProperty();
            derivedProperty3.description = "Description is freaking neutral!!!";
            derivedProperty3.bidAmount = BigDecimal.valueOf(300D);

            em.persist(derivedProperty1);
            em.persist(derivedProperty2);
            em.persist(derivedProperty3);

            tx.commit();

            for (DerivedProperty dp : getDerivedProperty(em)) {
              System.out.println("============================");
              System.out.println(dp.description);
              System.out.println(dp.bidAmount);
              System.out.println(dp.getShortDescription());
              System.out.println(dp.getAverageBidAmount());
              System.out.println("#############################");
            }
            em.close();
          }
        } finally {
          TM.rollback();
          emf.close();
        }
      }


     public List<DerivedProperty> getDerivedProperty(EntityManager em) {
      List<DerivedProperty> resultList = em.createQuery("from " + DerivedProperty.class.getSimpleName()).getResultList();
      return resultList;
  }
}

我的实体类是:

@Entity
class DerivedProperty {

  @Id
  @GeneratedValue
  protected Long id;

  protected String description;

  protected BigDecimal bidAmount;

  @org.hibernate.annotations.Formula("substr(description, 1, 12)")
  protected String shortDescription;

  @org.hibernate.annotations.Formula("(select avg(b.bidAmount) from DerivedProperty b where b.bidAmount = 200)")
  protected BigDecimal averageBidAmount;

  public String getShortDescription() {
    return shortDescription;
  }

  public BigDecimal getAverageBidAmount() {
    return averageBidAmount;
  }
}

编辑

我正在关注Java Persistence with Hibernate 2nd Ed这本书。

enter image description here

谢谢

最佳答案

您的 DerivedProperty 实例是从持久性上下文返回的(只有它们的 id 从查询返回的结果集中使用)。这就是公式尚未被评估的原因。

如果您不关闭实体管理器,则不会清除持久性上下文。在提交第一个事务后尝试添加 em.clear() 以强制清除持久性上下文。

关于java - 如何在 Hibernate/JPA 中使用@Formula,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36166212/

相关文章:

java - 打印二叉树的层序遍历,从左到右和从右到左交替

java代码如何读取csv文件中的空值

java - @scope - spring - 使用注释设置范围 "prototype",行为类似于单例。我哪里出错了?

java - 如何在 hibernate 中映射对实体的访问?

java - hibernate注释和连接

java - JLabel setText() 方法不起作用

java - 随机生成特定数字

java - Spring Data Avg NullPointerException

java - 如何在运行时在 Tomcat 中创建一个 hibernate.properties 文件

java - 扩展 hibernate 实体以添加非拥有关系,针对同一个表