-->
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: **Edit: Its working: Why doesn't my adapted usertype work?
PostPosted: Mon Oct 22, 2012 11:36 pm 
Newbie

Joined: Mon Oct 22, 2012 10:40 pm
Posts: 1
This code is actually working sorry I posted too soon.


Can someone please shed some light on why the following would not be trimming text:

import java.io.Serializable;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Types;

import org.apache.commons.lang.StringUtils;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;

import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.usertype.UserType;

/**

* Custom class for trimming strings on the way out from the database, Hibernate 3 version

* @author Paul Newport

*/

public class CustomStringTrimTypeH4 implements UserType {

/**

* default constructor

*/

public CustomStringTrimTypeH4() {

}

/**

* @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(Object x, Object y) {

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(ResultSet inResultSet, String[] names, Object o)

throws SQLException {

String val =

(String) StandardBasicTypes.STRING.nullSafeGet(inResultSet, names[0]);

return StringUtils.trim(val);

}
*/
/**

* @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)

*/
/*
public void nullSafeSet(

PreparedStatement inPreparedStatement,

Object o,

int i)

throws SQLException {

String val = (String) o;

inPreparedStatement.setString(i, val);

}
*/
/**

* @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)

*/

public Object deepCopy(Object o) {

if (o == null) {

return null;

}

return new String(((String) o));

}

/**

* @see org.hibernate.usertype.UserType#isMutable()

*/

public boolean isMutable() {

return false;

}

/**

* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)

*/

public Object assemble(Serializable cached, Object owner) {

return cached;

}

/**

* @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)

*/

public Serializable disassemble(Object value) {

return (Serializable) value;

}

/**

* @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)

*/

public Object replace(Object original, Object target, Object owner) {

return original;

}

/**

* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)

*/

public int hashCode(Object x) {

return x.hashCode();

}

@Override
public Object nullSafeGet(ResultSet inResultSet, String[] names,
SessionImplementor sessionimpl, Object ob) throws HibernateException,
SQLException {
/*String val = (String) StandardBasicTypes.STRING.nullSafeGet(inResultSet, names[0],sessionimpl, ob);

return StringUtils.trim(val);*/
String val = inResultSet.getString(names[0]);
return val != null ? val.trim() : null;
}

@Override
public void nullSafeSet(PreparedStatement inPreparedStatement, Object o, int i,
SessionImplementor arg3) throws HibernateException, SQLException {
String val = (String)o;
inPreparedStatement.setString(i, StringUtils.trim(val));

}

}


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.