Hello,
I sanyone aware with a problem of using a custom type as an id in the mapping. We have a legacy object id class that wraps the Long in all our POJOs.
What follows is my user type. When I use a custom finder to do a lookup it returns a list but the list just contains null references. Any ideas? It works if I use a a Long as the ID. Have attached my UserType and my hbm.
Many thanks for any help.
Cheers, Ben
package com.du.ezmarket.integration.marketbasket;
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;
import com.du.ezmarket.business.DomainObjectID;
/**
* @author hollandb
*/
public class DomainObjectType implements UserType {
private static final int[] SQL_TYPES = {Types.NUMERIC};
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#sqlTypes()
*/
public int[] sqlTypes() {
return SQL_TYPES;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#returnedClass()
*/
public Class returnedClass() {
return DomainObjectID.class;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
*/
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) return true;
if (x == null || y == null) return false;
return x.equals(y);
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/
public int hashCode(Object arg0) throws HibernateException {
return arg0.hashCode();
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet(ResultSet resultSet, String[] names, Object arg2) throws HibernateException, SQLException {
if (resultSet.wasNull()) return null;
Long value = new Long(resultSet.getLong(names[0]));
return new DomainObjectID(value);
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
*/
public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException, SQLException {
if (value == null) {
statement.setNull(index, Types.NUMERIC);
} else {
DomainObjectID id = (DomainObjectID)value;
// The convert() method isn't shown in our examples
statement.setLong(index, id.longValue().longValue());
}
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
*/
public Object deepCopy(Object arg0) throws HibernateException {
return arg0;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#isMutable()
*/
public boolean isMutable() {
return false;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
*/
public Serializable disassemble(Object arg0) throws HibernateException {
return (Serializable) arg0;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
*/
public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
return arg0;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
*/
public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
return arg0;
}
}
<hibernate-mapping>
<class name="com.du.ezmarket.business.marketbasket.MarketBasketImpl" table="tblmarketbasket">
<id name="id" column="id" type="com.du.ezmarket.integration.marketbasket.DomainObjectType">
</id>
<property name="name" />
<property name="description"/>
</class>
<sql-query name="findBasketsByHeaderId">
<return alias="marketbasket" class="com.du.ezmarket.business.marketbasket.MarketBasketImpl"/>
SELECT
tblmarketbasket.ID AS {marketbasket.id},
tblmarketbasket.NAME AS {marketbasket.name},
tblmarketbasket.description AS {marketbasket.description}
FROM tblmarketbasket WHERE tblmarketbasket.FK_AUCTIONHEADERID=10001
</sql-query>
</hibernate-mapping>
|