billdoor
billdoor

Reputation: 2043

@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 ?:/

Upvotes: 0

Views: 1805

Answers (1)

Sreekumar Ani
Sreekumar Ani

Reputation: 11

Make sure that the object you expect to be set with Formula, is not getting loaded from the cache(where it having the same not set)

Upvotes: 1

Related Questions