-->
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.  [ 5 posts ] 
Author Message
 Post subject: POJO Date property set to null ends up set to 1969-12-31
PostPosted: Wed Mar 23, 2005 11:23 pm 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:20 pm
Posts: 43
What is the correct way to set a Date property to null such that both Java and SQL will treat/see it as null.

Thanks in advance - Richard

Hibernate version:
2.1.8

Mapping documents:
Code:
<hibernate-mapping>
    <class name="com.acme.persistence.PoMeasurementItem" table="PoMeasurementItem" proxy="com.acme.persistence.PoMeasurementItem" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version" >
        <id name="id"type="long" unsaved-value="null" >
                <column name="id"  unique-key="id" />
            <generator class="native"></generator>
        </id>
        <version name="version" type="int"
            column="version" access="property" unsaved-value="undefined" />
        <many-to-one name="measurementItemSet" class="com.acme.persistence.PoMeasurementItemSet"
            cascade="none" outer-join="true" update="true" insert="true"
            access="property" >
            <column name="measurementItemSet" unique-key="userkey" />
        </many-to-one>
        <property name="liNum" type="int" update="true" insert="true"
            access="property" column="liNum" />
        <property name="reviewDate" type="java.sql.Date"
            update="true" insert="true" access="property" >
            <column name="reviewDate"  unique-key="userkey" />
        </property>
        <set name="assessmentItems" table="PoAssessmentItem" lazy="true"
            inverse="true" cascade="all" sort="unsorted" >
              <key column="measurementItem"></key>
              <one-to-many class="com.acme.persistence.PoAssessmentItem" />
        </set>
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
            ...
            java.sql.Date reviewDate = null;
            for (int i=0; i<ps.getProblemCt();i++) {
                PoMeasurementItem mi = new PoMeasurementItem();
                mi.setLiNum(i);
                mis.addMeasurementItem(mi);
                mi.setReviewDate(reviewDate);
            }
            ...
            if (cmMI != null) {
                userTx.getSession().save(cm.getMeasurementParms());
                userTx.getSession().save(cm);
                userTx.getSession().flush();
            }
            ...


Later the check below to see if the ReviewDate is null fails:
Code:
            PoMeasurementItem measurementItem = ai.getMeasurementItem();
            if (reviewItem && measurementItem.getReviewDate() == null ) {
                measurementItem.setReviewDate(assessment.getAssessmentDate());
            }


Also, the query
select * from PoMeasurementItem where reviewDate is null ;
returns no rows from the database as they are all set to '1969-12-31'.

Full stack trace of any exception that occurs:
No exception - but value of 1969-12-31 is stored in the database.


Name and version of the database you are using:
MySQL 4.1.7

The generated SQL (show_sql=true):
Code:
create table PoMeasurementItemSet (
   id BIGINT NOT NULL AUTO_INCREMENT,
   version INTEGER not null,
   classMeasurement BIGINT not null,
   problemSet BIGINT not null,
   lisNum INTEGER not null,
   primary key (id),
   unique (classMeasurement, problemSet)
) type=InnoDB ;


Debug level Hibernate log excerpt:
N/A


Top
 Profile  
 
 Post subject: POJO Date property set to null ends up set to 1969-12-31
PostPosted: Thu Mar 24, 2005 12:09 am 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:20 pm
Posts: 43
BTW, I have searched the forum (using "MySQL Date null 1969-1231" and googled, as well as Hibernate in Action, the reference manual. FAQ and Wiki. If I've overlooked this, apologies but I really did look.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 24, 2005 4:56 am 
Beginner
Beginner

Joined: Wed Jan 26, 2005 5:34 am
Posts: 41
Location: France, Paris
Just a suggestion : 1969-12-31 seems to be 1970-01-01 with a negative timezone. Maybe should you search in this way...

_________________
Vincent


Top
 Profile  
 
 Post subject: POJO Date property set to null ends up set to 1969-12-31
PostPosted: Thu Mar 24, 2005 10:42 am 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:20 pm
Posts: 43
Thanks for the suggestion - but running my searches scans using 1970-01-01 did not turn up any new relant information on how to check for a null Date property for a Hibernate persisted object.

Still looking - I really hate to thing I'm going to have to hardcode something like:
Code:
  if ( measItem.getReviewDate() == null || measItem.equals('1969-12-31') ) {
...
  }


Top
 Profile  
 
 Post subject: POJO Date property set to null - SOLVED
PostPosted: Thu Mar 24, 2005 1:03 pm 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:20 pm
Posts: 43
One more example of what too little sleep can do to you.

My POJO getter was using a nullSafe method to return a default of "new Date(0)" when Date was null.

Sincere apologies for wasting anybody's time.

-Richard


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

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.