我刚刚发现 Hibernate 中的 @Formula
注解,但无法让它工作。
我尝试使用 hibernate Session 以及带有 persistence.xml
的 JPA Entitymanager
来访问实体,在这两种情况下都是我的 @Formula
带注释的字段保持“null”。
我的实体(从示例复制):
@Entity(name = "Account")
public static class Account {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
private Double credit;
private Double rate;
@Formula(value = "credit * rate")
private Double interest;
//Getters and setters omitted for brevity
}
我的测试设置(初始化测试用例的 session 和实体管理器):
private SessionFactory sessionFactory;
private Session session = null;
EntityManager em;
@Before
public void before() {
EntityManagerFactory emf=Persistence.createEntityManagerFactory("test-pu");
em=emf.createEntityManager();
Configuration configuration = new Configuration();
configuration.addAnnotatedClass(TestEntity.class);
configuration.addAnnotatedClass(Account.class);
configuration.setProperty("hibernate.dialect",
"org.hibernate.dialect.PostgreSQL94Dialect");
configuration.setProperty("hibernate.connection.driver_class",
"org.postgresql.Driver");
configuration.setProperty("hibernate.connection.url",
"jdbc:postgresql://localhost:5432/Archiv");
configuration.setProperty("hibernate.hbm2ddl.auto",
"create");
configuration.setProperty("hibernate.connection.username",
"postgres");
configuration.setProperty("hibernate.connection.password",
"postgres");
sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();
}
实体管理器的 persistence.xml:
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="test-pu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.test.ORMTest$Account</class>
<class>com.test.ORMTest$TestEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL94Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/Archiv"/>
<property name="hibernate.connection.username" value="postgres"/>
<property name="hibernate.connection.password" value="postgres"/>
<property name="hibernate.format_sql" value="true"/>
<property name="javax.persistence.validation.mode" value="NONE"/>
<property name="hibernate.service.allow_crawling" value="false"/>
<property name="hibernate.session.events.log" value="true"/>
</properties>
</persistence-unit>
</persistence>
最后但并非最不重要的一点是测试代码:
@Test public void emTest(){
em.getTransaction().begin();
Account account = new Account();
account.credit=100.0;
account.rate=0.1;
em.persist(account);
em.getTransaction().commit();
em.getTransaction().begin();
Account account1 = em.find(Account.class, 1l);
Double d = account1.getInterest();
System.out.println(d);
em.getTransaction().commit();
}
@Test
public void sessionTest() {
Transaction transaction = session.beginTransaction();
Account account = new Account();
account.credit=100.0;
account.rate=0.1;
session.save(account);
transaction.commit();
transaction = session.beginTransaction();
Account account1= session.get(Account.class, 1);
Double d = account1.getInterest();
System.out.println(d);
transaction.commit();
}
两种情况都为 d
打印“null”。
根据示例,两种情况都应该有效。任何坚定使用 Hibernate 的人都可以告诉我哪里出了问题吗?:/
最佳答案
确保您希望使用公式设置的对象没有从缓存中加载(其中未设置相同的对象)
关于java - @Formula Hibernate 注解未评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40418496/