hibernate 3.2.4sp1
Perhaps there's a problem with your UserType implementation?? I've included everything from my test below.
Parent.hbm.xml
Code:
<class name="Parent" table="mwutk_parent">
<id name="id">
<generator class="native" />
</id>
<map name="values" table="values_map">
<key column="parent_id"/>
<map-key column="map_key" type="test.mapwithusertypekey.MapKeyUserType"/>
<element column="map_value" type="string"/>
</map>
</class>
</hibernate-mapping>
Parent.java
Code:
package test.mapwithusertypekey;
import java.util.Map;
public class Parent {
private Long id;
private Map<MapKey, String> values;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Map<MapKey, String> getValues() {
return values;
}
public void setValues(Map<MapKey, String> values) {
this.values = values;
}
}
MapKey.java
Code:
package test.mapwithusertypekey;
public class MapKey {
private int key;
public MapKey(int v) {
key = v;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
@Override
public boolean equals(Object obj) {
MapKey k = (MapKey)obj;
return k.key == key;
}
@Override
public int hashCode() {
return 29*(key+7);
}
}
MapKeyUserType.java
Code:
package test.mapwithusertypekey;
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;
public class MapKeyUserType implements UserType {
private static final int[] SQL_TYPES = { Types.INTEGER };
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class<MapKey> returnedClass() {
return MapKey.class;
}
private Object createMapKey(int keyValue) {
return new MapKey(keyValue);
}
private int getMapKeyValue(Object mapKey) {
return ((MapKey)mapKey).getKey();
}
public boolean equals(Object x, Object y) {
return x == y;
}
public Object deepCopy(Object value) {
return value;
}
public boolean isMutable() {
return false;
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
int instanceId = rs.getInt(names[0]);
return (rs.wasNull() ? null : createMapKey(instanceId));
}
public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException, SQLException {
if (value == null) {
statement.setNull(index, Types.INTEGER);
} else {
statement.setInt(index, getMapKeyValue(value));
}
}
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value;
}
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached;
}
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}
}
TestIt.java
Code:
package test.mapwithusertypekey;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.hibernate.Session;
public class TestIt extends TestCase {
public void testIt() {
Long parentId = createParent();
Session s = HibernateUtil.getSession();
Parent p = (Parent)s.load(Parent.class, parentId);
Map<MapKey, String> map = p.getValues();
MapKey mk1 = new MapKey(1);
MapKey mk2 = new MapKey(2);
assertEquals("one", map.get(mk1));
assertEquals("two", map.get(mk2));
s.close();
}
private Long createParent() {
Session s = HibernateUtil.getSession();
s.beginTransaction();
Parent p = new Parent();
MapKey mk1 = new MapKey(1);
MapKey mk2 = new MapKey(2);
Map<MapKey, String> map = new HashMap<MapKey, String>();
map.put(mk1, "one");
map.put(mk2, "two");
p.setValues(map);
Long id = (Long)s.save(p);
s.getTransaction().commit();
s.close();
return id;
}
}
HibernateUtil.java
Code:
package test.mapwithusertypekey;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory factory;
static {
Configuration config = new Configuration()
.addClass(Parent.class)
// HSQLDB Config
// .setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect")
// .setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver")
// .setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test")
// .setProperty("hibernate.connection.username", "sa")
// .setProperty("hibernate.connection.password", "")
// .setProperty("hibernate.hbm2ddl.auto", "create-drop")
// MYSQL Config
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/test")
.setProperty("hibernate.connection.username", "root")
.setProperty("hibernate.connection.password", "password")
.setProperty("hibernate.hbm2ddl.auto", "create-drop")
.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider")
.setProperty("hibernate.show_sql", "true");
HibernateUtil.setSessionFactory(config.buildSessionFactory());
}
public static synchronized Session getSession() {
if (factory == null) {
factory = new Configuration().configure().buildSessionFactory();
}
return factory.openSession();
}
public static void setSessionFactory(SessionFactory factory) {
HibernateUtil.factory = factory;
}
}