I was trying to develop a custom Spring/Hibernate Converter to automatically bind form's checkboxes tag to Hibernate's persistent entities, when I discovered this bug.
Behind the scenes, Spring use reflection to discover the element type of a collection, in order to guess the right converter for the elements in it.
Unfortunately, when one read a persistent instance via Session.load() method, the type parameter informations are lost.
You can see the complete test in the it.unipg.jira.hibernate.Test class of the attached example. To run the tests, extract the archive and run mvn -P hibernate3 clean tests to test Hibernate 3 and mvn -P hibernate4 clean tests to test Hibernate 4.
BTW, consider the JPA entity below:
@Entity
public class MyEntity
{
private Integer id;
private Set<OtherEntity> otherEntities = new HashSet<OtherEntity>();
@ManyToMany
@JoinTable
public Set<OtherEntity> getOtherEntities()
{
return otherEntities;
}
}
Consider now the following pseudo-java code:
Class myEntityClass = MyEntity.class;
Serializable myId = new Integer(1);
Object persistentInstance = session.load(myEntityClass, myId);
Class proxyClass = persistentInstance.getClass();
Method method = proxyClass.getMethod("getOtherEntities");
Type type = method.getGenericReturnType();
assert type instanceof ParameterizedType;
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
As you can see, if we load() the persistent instance, the proxy javassist-generated class do not inherit the type parameters informations, so we cannot the collection's generic type.
I mark this Issue as Minor because one can effectively use get() instead of load() in most situations.
Sorry for my poor english.
|