I encountered the following problem in a larger project, and isolated it to this example. I verified that it does not occur if the id generator class is native and the version property removed. Additionally, it does not appear to occur when the id and version properties are as below, and the collection is non-indexed (judging from the lack of this exception elsewhere in the larger project).
(Note: I worked around this for awhile with the published Hibernate 2.0 solution for a bidirectional list mapping
http://www.hibernate.org/193.html, but eventually ran into difficulties with that as well.)
Any solution that allows me to use assigned identifiers and access a list from the many end, would be greatly appreciated.
Hibernate version:
3.2.2.ga
Mapping documents:
Fairly basic example for bidirectional list & assigned identifiers.
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="bidilist">
<class name="Parent">
<id name="id" type="string">
<generator class="assigned"/>
</id>
<version name="concurrencyVersion" unsaved-value="null"/>
<list name="children" cascade="all-delete-orphan" lazy="true">
<key column="ParentId" not-null="true"/>
<list-index column="INDEX"/>
<one-to-many class="Child"/>
</list>
<property name="label"/>
</class>
</hibernate-mapping>
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="bidilist">
<class name="Child">
<id name="id" type="string">
<generator class="assigned"/>
</id>
<version name="concurrencyVersion" unsaved-value="null"/>
<many-to-one name="parent" class="Parent"
column="ParentId" not-null="true"
insert="false" update="false"
/>
<property name="label" column="LABEL"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Attempt to retrieve from the many end will stumble on a cryptic typecast exception. Not the case with bidirectional sets, or natively generated IDs.
Code:
Transaction tx = session.beginTransaction();
System.out.println(session.createCriteria(Child.class).add(
Example.create(child1)).list());
tx.commit();
Full stack trace of any exception that occurs:Exception in thread "main" java.lang.ClassCastException: org.hibernate.property.BackrefPropertyAccessor$1 cannot be cast to java.lang.String
at org.hibernate.type.StringType.toString(StringType.java:44)
at org.hibernate.type.NullableType.nullSafeToString(NullableType.java:93)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:140)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1698)
at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1669)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1554)
at org.hibernate.loader.Loader.doQuery(Loader.java:661)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2211)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
at org.hibernate.loader.Loader.list(Loader.java:2090)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at bidilist.Driver.main(Driver.java:46)
Name and version of the database you are using:HSQLDB 1.8.0
The generated SQL (show_sql=true):Hibernate: select this_.ID as ID0_0_, this_.CONCURRENCY_VERSION as CONCURRE2_0_0_, this_.PARENT_ID as PARENT3_0_0_, this_.LABEL as LABEL0_0_ from CHILD this_ where (this_.LABEL=? and this_.PARENT_ID=? and this_.INDEX=?)
Debug level Hibernate log excerpt:Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.htmlCode: