-->
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.  [ 6 posts ] 
Author Message
 Post subject: MySQL vs. DB2 length checks and consistency in hibernate
PostPosted: Fri May 06, 2005 11:02 am 
Newbie

Joined: Fri Apr 01, 2005 1:03 am
Posts: 12
Hibernate version: 3.0

Hello.

I am working with a group developing an application with Hibernate. We have been using MySQL in our Developer environment and Continuous Integration Environments, and DB2 OS/390 in our Qual environment (with the goal of DB2 OS/390 when we go to PROD).

We have noticed some slight differences in how our unit tests (and application) behave across these databases, and I was wondering if anyone has encountered this and/or developed an elegant work around.

The problem is that MySQL does not seem to complain when we store a String value (say 'StringValue1') that is longer than the maximum length set by the VARCHAR declaration. DB2 on the other hand throws an exception mentioning that the 'host variable' is longer than allowed.

We'd like to intercept these length problems before any SQL is attempted to execute and throw a consistent exception (thus, giving our code consistent behavior with both MySQL and DB2). Has anyone else encountered this and developed a solution? If not, what are your thoughts on the most appropriate place to do something like this in the Hibernate API?

- Matt


Top
 Profile  
 
 Post subject: MySQL vs. DB2 length checks and consistency in hibernate
PostPosted: Mon May 16, 2005 1:05 am 
Newbie

Joined: Fri Apr 01, 2005 1:03 am
Posts: 12
So.... I found PreInsertEventListener, which looks like it might be in the right spot for me to check a length just before delegating to the database (so that it will be enforced for mysql). I'm having a problem finding the length of a varchar field within the context of the PreInsertEvent though. There doesn't seem to be any access to the Configuration object or mappings. There's access to a Session, SessionFactory, Persister, and the actual entity. Does anyone have an idea how to get the maxlength information in the context of the event?

Code:
        Configuration cfg = new Configuration().configure();
       
        cfg.getSessionEventListenerConfig().setPreInsertEventListener(new PreInsertEventListener() {
            public boolean onPreInsert(PreInsertEvent event)
            {
                ClassMetadata classData = event.getPersister().getClassMetadata();
                String propertyNames[] = classData.getPropertyNames();
                Type types[] = classData.getPropertyTypes();

                for(int x=0;x<types.length;x++)
                {                   
                    if(types[x] instanceof StringType)  // only works where a java var maps directly to a single varchar column in a table
                    {
                            EntityPersister entityPersister = event.getPersister();
                            String value = (String) entityPersister.getPropertyValue(event.getEntity(),x,EntityMode.POJO);

                            int maxLength = 0; //?????? where to get this??
                            int actualLength = value.length();
                            System.out.println("Checking varchar field [" +  propertyNames[x] + "].   Value is [" + value + "].  Length is [" + actualLength + "].  Max length is [" + maxLength + "]");
                           
                            if(actualLength > maxLength)
                            {
                                throw new HibernateException(propertyNames[x] + " in " + event.getEntity().getClass() + " exceeds max length allowed by database (" + maxLength + ")");
                            }
                    }
                }
               
                return false;
            }
        });



Top
 Profile  
 
 Post subject:
PostPosted: Mon May 16, 2005 1:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No no no. The correct place to do this is in a UserType.


Top
 Profile  
 
 Post subject: MySQL vs. DB2 length checks and consistency in hibernate
PostPosted: Mon May 16, 2005 9:40 pm 
Newbie

Joined: Fri Apr 01, 2005 1:03 am
Posts: 12
I'm sorry. Perhaps I'm just dense, but I've been looking through the documentation, looking at samples in the hibernate junit tests, and am not having much luck seeing a clear path through this. Perhaps it's just my unfamiliarity with the API.

I've tried implementing UserType in a class called MaxLengthStringType with the plan to catch the strings which exceed the max length in the nullSafeSet() method. However, I'm back to my original problem of not being able to cleanly lookup the actual maximum 'length' that's specified in the hibernate mappings.

If I used a ParameterizedType, I would have to specify the length twice in the mapping file, right?

If I use a CompositeUserType, I have access to an additional method which provides a SessionImplementor, but I don't see a clear path to lookup the length attribute of the value which is being set:
Code:
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)


Any help is appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 16, 2005 11:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You can use a ParameterizedUserType and then typedef the common lengths you are using. Or I could see about passing the length as a standard parameter. If you add a feature request for that, I will look into it.


Top
 Profile  
 
 Post subject: MySQL vs. DB2 length checks and consistency in hibernate
PostPosted: Tue May 31, 2005 2:30 pm 
Newbie

Joined: Fri Apr 01, 2005 1:03 am
Posts: 12
Here is the JIRA request:

http://opensource.atlassian.com/project ... se/HHH-563


Thanks.
- Matt


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