简体   繁体   中英

@Formula Hibernate Annotation not evaluated

i just found out about the @Formula Annotation in Hibernate, but cannot get it to work.

I Tried using a hibernate Session to access the entites as well as a JPA Entitymanager with persistence.xml , in both cases my @Formula annotated field remains "null".

my Entity (copied from an Example):

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

My Test-Setup (initializing a Session an an entitymanager for Test cases):

    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();

    }

The persistence.xml for the entitymanager:

    <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>

And last but not least, the Test Code:

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

Both Cases print "null" for d .

According to the examples both cases should work. Anyone firm with Hibernate to tell me where i went wrong ?:/

确保您希望用公式设置的对象没有从缓存中加载(未设置相同对象的地方)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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