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.  [ 7 posts ] 
Author Message
 Post subject: Oracle Empty Strings and NULL
PostPosted: Tue Apr 09, 2013 12:42 pm 
Newbie

Joined: Tue Apr 09, 2013 11:52 am
Posts: 4
Oracle sets all empty strings to NULL when writing to the database. My problem is, that in my java domain it be much simpler if these NULL's were empty strings. I could do this by doing Null checks in all the setters, but I would like to avoid that if I could.

Is there anyway to intercept incoming Strings and do a NULL check in one place for the entire application. I tried using interceptors, but those do not intercept individual incoming fields, but they intercept the java object.

There is a interface called FieldInterceptor that looks like it might be able to be used to suit my needs, but I can not find any documentation for it, or any examples about how to do something like this.

Does anyone know any simple way to do this?


Top
 Profile  
 
 Post subject: Re: Oracle Empty Strings and NULL
PostPosted: Wed Apr 10, 2013 1:54 am 
Regular
Regular

Joined: Wed Apr 10, 2013 1:02 am
Posts: 50
Hi,

I am using hibernate 4.X and Oracle XE.

You can set interceptor on a session or on a SessionFactory.

for sessionfactory use

new Configuration().setInterceptor( new NullInterceptor() );

for session use

Code:

SessionBuilder sessionbuilder =new Configuration(). configure().buildSessionFactory().
            withOptions();
      sessionbuilder.interceptor(interceptor);



Here is a simple example

Code:
package com.akash.interceptor;

import java.io.Serializable;

import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;

import com.akash.domain.User;


public class NullInterceptor  extends EmptyInterceptor{
   private static final long serialVersionUID = 1L;
@Override
public boolean onSave(Object entity, Serializable id, Object[] state,
      String[] propertyNames, Type[] types) {
   System.out.println("On Save Interceptor");
   if(null==entity||!(entity instanceof User))
      return false;
   System.out.println(entity.toString());
   User user =(User) entity;
   if(user.getFirstName().toString().isEmpty())
      user.setFirstName("default");
   return true;
}

   
}


Then apply this interceptor in main

Code:
package com.akash.domain;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionBuilder;
import org.hibernate.cfg.Configuration;

import com.akash.domain.User;
import com.akash.interceptor.NullInterceptor;
import com.akash.util.Hibernate4DaoFactory;
import com.akash.util.Hibernate4Util;
public class MainClient {
   public static void main(String[] args) {
      User user = new User(12L, "Sarah", "Williams");
      User user1 = new User(13L, "Wiliiam", "Parker");
      User user2= new User(14L, "James", "Gostling");
      
      NullInterceptor interceptor = new NullInterceptor();
      SessionBuilder sessionbuilder =new Configuration(). configure().buildSessionFactory().
            withOptions();
      sessionbuilder.interceptor(interceptor);
      Session session =sessionbuilder.openSession();
      session.beginTransaction();
      session.save(user);
      session.getTransaction().commit();

   }

}


Top
 Profile  
 
 Post subject: Re: Oracle Empty Strings and NULL
PostPosted: Wed Apr 10, 2013 9:42 am 
Newbie

Joined: Tue Apr 09, 2013 11:52 am
Posts: 4
Thanks, but unfortunately I do not think that will work. That is similar to what I tried before I posted to the forum. The problem is I am talking about var Char which are just columns in the database. When they get saved and loaded they individually do not go through the interceptor, they go through the interceptor as part of the Object that is being saved or loaded. So even if there are 10 fields that are Strings coming from the DB, the object it is coming in on would still not be null.

What I would like to do is intercept each field coming in and do a null check, but I have not yet figured out how to do that.

Thanks


Top
 Profile  
 
 Post subject: Re: Oracle Empty Strings and NULL
PostPosted: Wed Apr 10, 2013 9:46 am 
Newbie

Joined: Tue Apr 09, 2013 11:52 am
Posts: 4
Actually looking at your response again my previous post is not valid.

I could do what you said, but I do not want to individually check each field. If I wanted to do that I could do that in the setters. This will be a common occurrence across the entire application on multiple objects, so I would like to do it in one place rather then doing null checks in setters or in the interceptor itself on the field level.


Top
 Profile  
 
 Post subject: Re: Oracle Empty Strings and NULL
PostPosted: Wed Apr 10, 2013 10:57 am 
Regular
Regular

Joined: Wed Apr 10, 2013 1:02 am
Posts: 50
My dear friend its absolutely possible. You can follow these steps

1 apply intercepter on session factory
2 use introspecter to get all getters and assign to methods array.. (jmx)
3 read the datatype for each method

Above logic in onsave method.

_________________
Regards
Akash Miital
http://akash.thegrassroots.co.in/hibernate/


Top
 Profile  
 
 Post subject: Re: Oracle Empty Strings and NULL
PostPosted: Wed Apr 10, 2013 10:59 am 
Regular
Regular

Joined: Wed Apr 10, 2013 1:02 am
Posts: 50
My dear friend its absolutely possible. You can follow these steps

1 apply intercepter on session factory
2 use introspecter to get all getters and assign to methods array.. (jmx)
3 read the datatype for each method

Above logic in onsave method.

_________________
Regards
Akash Miital
http://akash.thegrassroots.co.in/hibernate/


Top
 Profile  
 
 Post subject: Re: Oracle Empty Strings and NULL
PostPosted: Wed Apr 10, 2013 11:39 am 
Newbie

Joined: Tue Apr 09, 2013 11:52 am
Posts: 4
Well that would work on save, but I am trying to do it on load.

But anyways I just figured it out, here is the code if you are curious.

Code:
public boolean onLoad( Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types ) {
        for ( int i = 0; i < state.length; i++ ) {
            if ( types[ i ] instanceof StringType ) {
                if ( state[ i ] == null ) {
                    state[ i ] = "";
                }
            }
        }
        return super.onLoad( entity, id, state, propertyNames, types );
    }



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