Hi,
I have a problem mapping an array of CompositesUserType's.
The setup:
A "Router" class has an array of "IpRange's". The "IpRange" class consist of two String values. To be able to map this to the Database via Hibernate, I have implemented the CompositeUserType interface called "IpRangeType".
My problem is: How do I do this mapping?
I have tried following without luck:
Code:
<array
name="ipRanges"
inverse="false"
cascade="all"
>
<key
column="routerId"
>
</key>
<index
column="arrayIndex"
/>
<element
column="value"
type="IpRangeType"
not-null="false"
unique="false"
/>
</array>
It get:
org.hibernate.MappingException: collection element mapping has wrong number of columns: router.ipRanges type: IpRangeType
I have also tried with two "column" tags, but then it also complains...
Here is the IpRangeType impl:
Code:
public class IpRangeType implements CompositeUserType
{
public String[] getPropertyNames()
{
return new String[] {"lowerLimit", "uppperLimit"};
}
public Type[] getPropertyTypes()
{
return new Type[] {Hibernate.STRING, Hibernate.STRING};
}
public Object getPropertyValue(Object component, int property) throws HibernateException
{
IpRange ipRange = (IpRange) component;
if(property == 0)
return ipRange.getLowerLimit();
else
return ipRange.getUpperLimit();
}
public void setPropertyValue(Object component, int property, Object value) throws HibernateException
{
IpRange ipRange = (IpRange) component;
if(property == 0)
ipRange.setLowerLimit((String) value);
else
ipRange.setUpperLimit((String) value);
}
public Class returnedClass()
{
return IpRange.class;
}
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); // Use the IpRange implementation of "equals"
}
public int hashCode(Object x) throws HibernateException
{
return x.hashCode();
}
/**
* Gets the values from the database
* @param
* @return
*/
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
throws HibernateException, SQLException
{
if(rs.wasNull())
return null;
String dbLowerLimit = rs.getString(names[0]);
String dbUpperLimit = rs.getString(names[1]);
return new IpRange(dbLowerLimit, dbUpperLimit);
}
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
throws HibernateException, SQLException
{
if(value == null)
{
st.setNull(index, Types.VARCHAR);
st.setNull(index+1, Types.VARCHAR);
}
else
{
IpRange ipRange = (IpRange) value;
st.setString(index, ipRange.getLowerLimit());
st.setString(index+1, ipRange.getUpperLimit());
}
}
public Object deepCopy(Object value) throws HibernateException
{
if (value == null)
return null;
else
{
IpRange ipRange = (IpRange) value;
// return copy
return new IpRange(ipRange.getLowerLimit(), ipRange.getUpperLimit());
}
}
public boolean isMutable()
{
return true;
}
public Serializable disassemble(Object value, SessionImplementor session) throws HibernateException
{
return (Serializable) deepCopy(value);
}
public Object assemble(Serializable cached, SessionImplementor session, Object owner)
throws HibernateException
{
return deepCopy(cached);
}
public Object replace(Object original, Object target, SessionImplementor session, Object owner)
throws HibernateException
{
return null;
}
}
Making it a Collection is not an option....
Hibernate version: 3.0.1
Name and version of the database you are using: MySQL 4.1.14
Please help me!!!
BR
Anders