-->
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: UserType and fully qualified classname
PostPosted: Tue Jun 07, 2005 4:35 am 
Newbie

Joined: Sun Jun 05, 2005 10:55 am
Posts: 4
Location: Germany
Hello,

I'm using hibernate3 on DB2 with a custom usertype to trim strings. My question is, do I have to use every time the full class name:

type="de.test.framework.hibernate3.usertype.TrimmedString" ?

I there a way to register this usertype, so that I can use an alias like

type="TrimmedString" ?

Thank you,

Holger


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 07, 2005 5:08 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Yes, see the refererence documentation about usertype.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 07, 2005 6:05 am 
Newbie

Joined: Sun Jun 05, 2005 10:55 am
Posts: 4
Location: Germany
christian wrote:
Yes, see the refererence documentation about usertype.


For the next user, which looks for the answer:

http://www.hibernate.org/hib_docs/v3/re ... pes-custom


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 07, 2005 12:29 pm 
Newbie

Joined: Wed Nov 12, 2003 3:49 pm
Posts: 19
Holger,

Did you have to modify your java class to make your custom usertype work? I find references to interface org.hibernate.usertype.UserType in Hibernate in Action, but the online html doc reads as though this can all be mapped in xml without modifying the java source.

Do I need to implement the UserType interface to make my custom class work, or can it all be done declaratively through the hbm file?

Thanks,
Dave

(I am using custom usertypes just for the purpose of strong typing throughout my applications. My classes aren't much more than wrappers around the java wrapper classes - nothing fancy.)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 08, 2005 2:46 am 
Newbie

Joined: Sun Jun 05, 2005 10:55 am
Posts: 4
Location: Germany
DaveSegal wrote:
Holger,

Did you have to modify your java class to make your custom usertype work? I find references to interface org.hibernate.usertype.UserType in Hibernate in Action, but the online html doc reads as though this can all be mapped in xml without modifying the java source.

Do I need to implement the UserType interface to make my custom class work, or can it all be done declaratively through the hbm file?

Thanks,
Dave

(I am using custom usertypes just for the purpose of strong typing throughout my applications. My classes aren't much more than wrappers around the java wrapper classes - nothing fancy.)


I used the usertype from here:
http://forum.hibernate.org/viewtopic.ph ... mmedstring
and extended it for hibernate3. No, I did not modify my java class and I don't know if there is a way to do this only declaratively through xml-mapping. Maybe anybody else knows this ?

I use this usertype:

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

public class TrimmedString implements UserType {
/**
* No arg-constructor.
* Creates a TrimmedString object.
*/
public TrimmedString() {
super();
}

/**
* @see org.hibernate.usertype.UserType#sqlTypes()
*/
public int[] sqlTypes() {
return new int[] { Types.CHAR };
}

/**
* @see org.hibernate.usertype.UserType#returnedClass()
*/
public Class returnedClass() {
return String.class;
}

/**
* @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
*/
public boolean equals(final Object x, final Object y) throws HibernateException {
return (x == y) || (x != null && y != null && (x.equals(y)));
}

/**
* @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet(final ResultSet rs, final String[] names, final Object owner) throws HibernateException, SQLException {
String val = rs.getString(names[0]);
if (null == val) {
return null;
}
return val.trim();
}

/**
* @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
*/
public void nullSafeSet(final PreparedStatement st, final Object value, final int index) throws HibernateException, SQLException {
st.setString(index, (String) value);
}

/**
* @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
*/
public Object deepCopy(final Object value) throws HibernateException {
if (value == null) {
return null;
}
return new String((String) value);
}

/**
* @see org.hibernate.usertype.UserType#isMutable()
*/
public boolean isMutable() {
return false;
}

/**
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/
public int hashCode(final Object x) throws HibernateException {
return x.hashCode();
}

/**
* @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
*/
public Serializable disassemble(final Object value) throws HibernateException {
return (Serializable) value;
}

/**
* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
*/
public Object assemble(final Serializable cached, final Object owner) throws HibernateException {
return cached;
}

/**
* @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
*/
public Object replace(final Object original, final Object target, final Object owner) throws HibernateException {
return original;
}
}

In my mapping:
<typedef class="hibernate3.usertype.TrimmedString" name="trimmed_string" /

For example:

<property name="beschreibung" column="BESCHREIBUNG" type="trimmed_string" length="25"/>


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Fri Jun 10, 2005 12:10 pm 
Newbie

Joined: Wed Nov 12, 2003 3:49 pm
Posts: 19
Holger - That was very helpful - I got it working.

Let me say three things: ThankYou, ThankYou, ThankYou!

Dave


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.