-->
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.  [ 3 posts ] 
Author Message
 Post subject: Custom Type as ID
PostPosted: Wed Jun 08, 2005 12:14 pm 
Newbie

Joined: Wed Jun 01, 2005 11:00 am
Posts: 17
Location: UK
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>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 08, 2005 12:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Code:
if (resultSet.wasNull()) return null;
Long value = new Long(resultSet.getLong(names[0]));
return new DomainObjectID(value);


should be

Code:
Long value = new Long(resultSet.getLong(names[0]));
if (resultSet.wasNull()) return null;
return new DomainObjectID(value);


known bug in HiA book - read the errata at: http://forum.hibernate.org/viewtopic.php?t=935347

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 08, 2005 12:45 pm 
Newbie

Joined: Wed Jun 01, 2005 11:00 am
Posts: 17
Location: UK
Thank you, shall try it tomorrow am.
your a star

:-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

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.