Hi,
I am having the same problem as the original poster but alas I am unable to change the schema.
I am trying to find an existing record on the Oracle 10g DB via the primary key which is a CHAR(10) but the id does not neccesarily have 10 characters all the time.
I have instigated a UserType to trim out the result when it is returned but this does not seem to be working either.
Here is the relavent section of FixedString UserType
Code:
public class FixedString implements UserType, Serializable {
...
/**
*
* @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) Hibernate.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);
}
}
And here is a simple hibernate mapping which recreates the problem that I am suffering.
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.yell.keywords.persistence.FixedCharTrialObject"
table="PPC_FIXED_TEST" dynamic-insert="true" dynamic-update="true"
optimistic-lock="version">
<id name="id"
type="com.yell.keywords.businessobjects.FixedString"
column="IDENTIFIER" unsaved-value="null">
<generator class="assigned"></generator>
</id>
<version name="itimestamp" type="timestamp"
column="ITIMESTAMP" />
<property name="something"
type="com.yell.keywords.businessobjects.FixedString" column="SOMETHING" />
<property name="somethingElse"
type="com.yell.keywords.businessobjects.FixedString" column="SOMETHING_ELSE"/>
</class>
</hibernate-mapping>
I have inserted a simple row into the table via the hibernate session:
Code:
public void testLoadingData() throws Exception {
FixedCharTrialObject object = new FixedCharTrialObject();
object.setId("1");
object.setSomething("Hello");
object.setSomethingElse("there");
Transaction transaction = session.beginTransaction();
session.save(object);
transaction.commit();
}