Hi,
I'm using Hibernate 3.1rc2.
In my application i'm using a wrapper class for the primary key of every entity, e.g.
Code:
@Entity
public class ProductEJB implements Serializable {
...
@Column(name = "id")
@Id( generate = GeneratorType.AUTO)
@Type( type="EntityId")
public ProductId getId()
Class ProductId is a simple type safe wrapper for Integer and I want it to be generated natively by the database. I created apropriate custom Hibernate type, but it didn't work because of an assert in org.hibernate.id.IdentifierGeneratorFactory: ( getReturnedClass() in my case returned ProductId)
Code:
public static Serializable get(ResultSet rs, Type type)
throws SQLException, IdentifierGenerationException {
Class clazz = type.getReturnedClass();
if ( clazz==Long.class ) {
return new Long( rs.getLong(1) );
}
...
else {
throw new IdentifierGenerationException("this id generator generates long, integer, short or string");
}
}
I also tried to extend post insert generator class from Hibernate, but in the end the same static member from IdentifierGeneratorFactory was called.
The only solution I found was to patch Hibernate source, so I added to org.hibernate.type:
Code:
public interface AutoGeneratedIdType {
public Serializable getGeneratedId( ResultSet rs) throws SQLException;
}
and changed IdentifierGeneratorFactory:
Code:
...
else if ( type instanceof AutoGeneratedIdType) {
return ((AutoGeneratedIdType)type).getGeneratedId( rs);
}
...
Is there a better solution to my problem?
cheers, Pawel