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] 0When 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.