-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapping java.util.Date to Oracle 10g date field
PostPosted: Tue Nov 27, 2007 3:49 pm 
Newbie

Joined: Wed Jan 10, 2007 9:33 am
Posts: 13
Hi. I am using Hibernate 3,2. I have seen some commentary about this issue, but in my case I am trying to build a composite key to use with the findById() method. One of the key's columns is a java.util.Date. I am trying to create the composite key object, and then do a findById() with my DAO, passing the composite key. The problem is that the lookup never brings back the entity. It is failing because of the date object part of the key (yes, I am sure).

For a sanity check, I performed a simple Criteria query where I just setMaxSize() to 1. I brought back the first entity chosen. I then built the composite key that I am referring to from the retrieved entity's values. I then did a findById() with that composite key, and it worked.

So my problem is, how can I duplicate the Date object that was retrieved from the DB with my own values? I also did an "instanceof check" on the entity that I pulled back in the second experiment. The object is of type java.sql.Date. When I try to build a java.sql.Date with the date value that I wanted to use in the first experiment, the findById() does not return the entity.

Thanks.


Top
 Profile  
 
 Post subject: Less than ideal
PostPosted: Thu Nov 29, 2007 1:28 pm 
Newbie

Joined: Wed Jan 10, 2007 9:33 am
Posts: 13
Had to employ some native SQL to get around this oracle date query problem. Here's what I had to do to get around the mismatch on an oracle date query. I have this in my DAO, so it's only in one place. Hopefully better support for oracle dates will be included in a future Hibernate release.

Code:
    public MyDBEntity findById( com.cji.noah.entities.MyDBEntityId id)
    {
        log.debug("getting MyDBEntity instance with id: " + id);
        try {
            MyDBEntity instance = getInstanceUsingNativeSQL(id);
            if (instance==null) {
                log.debug("get successful, no instance found");
            }
            else {
                log.debug("get successful, instance found");
            }
            return instance;
        }
        catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
        catch (SQLException sqle)
        {
            log.error("get failed", sqle);
            throw new RuntimeException(sqle);
      }
    }
   
    private MyDBEntity getInstanceUsingNativeSQL(MyDBEntityId id) throws HibernateException, SQLException
    {
       String myEntitySQL = "Select * from SCHEMA.TABLE where srch_col1 = " + id.getSrchCol1() + " and " +
              "srch_col2 = " + id.getSrchCol2() + " and " +
              "srch_col3 = " + id.getSrchCol3() + " and " +
              "trunc(MY_DATE) = to_date('" + id.getSrchDate() + "','yyyy-mm-dd')";
       
       List queryResults = sessionFactory.getCurrentSession().createSQLQuery(myEntitySQL).addEntity(MyDBEntity.class).list();
       
       if(queryResults != null && !(queryResults.isEmpty()))
       {
          return (MyDBEntity) queryResults.get(0);
       }
       else
          return null;
   }
   


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.