[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - UserType ignored

terryb do-not-reply at jboss.com
Mon Apr 14 01:59:27 EDT 2008


Hi,

I am trying to implement a usertype for spaced trimmed String. I have done as below but during execution Hibernate just ignores my usertype.

Is there something else needed for my StringTrimmed usertype to be used instead of the default String?


  | ----- UserType ----------------------
  | package au.edu.tisc.usertype;
  | 
  | import java.io.Serializable;
  | import java.sql.PreparedStatement;
  | import java.sql.ResultSet;
  | import java.sql.SQLException;
  | 
  | import org.hibernate.Hibernate;
  | import org.hibernate.HibernateException;
  | import org.hibernate.usertype.UserType;
  | 
  | /**
  |  * String user type with left and right spaces trimmed.
  |  */
  | public class StringTrimmed implements UserType {
  | 	
  | 	public int[] sqlTypes() {
  | 		return new int[] { Hibernate.STRING.sqlType() };
  | 	}
  | 
  | 	public Class returnedClass() {
  | 		return String.class;
  | 	}
  | 
  | 	public boolean isMutable() {
  | 		return false;
  | 	}
  | 
  | 	public Object deepCopy(Object value) {
  | 		
  | 		String stringValue = (String) value;
  | 		String copy = new String(stringValue);
  | 		return copy;
  | 	}
  | 
  | 	public Serializable disassemble(Object value) {
  | 		return (Serializable) value;
  | 	}
  | 
  | 	public Object assemble(Serializable cached, Object owner) {
  | 		return cached;
  | 	}
  | 
  | 	public Object replace(Object original, Object target, Object owner) {
  | 		
  | 		String stringValue = (String) original;
  | 		String copy = new String(stringValue);
  | 		return copy;
  | 	}
  | 
  | 	public boolean equals(Object x, Object y) {
  | 		
  | 		if (x == y) {
  | 			return true;
  | 		}
  | 		
  | 		if (x == null || y == null) {
  | 			return false;
  | 		}
  | 		
  | 		return x.equals(y);
  | 	}
  | 
  | 	public int hashCode(Object x) {
  | 		return x.hashCode();
  | 	}
  | 
  | 	public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws SQLException {
  | 
  | 		String stringValue = resultSet.getString(names[0]);
  | 		if (resultSet.wasNull()) {
  | 			return null;
  | 		}
  | 		
  | 		return stringValue;
  | 	}
  | 
  | 	public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException, SQLException {
  | 		
  | 		if (value == null) {
  | 			statement.setNull(index, Hibernate.STRING.sqlType());
  | 		} else {
  | 			String untrimmedValue = (String) value;
  | 			statement.setString(index, untrimmedValue.trim());
  | 		}
  | 	}
  | }
  | -----------------------------------------------------------
  | 
  | ----------------- Entity class using usertype -------------
  | 
  | @Entity
  | @Table(name = "Client")
  | @TypeDef(name="StringTrimmed", typeClass=au.edu.tisc.usertype.StringTrimmed.class)
  | public class Client implements java.io.Serializable {
  | 
  |     private String id;
  |     ...
  |         
  | 	@Type(type = "StringTrimmed")
  |     private String notes;
  | 
  |     ...
  | 
  |     @Column(name = "Notes", length = 250)
  |     @Length(max = 250)
  |     public String getNotes() {
  |         return this.notes;
  |     }
  | 
  |     public void setNotes(String notes) {
  |         this.notes = notes;
  |     }
  | 
  | }
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4143763#4143763

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4143763



More information about the jboss-user mailing list