4 Replies Latest reply on May 30, 2008 2:27 PM by Uros Majeric

    Curios hibernate behavior

    Daniel Wachter Newbie

      Hello together


      Maybe I must post this in a hibernate forum, but before seam I never noticed a similar behavior.


      I just noticed some strange hibernate behavior when I select a list of objects:



      My bean:


      List<Alert> m_LastAlerts = m_EM.createQuery("SELECT a FROM Alerts a").getResultList();
      for(Alert alert : m_LastAlerts) {
         System.out.println("ALERT: " + alert.getID() + " / " + alert.getDateTime());
      }



      My output:


      13:43:35,191 INFO  [STDOUT] ALERT: 1 / 2008-03-10 13:42:58.0
      13:43:35,191 INFO  [STDOUT] ALERT: 2 / 2008-03-09 13:42:58.0
      13:43:35,192 INFO  [STDOUT] ALERT: 3 / 2008-03-10 13:42:58.0



      But the data in my database has the following data:


      ALERT: 1 / 2008-03-08
      ALERT: 2 / 2008-03-09
      ALERT: 3 / 2008-03-10



      The Query from hibernate is correct (And would return the right dates) but it seams that hibernate takes a cached date for creating a new object!
      If I make a Limit on the query (e.g. Limit 2) the date comes correct!
      Any suggestions for me? Should I better post this problem in a hibernate forum or is my question stupid?
      Thank you


      My Entities:
      Dysfunction.java:


      @MappedSuperclass
      public abstract class Dysfunction implements Serializable {
      
           private long m_ID;
           private Date m_DateTime;
      
           @Id
           @GeneratedValue
           @Column(name = "id")
           public long getID() {
                return m_ID;
           }
      
           /**
            * 
            * @param p_ID
            *            ID of the Dysfunction
            */
           public void setID(long p_ID) {
                m_ID = p_ID;
           }
      
           @NotNull
           @Basic
           @Temporal(TemporalType.TIMESTAMP)
           public Date getDateTime() {
                return m_DateTime;
           }
      
           /**
            * 
            * @param p_DateTime
            *            Date and time when the dysfunction occured
            */
           public void setDateTime(Date p_DateTime) {
                m_DateTime = p_DateTime;
           }
      
           @NotNull
           public int getAlertBit() {
                return m_AlertBit;
           }
      
      
      }





      Alert.java


      @Entity
      @Table(name = "alert")
      @Name("alert")
      public class Alert extends Dysfunction implements Serializable {
      
           private boolean m_Generated;
           private Causation m_Causation;
           private AlertSetting m_AlertSetting;
           
      
           /**
            * Default Constructor
            */
           public Alert() {
      
           }
      
           @ManyToOne
           @NotNull
           public Causation getCausation() {
                return m_Causation;
           }
      
           /**
            * 
            * @param p_Causation
            *            Causation of the alert
            */
           public void setCausation(Causation p_Causation) {
                m_Causation = p_Causation;
           }
      
           @ManyToOne
           public AlertSetting getAlertSetting() {
                return m_AlertSetting;
           }
      
           /**
            * 
            * @param p_AlertSetting
            *            AlertSetting of the alert
            */
           public void setAlertSetting(AlertSetting p_AlertSetting) {
                m_AlertSetting = p_AlertSetting;
           }
      
      
           public boolean isGenerated() {
                return m_Generated;
           }
      
           /**
            * 
            * @param p_Generated
            *            Generated flag of the alert
            */
           public void setGenerated(boolean p_Generated) {
                m_Generated = p_Generated;
           }
      
      }



      My Import script:


      INSERT INTO alert (dateTime, generated, causation_id) VALUES (DATE_SUB(sysdate(), INTERVAL 2 DAY), 0, 1)
      INSERT INTO alert (dateTime, generated, causation_id) VALUES (DATE_SUB(sysdate(), INTERVAL 1 DAY), 0, 1)
      INSERT INTO alert (dateTime, generated, causation_id) VALUES (sysdate(), 0, 1)