-->
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.  [ 5 posts ] 
Author Message
 Post subject: Custom Usertype
PostPosted: Tue Mar 23, 2010 9:19 am 
Newbie

Joined: Tue Mar 23, 2010 9:04 am
Posts: 6
Hi

I have a legacy database where string have not been implemented with VARCHAR but instead CHAR with a length. They are all not nullable but uses a special character to represent a database null.

I have created my own usertype that trim and convert null, so I can work with the java type String in an ordinary way.

My entity model is pretty large and there is a lot of String fields, how do I define that I need all mappings that return a String to use my new type.

Iam using annotations and I really dont want to specify it on each element like.

Code:
@org.hibernate.annotations.Type(type = "com.mysite.usertypes.MyUserType")


Kind regards, Flemming Behrend


Top
 Profile  
 
 Post subject: Re: Custom Usertype
PostPosted: Tue Mar 23, 2010 10:32 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
What about Interceptors?

You could even configure a list of "suitable fields" to which apply the transformation (instead of every String)


Top
 Profile  
 
 Post subject: Re: Custom Usertype
PostPosted: Tue Mar 23, 2010 11:02 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
After thinking twice I am wondering if my previous post is bollocks.

Maybe the only solution is replacing org.hibernate.type.StringType class with your own implementation.
Could it be done at runtime with some kind of CGILIB magic?


Top
 Profile  
 
 Post subject: Re: Custom Usertype
PostPosted: Tue Mar 23, 2010 7:02 pm 
Newbie

Joined: Tue Mar 23, 2010 9:04 am
Posts: 6
Thanks for the suggestion, I was hoping that there was a way to configure hibernate to use a custom usertype instead of one of it's build in types.

Setting it in the configuration file or something so I only had to do it one place.

I not going to hack the class files, then I will rather create a little tool that runs through all my entities and add the hibernate annotation to the source file.


Top
 Profile  
 
 Post subject: Re: Custom Usertype
PostPosted: Tue Mar 23, 2010 7:40 pm 
Newbie

Joined: Tue Mar 23, 2010 7:20 pm
Posts: 2
I'm having the exact same problem. It makes it impossible to have DRY code when you have to do type mapping on every single usage. Furthermore if you've created a type that constrains the scale or precision of a type, you shouldn't need to add a @Column annotation and redundantly specify this.

Consider this case:

I have a custom UserType called Yield which extends BigDecimal but has a validate() method to ensure that the scale is <= 4 and precision is <=8. Then I have a YieldUserType object that handles implementing UserType.

To use this type I'd need the following annotations applied to all members of type Yield even though it's redundant.

Code:
@Column(scale = 4, precision = 8)
@Type(type = "org.hibernate.type.BigDecimalType")
private Yield escrowYield;

@Column(scale = 4, precision = 8)
@Type(type = "org.hibernate.type.BigDecimalType")
private Yield brokerYield;

@Column(scale = 4, precision = 8)
@Type(type = "org.hibernate.type.BigDecimalType")
private Yield instrumentYield;


It seems like some kind of conversion strategy enhancement would be a good solution where you essentially map concrete custom types to usertypes and ideally provide @Column defaults. The documentation even refers to "conversion strategies" on this page: http://docs.jboss.org/hibernate/stable/core/reference/en/html/mapping.html#mapping-types-custom however there is no further mention of what this is.

Does anyone know of anything like this already in hibernate?


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