4 Replies Latest reply on Mar 2, 2011 4:53 AM by antibrumm.mfrey0.bluewin.ch

    Formula annotation is being ignored.

    makoto.makoto.hanafusa.soliton.co.jp

      Hi.


      I'd like to use @Formula. For instance, in this way.


      @Entity
      @Table(name="employee")
      public class Employee implements Serializable {
              private static final long serialVersionUID = 1L;
      
              @EmbeddedId
              private EmployeePK id;
      
              @Column(length=50)
              private String name;
      
              @Transient
              @Formula("lower(name)")
              private String lowerName;
      
      





      But @Formula is being ignored. Cannot @Formula be used with seam?


      Could someone show me how to use @Formula.


      Thanks!

        • 1. Re: Formula annotation is being ignored.
          matteg.gerry.matte.shaw.ca

          Thanks for posting this question.  I did not know this useful annotation existed.  When you do resolve the problem, please post the solution for the rest of us to learn.


          My suggestions (guesses really) are:
          Either you are using a hibernate version prior to 3.1.4 or possibly you need to annotate the property rather than the getter method or possibly use the full classname in the formula - @org.hibernate.annotations.Formula(.....


          see http://openxava.wikispaces.com/model_en#toc9 (below)


          Formula (new in v3.1.4)


          Using @Formula from Hibernate Annotations you can define a calculation for your property. This calculation is expressed using SQL, and it is executed by the database, not by Java. You only need to write a valid SQL fragment:


          @org.hibernate.annotations.Formula("UNITPRICE 1.16")
          private BigDecimal unitPriceWithTax;
          public BigDecimal getUnitPriceWithTax() {
              return unitPriceWithTax;
          }


          The use is simple. Put your calculation exactly in the same way that you would put it in a SQL statement.
          Usually the properties with @Formula are read only properties, that is, they have only getter, not setter. When the object is read from database the calculation is done by the database and the property is populate with it.
          This is an alternative to calculated properties. It has the advantage that the user can filter by this property in list mode, and the disadvantage that you have to use SQL instead of Java, and you cannot use @Depends for live recalculation of the value.

          • 2. Re: Formula annotation is being ignored.
            matteg.gerry.matte.shaw.ca

            Oops - I see that you are annotating property definition.  Possibly you should try it without the @Transient annotation ...... (and no setter method of course).

            • 3. Re: Formula annotation is being ignored.
              makoto.makoto.hanafusa.soliton.co.jp

              Hi, Gerry! Thank you for your reply.




              Using @Formula from Hibernate Annotations you can define a calculation for your property.


              My environments are the following.



              • JBoss 5.1.0.GA / Seam 2.2.1.Final / Hibernate Annotations 3.4.0.GA





              Possibly you should try it without the @Transient annotation ...... (and no setter method of course).


              Yes. I have tried that. But it didn't work.


              Someone is using formula annotation?

              • 4. Re: Formula annotation is being ignored.
                antibrumm.mfrey0.bluewin.ch

                Hi


                I've used the @Formula annotation already but i did not use @Transient with it. @Formula is evaluated within the SQL statement and because of that its not 'transient' i would say. (Hibernate needs to fetch it from the DB)


                Try it without and you should see the generated sql containing this field as your given formula.


                (This question would be better in the hibernate forums)