-
1. Re: Mapping String attribute in EntityBean to CHAR(size) in
andydale Dec 14, 2006 5:10 AM (in response to henning1)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 Dec 14, 2006 9:58 AM (in response to 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 Mar 25, 2007 9:55 PM (in response to henning1)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 Mar 26, 2007 6:00 AM (in response to henning1)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 Mar 26, 2007 10:41 AM (in response to henning1)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 Mar 26, 2007 10:50 AM (in response to henning1)Ok, this was an easy one @Column(length = 100000) did the trick.
Thanks!
Chris