title: @Formula pattern in hibernate
body: <p>I was reading some interview questions on Hibernate and came across Hibernate derived properties. I was trying a simple example using @Formula annotations but it is not working. Can anyone please tell me what am i missing. Code snippets below</p>
  
  <p>The output and the SQL queries are display at the end.</p>
  
  <p><strong>Entity (Employee.java)</strong></p>
  
  <pre><code>import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.Table;
  import org.hibernate.annotations.Formula;
  
  @Entity
  @Table(name="EMPLOYEE")
  public class Employee implements java.io.Serializable {
  
  private static final long serialVersionUID = -7311873726885796936L;
  
  @Id
  @Column(name="ID")
  private Integer id;
  
  @Column(name="FIRST_NAME", length=31)
  private String firstName;
  
  @Column(name="LAST_NAME", length=31)
  private String lastName;
  
  @Column(name="MONTHLY_SALARY")
  private float monthlySalary;
  
  @Formula("MONTHLY_SALARY*12")
      private float yearlySalary;
  
  public Employee() {
  }
  
  public Integer getId() {
      return id;
  }
  
  public void setId(Integer id) {
      this.id = id;
  }
  
  public String getFirstName() {
      return firstName;
  }
  
  public void setFirstName(String firstName) {
      this.firstName = firstName;
  }
  
  public String getLastName() {
      return lastName;
  }
  
  public void setLastName(String lastName) {
      this.lastName = lastName;
  }
  
  public float getMonthlySalary() {
      return monthlySalary;
  }
  
  public void setMonthlySalary(float monthlySalary) {
      this.monthlySalary = monthlySalary;
  }
  
  public float getYearlySalary() {
      return yearlySalary;
  }
  }
  </code></pre>
  
  <p><strong>hibernate.cfg.xml</strong>
      </p>
  
  <pre><code>&lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
      &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.Oracle9iDialect&lt;/property&gt;
      &lt;property name="hibernate.connection.driver_class"&gt;oracle.jdbc.driver.OracleDriver&lt;/property&gt;
      &lt;property name="hibernate.connection.username"&gt;system&lt;/property&gt;
      &lt;property name="hibernate.connection.password"&gt;system&lt;/property&gt;
      &lt;property     name="hibernate.connection.url"&gt;jdbc:oracle:thin:@localhost:1521:XE&lt;/property&gt;
      &lt;property name="hibernate.cache.use_query_cache"&gt;false&lt;/property&gt;
  
      &lt;property name="hibernate.jdbc.use_streams_for_binary"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.jdbc.batch_size"&gt;0&lt;/property&gt;
      &lt;property name="hibernate.max_fetch_depth"&gt;3&lt;/property&gt;
      &lt;property name="hibernate.hbm2ddl.auto"&gt;create&lt;/property&gt;
      &lt;property name ="show_sql"&gt;true&lt;/property&gt;
      &lt;mapping class="Employee"/&gt;
     &lt;!--   &lt;mapping class="dto.UserDetailsEmbeddedId"/&gt;--&gt;
  
  &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p><strong>Tester class</strong>
      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.hibernate.cfg.Configuration;
      public class HibernateTest {</p>
  
  <pre><code>/**
   * @param args
   */
  public static void main(String[] args) {
      // TODO Auto-generated method stub
      SessionFactory factory = (new     Configuration()).configure().buildSessionFactory();
      Session session = factory.openSession();
      Employee employee = new Employee();
      employee.setFirstName("Tarun");
      employee.setLastName("bhatt");
      employee.setMonthlySalary(34000);
      employee.setId(12);
  
      session.beginTransaction();
      session.save(employee);
      session.getTransaction().commit();
      System.out.println("salary1 = "+employee.getYearlySalary());
      session.close();
      System.out.println("salary = "+employee.getYearlySalary());
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Output</strong>
      salary1 = 0.0
      salary = 0.0</p>
  
  <p><strong>Queries</strong>
      Hibernate: create table EMPLOYEE (ID number(10,0) not null, FIRST_NAME varchar2(31 char), LAST_NAME varchar2(31 char), MONTHLY_SALARY float, primary key (ID))
      Hibernate: insert into EMPLOYEE (FIRST_NAME, LAST_NAME, MONTHLY_SALARY, ID) values (?, ?, ?, ?)</p>