By the way, here is the code for the CompositeUserType (I found it in some discussion board where Christian posted about EAV):
Code:
public class AttributeValueHibernate
implements CompositeUserType {
@Override
public Class returnedClass () {
return Object.class;
}
@Override
public String[] getPropertyNames () {
return new String[] {
MetaAttributeType.LONG.name (),
MetaAttributeType.DOUBLE.name (),
MetaAttributeType.DATETIME.name (),
MetaAttributeType.STRING.name ()
};
}
@Override
public Type[] getPropertyTypes () {
return new Type[] {
Hibernate.LONG,
Hibernate.DOUBLE,
Hibernate.TIMESTAMP,
Hibernate.STRING
};
}
@Override
public Object getPropertyValue (final Object pComponent,
final int pRoperty) throws HibernateException {
switch (pRoperty) {
case 0:
return pComponent instanceof Long ? pComponent : null;
case 1:
return pComponent instanceof Double ? pComponent : null;
case 2:
return pComponent instanceof Date ? pComponent : null;
case 3:
return pComponent instanceof String ? pComponent : null;
}
throw new IllegalArgumentException ("Meta model doesn't support object of type: " + pComponent.getClass ());
}
@Override
public void setPropertyValue (final Object pComponent,
final int pRoperty,
final Object pValue) throws HibernateException {
throw new UnsupportedOperationException ();
}
@Override
public Object nullSafeGet (final ResultSet pResultSet,
final String[] pNames,
final SessionImplementor pSession,
final Object pOwner)
throws HibernateException, SQLException {
Long intValue = (Long) Hibernate.LONG.nullSafeGet (pResultSet, pNames[0], pSession, pOwner);
if (intValue != null) {
return intValue;
}
Double doubleValue = (Double) Hibernate.DOUBLE.nullSafeGet (pResultSet, pNames[1], pSession, pOwner);
if (doubleValue != null) {
return doubleValue;
}
Date datetimeValue = (Date) Hibernate.TIMESTAMP.nullSafeGet (pResultSet, pNames[2], pSession, pOwner);
if (datetimeValue != null) {
return datetimeValue;
}
return (String) Hibernate.STRING.nullSafeGet (pResultSet, pNames[3], pSession, pOwner);
}
@Override
public void nullSafeSet (final PreparedStatement pStatement,
final Object pValue,
final int pIndex,
final SessionImplementor pSession)
throws HibernateException, SQLException {
Hibernate.LONG.nullSafeSet (pStatement,
pValue instanceof Long ? pValue : null, pIndex);
Hibernate.DOUBLE.nullSafeSet (pStatement,
pValue instanceof Double ? pValue : null, pIndex + 1);
Hibernate.TIMESTAMP.nullSafeSet (pStatement,
pValue instanceof Date ? pValue : null, pIndex + 2);
Hibernate.STRING.nullSafeSet (pStatement,
pValue instanceof String ? pValue : null, pIndex + 3);
}
@Override
public boolean isMutable () {
return false;
}
@Override
public boolean equals (final Object pX,
final Object pY) throws HibernateException {
if (pX == null) {
return pY == null;
} else {
return pX.equals (pY);
}
}
@Override
public int hashCode (final Object pObject) throws HibernateException {
return pObject.hashCode ();
}
@Override
public Object replace (final Object pOriginal,
final Object pTarget,
final SessionImplementor pSession,
final Object pOwner) {
return pOriginal;
}
@Override
public Object deepCopy (final Object pValue) throws HibernateException {
return pValue;
}
@Override
public Serializable disassemble (final Object pValue,
final SessionImplementor pSession) throws HibernateException {
return (Serializable) pValue;
}
@Override
public Object assemble (final Serializable pCached,
final SessionImplementor pSession,
final Object pOwner) throws HibernateException {
return pCached;
}
}