Hi,
Hibernate version: 3.1.3
I have the classes Language, Product, Description et DescriptionId :
Code:
public class Language {
private int id;
private String code;
}
public class Product {
private int id;
private String code;
private Map<Language,Description> descriptions;
}
public class Description {
private DescriptionId id;
private String name;
private String information;
}
public class DescriptionId {
private int productId;
private int languageId;
}
My mapping runs well :
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>
<class name="Language" table="language">
<id name="id" type="int">
<column name="id" />
<generator class="assigned" />
</id>
<property name="code" type="string">
<column name="code" not-null="true" />
</property>
</class>
<class name="Product" table="product">
<id name="id" type="int">
<column name="id" />
<generator class="assigned" />
</id>
<property name="code" type="string">
<column name="code" not-null="true" />
</property>
<map name="descriptions" inverse="true">
<key column="product_id"/>
<map-key-many-to-many column="language_id" class="Language" />
<one-to-many class="Description"/>
</map>
</class>
<class name="Description" table="descrption" schema="public">
<composite-id name="id" class="DescrptionId">
<key-property name="productId" type="int">
<column name="product_id" />
</key-property>
<key-property name="languageId" type="int">
<column name="language_id" />
</key-property>
</composite-id>
<many-to-one name="product" class="Product" update="false" insert="false" fetch="select">
<column name="product_id" not-null="true" />
</many-to-one>
<many-to-one name="language" class="Language" update="false" insert="false" fetch="select">
<column name="language_id" not-null="true" />
</many-to-one>
<property name="name" type="string">
<column name="name" not-null="true" />
</property>
<property name="information" type="string">
<column name="information" not-null="true" />
</property>
</class>
</hibernate-mapping>
However, I wish that the Map key is a String corresponding to the column 'code' of the 'language' table. Is it possible ?
For information, I tried the following mapping :
Code:
<map name="descriptions" inverse="true">
<key column="product_id"/>
<map-key column="language.code" type="string" />
<one-to-many class="Description"/>
</map>
In the HQL Editor, the following request runs well :
Code:
select p.descriptions.name from Product p
But, the follwing Java code throws exception :
Code:
Collection<Product> products = HbUtil.currentSession().createQuery("from Product").list();
for (Product product : products) {
System.out.println(product.getDescriptions().size());
}
This is the stacktrace :
Code:
org.hibernate.exception.GenericJDBCException: could not initialize a collection: [Product.descriptions#1]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1926)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1676)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.AbstractPersistentCollection.readIndexExistence(AbstractPersistentCollection.java:125)
at org.hibernate.collection.PersistentMap.containsKey(PersistentMap.java:109)
at ProductTest.testListProduct(ProductTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.postgresql.util.PSQLException: ERROR: schema "product0_" does not exist
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1525)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1309)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:188)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:452)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:354)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:258)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1669)
at org.hibernate.loader.Loader.doQuery(Loader.java:662)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1919)
... 25 more
Is someone has a solution or another way ?
Thank,