-->
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: Embedded Objects don't seem to use UserType
PostPosted: Sun Dec 01, 2013 5:23 pm 
Newbie

Joined: Sun Dec 01, 2013 4:38 pm
Posts: 2
I am very new to Hibernate so this may be a simple problem (I hope).
I am connecting to a Legacy database which I have no control over. Most of the String fields are defined as fixed length char instead of var-char so they need to be trimmed to be useful. I have followed the recommendations discussed here .... https://forum.hibernate.org/viewtopic.php?t=928294 to a make a UserClass that trims the strings.

This User class works well at first level, but Strings in embedded objects still remained un-trimmed. Am I mis-understanding something?

Customer Class ....
Code:
@Entity
@Table(name = "CUSMST")
public class Hib_CUSMST_Customer implements Serializable {
   @Id
   @Column(name = "MCCUST", columnDefinition = "decimal")
   private Integer id;

   /**
    * "C"ompany "I"ndividaul
    */
   @Column(name = "MCTYCS", columnDefinition = "char")
   private String custType;

   // @AttributeOverrides({ @AttributeOverride(name = "name", column = @Column(name = "MCCUSN", columnDefinition = "char(50)")),
   // @AttributeOverride(name = "firstName", column = @Column(name = "MCCNM1", columnDefinition = "char(20)")),

   @Embedded
   @AttributeOverrides({ @AttributeOverride(name = "name", column = @Column(name = "MCCUSN", columnDefinition = "char")),
         @AttributeOverride(name = "firstName", column = @Column(name = "MCCNM1", columnDefinition = "char")),
         @AttributeOverride(name = "firstInitial", column = @Column(name = "MCINT1", columnDefinition = "char")),
         @AttributeOverride(name = "secondName", column = @Column(name = "MCCNM2", columnDefinition = "char")),
         @AttributeOverride(name = "secondInitial", column = @Column(name = "MCINT2", columnDefinition = "char")),
         @AttributeOverride(name = "preferredName", column = @Column(name = "MCCNMP", columnDefinition = "char")),
         @AttributeOverride(name = "preferredInitial", column = @Column(name = "MCINTP", columnDefinition = "char")),
         @AttributeOverride(name = "titleCode", column = @Column(name = "MCTITL", columnDefinition = "char")),
         @AttributeOverride(name = "standardName", column = @Column(name = "MCSTDN", columnDefinition = "char")),
         @AttributeOverride(name = "standardAltName", column = @Column(name = "MCSTDA", columnDefinition = "char")) })
   private Hib_CustomerName name;

   @Column(name = "MCADR#", columnDefinition = "decimal")
   private Integer addressId;


Embedded Customer Name ....
Code:
@Embeddable
@Access(AccessType.PROPERTY)
public class Hib_CustomerName implements Serializable {
   @Type(type = "nz.co.xxxx.bo.hibernate.HnzString")
   @Column(name = "name")
   private String name;
   @Type(type = "nz.co.xxxx.bo.hibernate.HnzString")
   @Column(name = "firstName")
   private String firstName;
   @Type(type = "nz.co.xxxx.bo.hibernate.HnzString")
   @Column(name = "firstInitial")
   private String firstInitial;
   @Type(type = "nz.co.xxxx.bo.hibernate.HnzString")
   @Column(name = "secondName")
   private String secondName;
   @Type(type = "nz.co.xxxx.bo.hibernate.HnzString")
   @Column(name = "secondInitial")
   private String secondInitial;
   @Type(type = "nz.co.xxxx.bo.hibernate.HnzString")
   @Column(name = "preferredName")
   private String preferredName;
   @Type(type = "nz.co.xxxx.bo.hibernate.HnzString")
   @Column(name = "preferredInitial")
   private String preferredInitial;
   @Column(name = "titleCode")
   @Type(type = "nz.co.xxxx.bo.hibernate.HnzString")
   private String titleCode;
   private String standardName;
   private String standardAltName;




String UserClass ....
Code:
package nz.co.xxxx.bo.hibernate;

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.engine.spi.SessionImplementor;
import org.hibernate.usertype.UserType;

public class HnzString implements UserType {
   public HnzString() {
      super();
   }

   public int[] sqlTypes() {
      return new int[] { Types.CHAR };
   }

   @SuppressWarnings("rawtypes")
   public Class returnedClass() {
      return String.class;
   }

   public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
      String val = rs.getString(names[0]);
      if (null == val)
         return (null);
      return val.trim();
   }

   @Override
   public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
      return nullSafeGet(rs, names, owner);
   }

   public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
      st.setString(index, (String) value);
   }

   @Override
   public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
      nullSafeSet(st, value, index);
   }

   @Override
   public Serializable disassemble(Object value) throws HibernateException {
      return (Serializable) value;
   }

   @Override
   public Object assemble(Serializable cached, Object owner) throws HibernateException {
      return cached;
   }

   @Override
   public Object replace(Object original, Object target, Object owner) throws HibernateException {
      return original;
   }

   public Object deepCopy(Object value) throws HibernateException {
      if (value == null)
         return null;
      return new String((String) value);
   }

   public boolean isMutable() {
      return false;
   }

   public boolean equals(Object x, Object y) throws HibernateException {
      return (x == y) || (x != null && y != null && (x.equals(y)));
   }

   @Override
   public int hashCode(Object x) throws HibernateException {
      return x.hashCode();
   }
}


So any Strings in the Customer class were Trimmed as expected. But Strings in the Customer Name embedded object remained un trimmed

Any insights would be very much appreciated


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.