To use a composite PK in a query, you have to implement a UserType for your Primary Key class:
Code:
public abstract class DESCRIPTIONPK_UserType implements UserType
{
private static final int SQLTYPES[] =
{
Types.VARCHAR, // code
Types.VARCHAR, // codeDivision
Types.TIME, // updateTime
....
};
public Object assemble(Serializable aCached, Object aOwner) throws HibernateException
{
return aCached;
}
public Object deepCopy(Object aValue) throws HibernateException
{
return aValue;
}
public Serializable disassemble(Object aValue) throws HibernateException
{
return (Serializable)aValue;
}
public boolean equals(Object aX, Object aY) throws HibernateException
{
return aX==aY;
}
public int hashCode(Object aX) throws HibernateException
{
if (aX==null)
return 0;
return aX.hashCode();
}
public boolean isMutable()
{
return false;
}
public Object nullSafeGet(ResultSet aRs, String[] aNames, Object aOwner)
throws HibernateException, SQLException
{
String code = aRs.getString(aNames[0]);
String codeDivision = aRs.getString(aNames[1]);
...
return new DESCRIPTIONPK(.....);
}
public void nullSafeSet(PreparedStatement aSt, Object aValue, int aIndex)
throws HibernateException, SQLException
{
if (aValue==null)
{
aSt.setNull(aIndex, Types.VARCHAR);
}
else
{
aSt.set....
}
}
public Object replace(Object aOriginal, Object aTarget, Object aOwner)
throws HibernateException
{
return null;
}
public Class returnedClass()
{
return enumType;
}
public int[] sqlTypes()
{
return SQLTYPES;
}
}
The query looks like this:
Code:
Query q = session.createQuery("from Description d where d.id= :id");
q.setParameter("id", key, Hibernate.custom(DESCRIPTIONPK_UserType.class));
List result = q.list();