6 Replies Latest reply on Mar 26, 2007 10:50 AM by chris1

    Mapping String attribute in EntityBean to CHAR(size) in DB

    henning1

      I have a composite PK class that consists of {int, int, String, String}.
      Both attributes of type String are mapped to CHAR(size) instead of VARCHAR2(size) in an Oracle DB.

      My Problem:
      The EntityManager's
      find(Class entityClass, Object primaryKey)
      method returns null instead of an EntityBean.

      It all works if I handcode my EJBQL and insert the two Strings with trailing white spaces to match the size of the CHAR-Colums in the DB.

      This is somewhat strange because a SQL select statement works fine without supplying trailing white spaces in the where clause.

      Any suggestions other than changing to VARCHAR2??

        • 1. Re: Mapping String attribute in EntityBean to CHAR(size) in
          andydale

          Hi,

          Could you not just annotate the getter/variable with something like so:

          @Column(columnDefinition="VARCHAR2(<size>)")


          Cheers

          Andy

          • 2. Re: Mapping String attribute in EntityBean to CHAR(size) in
            henning1

            The 'columDefinition' just helps the PersistenceProvider to set up the right DDL for the Table when you set it up to create the Tables at Deployment
            :-(
            But thanks

            • 3. Re: Mapping String attribute in EntityBean to CHAR(size) in
              chris1

              I have the same problem. There is a char(3) column in my mysql database I cannot change. What's the right java type I can map this to in my @Entity?

              char[], Character[] etc/ gives me an "Varchar expected" exception.

              Thanks,

              Chris

              • 4. Re: Mapping String attribute in EntityBean to CHAR(size) in
                fheldt

                I fixed this behavior with a simple UserType. Just annotate

                import org.hibernate.annotations.Type;
                ...
                @Column(name = "XYZ")
                @Type(type = "usertype.TrimmedStringType")
                private String xyz;
                


                The TrimmedStringType source is like this:
                package 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 TrimmedStringType implements UserType {
                
                 private static final int[] SQL_TYPES = { Types.CHAR };
                
                 public int[] sqlTypes() {
                 return SQL_TYPES;
                 }
                
                 public Class returnedClass() {
                 return String.class;
                 }
                
                 public Object assemble(Serializable cached, Object owner) throws HibernateException {
                 return cached;
                 }
                
                 public Object deepCopy(Object obj) throws HibernateException {
                 if (obj == null) {
                 return null;
                 }
                 return new String((String) obj);
                 }
                
                 public Serializable disassemble(Object value) throws HibernateException {
                 return (Serializable) value;
                 }
                
                 public boolean equals(Object x, Object y) throws HibernateException {
                 return (x == y) || (x != null && y != null && (x.equals(y)));
                 }
                
                 public int hashCode(Object x) throws HibernateException {
                 return x.hashCode();
                 }
                
                 public boolean isMutable() {
                 return false;
                 }
                
                 public Object nullSafeGet(ResultSet rs, String[] names, Object obj) throws HibernateException, SQLException {
                 String val = rs.getString(names[0]);
                 return val != null ? val.trim() : null;
                 }
                
                 public void nullSafeSet(PreparedStatement pstmt, Object obj, int i) throws HibernateException, SQLException {
                 pstmt.setString(i, (String)obj);
                 }
                
                 public Object replace(Object original, Object target, Object owner) throws HibernateException {
                 return original;
                 }
                }
                


                I hope this helps.

                • 5. Re: Mapping String attribute in EntityBean to CHAR(size) in
                  chris1

                  Thanks a lot! That worked for my char-datatype.

                  Do you have an idea how to deal with MEDIUMTEXT as (mysql) database type? What's the best java.sql.Type I can use for it?

                  Thanks,

                  Chris

                  • 6. Re: Mapping String attribute in EntityBean to CHAR(size) in
                    chris1

                    Ok, this was an easy one @Column(length = 100000) did the trick.

                    Thanks!

                    Chris