Hibernate version: 3.3.0.SP1
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.musicbrainz.hibernate">
<class name="HibernateArtist" table="artist" lazy="true">
<comment>MusicBrainz Artist.</comment>
<id name="row" column="id">
<generator class="sequence">
<param name="sequence">artist_id_seq</param>
</generator>
</id>
<property name="id" type="org.musicbrainz.hibernate.UserUUID" column="gid"/>
<property name="name" type="string" column="name" not-null="true"/>
<property name="sortName" type="string" column="sortname" not-null="true"/>
<property name="disambiguation" type="string" column="resolution"/>
</class>
</hibernate-mapping>
UserUUID.java:Code:
package org.musicbrainz.hibernate;
import org.hibernate.*;
import org.hibernate.usertype.UserType;
import java.io.Serializable;
import java.sql.*;
import java.util.UUID;
public class UserUUID implements UserType {
private static final int[] SQL_TYPES = { Types.CHAR };
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class returnedClass() {
return UUID.class;
}
public boolean equals(Object x, Object y)
throws HibernateException {
if (x == y) {
return true;
} else if (x == null || y == null) {
return false;
} else {
return ((UUID)x).equals((UUID)y);
}
}
public int hashCode(Object x)
throws HibernateException {
return ((UUID)x).hashCode();
}
public Object nullSafeGet(ResultSet resultSet,
String[] names, Object owner)
throws HibernateException, SQLException {
String uuidString = resultSet.getString(names[0]);
if (uuidString == null)
return null;
return UUID.fromString(uuidString);
}
public void nullSafeSet(PreparedStatement statement,
Object value, int index)
throws HibernateException, SQLException {
if (value == null) {
statement.setNull(index, Types.CHAR);
} else {
statement.setString(index, ((UUID)value).toString());
}
}
public Object deepCopy(Object value) throws HibernateException {
UUID old = (UUID)value;
return new UUID(old.getMostSignificantBits(), old.getLeastSignificantBits());
}
public boolean isMutable() {
return false;
}
public Serializable disassemble(Object value)
throws HibernateException {
return (UUID)value;
}
public Object assemble(Serializable cached,
Object owner)
throws HibernateException {
return (UUID)cached;
}
public Object replace(Object original,
Object target,
Object owner)
throws HibernateException {
return original;
}
}
Code between sessionFactory.openSession() and session.close():Code:
Iterator artists = session.createQuery("from HibernateArtist").iterate();
while (artists.hasNext()) {
Artist a = (Artist)artists.next();
System.out.println(a.getId());
System.out.println(a.getName());
}
Full stack trace of any exception that occurs:Code:
Hibernate: select hibernatea0_.gid as col_0_0_ from artist hibernatea0_
2008-10-17 19:46:31,974 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: 22003
2008-10-17 19:46:32,034 ERROR [org.hibernate.util.JDBCExceptionReporter] - Bad value for type int : a18fd46d-0fd0-4551-bab0-8c610d3e9c33
Exception in thread "main" org.hibernate.exception.DataException: could not get next iterator result
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:100)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.impl.IteratorImpl.next(IteratorImpl.java:156)
at org.musicbrainz.test.HibernateTest.main(HibernateTest.java:16)
Caused by: org.postgresql.util.PSQLException: Bad value for type int : a18fd46d-0fd0-4551-bab0-8c610d3e9c33
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.toInt(AbstractJdbc2ResultSet.java:2699)
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getInt(AbstractJdbc2ResultSet.java:2016)
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getInt(AbstractJdbc2ResultSet.java:2366)
at org.hibernate.type.IntegerType.get(IntegerType.java:51)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:184)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:173)
at org.hibernate.type.ManyToOneType.hydrate(ManyToOneType.java:126)
at org.hibernate.type.EntityType.nullSafeGet(EntityType.java:227)
at org.hibernate.impl.IteratorImpl.next(IteratorImpl.java:135)
... 1 more
Name and version of the database you are using:postgresql 8.3.3-0ubuntu0.8.04
My question:I'm using Hibernate to access the MusicBrainz database in postgres (
http://musicbrainz.org/doc/DatabaseSchema) and I'm mapping it to a Java object model similar to
http://users.musicbrainz.org/~matt/pyth ... inz2/html/The relevant thing to note is that the primary key of the database (column id) is not part of this model and that get/setId should get the UUID/MBID which is in column gid in the database. I'm not keen on changing the model itself since I have both a XML Webservice and a Neo4J implementation which shouldn't suffer because of this problem.
To the point:
Code:
<property name="id" type="org.musicbrainz.hibernate.UserUUID" column="gid"/>
makes things break as above while something like
Code:
<property name="xid" type="org.musicbrainz.hibernate.UserUUID" column="gid"/>
works just fine. It seems that the name id is somehow treated specially and assumed to be an integer at some point. I would consider this a bug, can anybody say if this is supposed to be the case? I've clearly stated the type I want, so the name I use shouldn't matter...
It's not very difficult to work around this problem, but should I file a bug?