-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate: Programmatically binding UserType’s on Components
PostPosted: Thu Apr 29, 2010 2:17 pm 
Newbie

Joined: Thu Apr 29, 2010 2:14 pm
Posts: 1
I have several different UserType's (org.hibernate.usertype.UserType) in the system. For this question I will focus on the fact that we use Joda time DateTime and its UserType, PersistentDateTime.

I have successfully bound the UserType programmatically to PersistentClass's by:
Code:
public void customize(Ejb3Configuration config) {

Iterator<PersistentClass> itr = config.getClassMappings();
   while(itr.hasNext()) {
       PersistentClass persistentClass = itr.next();
       for(Iterator<Property> iter = persistentClass.getPropertyIterator(); iter.hasNext();) {
           if(property.getType().getReturnedClass().equals(DateTime.class)) {
               SimpleValue original = (SimpleValue) property.getValue();
               SimpleValue value = new SimpleValue(original.getTable());
               value.setTypeName("org.joda.time.contrib.hibernate.PersistentDateTime");
               value.setTypeParameters(new Properties());
               value.setNullValue(original.getNullValue());

               Iterator<Column> colIter = original.getColumnIterator();
               while(colIter.hasNext())
                   value.addColumn(colIter.next());

               property.setValue(value);
           }

       }
   }
}


The above code WORKS... but not for Components which are not instances of PersistentClass. I tried the following code to handle Components as well... but it DOESN'T work when it really seems like it should.

Code:

public void customize(Ejb3Configuration config) {

Iterator<PersistentClass> itr = config.getClassMappings();
   while(itr.hasNext()) {
       PersistentClass persistentClass = itr.next();
       for(Iterator<Property> iter = persistentClass.getPropertyIterator(); iter.hasNext();) {
           Property property = iter.next();
           if(property.getType().getReturnedClass().equals(DateTime.class)) {
               SimpleValue original = (SimpleValue) property.getValue();
               SimpleValue value = new SimpleValue(original.getTable());
               value.setTypeName("org.joda.time.contrib.hibernate.PersistentDateTime");
               value.setTypeParameters(new Properties());
               value.setNullValue(original.getNullValue());

               Iterator<Column> colIter = original.getColumnIterator();
               while(colIter.hasNext())
                   value.addColumn(colIter.next());

               property.setValue(value);
           } else if (property.getValue() instanceof Component) {
               Component c = (Component)property.getValue();
               for(Iterator<Property> cIt= c.getPropertyIterator(); cIt.hasNext();) {
                    Property cProperty = cIt.next();
                    if(cProperty.getType().getReturnedClass().equals(DateTime.class)) {
                         SimpleValue original = (SimpleValue) cProperty.getValue();
                         SimpleValue value = new SimpleValue(original.getTable());
                         value.setTypeName("org.joda.time.contrib.hibernate.PersistentDateTime");
                         value.setTypeParameters(new Properties());
                         value.setNullValue(original.getNullValue());

                         Iterator<Column> colIter = original.getColumnIterator();
                         while(colIter.hasNext())
                             value.addColumn(colIter.next());

                         cProperty.setValue(value);
                    }
               }

           }

       }
}


I have tried tracing through the hibernate code. It seems like hibernate somehow handles Components differently than PersistentClasses but I can't seem to figure out how. My code above seems to correctly set the typeName on the component property but hibernate never uses the PersistentDateTime UserType when DateTime is used inside an @Embeddable Component. Of course, if I add the @Type annotation everything works. I don't want to have to add this annotation. I want to be able to bind the UserType programmatically for Components just the same as PersistentClasses.

Any insights?

Thanks!


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

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.