-->
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.  [ 3 posts ] 
Author Message
 Post subject: Question about overriding BigDecimalType
PostPosted: Mon Mar 13, 2006 5:27 pm 
Newbie

Joined: Mon Mar 13, 2006 5:19 pm
Posts: 5
I have a hibernate mapping file that maps my column to a big_decimal type. This is fine in our MySQL database. However, we have to connect to a legacy database as well which does not store it in the proper manner. It stores all numbers as integers basically and we have to apply the scale afterwards. So for example in MySQL we have a value of .05 and in the legacy database we have a value of 5 which has to have a -2 scale applied to it.

So, long story short, I already have my program written and all the domain objects use BigDecimal. Is there any way to have the same set of domain objects use the legacy database?

I have tried to use a subclass of UserType, but that requires my domain objects to be changed to use the new object. I really want to be able to not have to change my domain objects.

Thanks in advance.


Top
 Profile  
 
 Post subject: You can use a UserType
PostPosted: Mon Mar 13, 2006 7:21 pm 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
You can build a user type that maps ints from the legacy database to BigDecimal with the proper scale. You do not have to change your code at all. Here is what you can do in a class extending UserType:

Code:
public int[] sqlTypes() {
    return new int[] { Types.INTEGER };
}
public Class returnedClass() {
    return BigDecimal.class;
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
    Integer tmp = (Integer) Hibernate.INTEGER.nullSafeGet(rs, names[0]);
    return ... // Convert tmp.intValue() to BigInteger, apply the scale, and return
}
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
    BigDecimal tmp = (BigDecimal)value;
    Integer valueToSet = ... // Convert tmp to int, scaling it back
    Hibernate.INTEGER.nullSafeSet(st, valueToSet, index);
}

This should do the trick - I hope this helps.


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Fri Mar 17, 2006 4:57 pm 
Newbie

Joined: Mon Mar 13, 2006 5:19 pm
Posts: 5
Thanks skalinic!

I totally misunderstood how they were supposed to work. Everything qorks great now!


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