-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Problem with formula
PostPosted: Sat Sep 05, 2009 3:40 pm 
Newbie

Joined: Sat Sep 05, 2009 3:24 pm
Posts: 3
Hi,

I have a problem using a formula on a property field, the property field isn't set to the corresponding value when I query the database (HSQLDB).

this is the class I'm using

Code:
@Entity
public class DemandeConge implements java.io.Serializable {
   @Id
   @GeneratedValue
   private Long id;
      
   @Temporal(TemporalType.DATE)
   @Column(name = "DATE_DEBUT", nullable = false)
   private Date dateDebut;
      
   @Temporal(TemporalType.DATE)
   @Column(name = "DATE_FIN", nullable = false)
   private Date dateFin;   
   
   @org.hibernate.annotations.Formula("DATEDIFF('dd', DATE_DEBUT, DATE_FIN)")
   private int nombreJoursDemandes;
   
   public DemandeConge() {}
   
   public DemandeConge(Date dateDebut,
                  Date dateFin) {
      this.dateDebut = dateDebut;
      this.dateFin = dateFin;
   }
   
   public Long getId() { return id; }
   
   public Date getDateDebut() { return dateDebut; }
   public void setDateDebut(Date dateDebut) { this.dateDebut = dateDebut; }
   
   public Date getDateFin() { return dateFin; }
   public void setDateFin(Date dateFin) { this.dateFin = dateFin; }
   
   public int getNombreJoursDemandes() { return nombreJoursDemandes; }
   
}


and this is my client program
Code:
EntityManagerFactory entityManagerFactory =
                Persistence.createEntityManagerFactory("conge");
               
        EntityManager entityManager = entityManagerFactory.createEntityManager();
       
        EntityTransaction entityTransaction = entityManager.getTransaction();
        entityTransaction.begin();
       
        Date dateDebut = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateDebut);
        calendar.add(Calendar.DAY_OF_MONTH, 3);       
        Date dateFin = calendar.getTime();
        DemandeConge demandeConge = new DemandeConge(dateDebut, dateFin);
       
        entityManager.persist(demandeConge);
        entityManager.flush();
               
        Query query = entityManager.createQuery("FROM DemandeConge dc");
        System.out.println(((DemandeConge)query.getSingleResult()).getNombreJoursDemandes());
       
        entityTransaction.commit();
        entityManager.close();
        entityManagerFactory.close();


The problem is that nombreJoursDemandes is 0 instead of being 3.

[java] Hibernate: insert into DemandeConge (id, DATE_DEBUT, DATE_FIN) values (null, ?, ?)
[java] Hibernate: call identity()
[java] Hibernate: select top ? demandecon0_.id as id1_, demandecon0_.DATE_DEBUT as DATE2_1_, demandecon0_.DATE_FIN
as DATE3_1_, DATEDIFF('dd', demandecon0_.DATE_DEBUT, demandecon0_.DATE_FIN) as formula1_ from DemandeConge demandecon0_
[java] 0


When I query the database myself with SELECT DATEDIFF('dd', DATE_DEBUT, DATE_FIN) FROM demandeConge it sends 3

if I use the query generated by hibernate in the hsqldb manager I get the correct answer 3.

But in my program nombreJoursDemandes stays at 0.

If someone can help, thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.