2016-03-22 12 views
3

で@Formulaを使用するためにはどうすれば@Formula注釈は以下のコードの簡単な片を用いて動作を確認しようとしています。にHibernate/JPA

私が説明とbidAmount列の値が、@FormulaすなわちshortDescriptionとaverageBidAmountリターンヌルで注釈を付けフィールドをプリントアウトすることができています。

誰もがここでコードが間違っているものを指摘して助けてくださいことはできますか?

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クラスは次のとおりです。

私は本以下の午前

@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; 
    } 
} 

EDIT Java Persistence with Hibernate 2nd Ed.

enter image description here

おかげ

答えて

1

あなたDerivedPropertyインスタンスは、永続コンテキスト(だけ自分のIDがクエリから返された結果セットから使用されている)から返されました。そのため、数式は評価されていません。あなたはエンティティマネージャを閉じていない場合

永続コンテキストはクリアされません。永続コンテキストを強制的にクリアするために最初のトランザクションをコミットした後にem.clear()を追加してください。

+0

ハック!私はあなたたちがどこにいるのか分からない。私はこれを何時間も探してきました。 :( – EMM

関連する問題