-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Champ de composants comme index de map
PostPosted: Fri Sep 01, 2006 11:59 am 
Newbie

Joined: Wed Jul 19, 2006 4:05 am
Posts: 13
Salut,

Hibernate version: 3.1.3

J'ai des classes Langue, Produit, Description et DescriptionId :
Code:
public class Langue {
  private int id;
  private String code;
}
public class Produit {
  private int id;
  private String code;
  private Map<Langue,Description> descriptions;
}
public class Description {
  private DescriptionId id;
  private String nom;
  private String information;
}
public class DescriptionId {
  private int produitId;
  private int langueId;
}


Mon mapping fonctionne très bien :
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="Langue" table="langue">
      <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="Produit" table="produit">
      <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="produit_id"/>
         <map-key-many-to-many column="langue_id" class="Langue" />
         <one-to-many class="Description"/>
      </map>
   </class>
   <class name="Description" table="descrption" schema="public">
      <composite-id name="id" class="DescrptionId">
         <key-property name="produitId" type="int">
            <column name="produit_id" />
         </key-property>
         <key-property name="langueId" type="int">
            <column name="langue_id" />
         </key-property>
      </composite-id>
      <many-to-one name="produit" class="Produit" update="false" insert="false" fetch="select">
         <column name="produit_id" not-null="true" />
      </many-to-one>
      <many-to-one name="langue" class="Langue" update="false" insert="false" fetch="select">
         <column name="langue_id" not-null="true" />
      </many-to-one>
      <property name="nom" type="string">
         <column name="nom" not-null="true" />
      </property>
      <property name="information" type="string">
         <column name="information" not-null="true" />
      </property>
   </class>
</hibernate-mapping>


Cependant, je souhaiterai que la clé de ma Map soit un String correspondant un champ code de la table langue. Celà est-il possible ?
Merci,


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 01, 2006 12:23 pm 
Newbie

Joined: Wed Jul 19, 2006 4:05 am
Posts: 13
Pour information j'ai essayé le mapping suivant :
Code:
      <map name="descriptions" inverse="true">
         <key column="produit_id"/>
         <map-key column="langue.code" type="string" />
         <one-to-many class="Description"/>
      </map>


Dans HQL Editor, la requête suivante fonctionne correctement :
Code:
select p.descriptions.nom from Produit p


Par contre, le code Java suivant provoque une exception :
Code:
Collection<Produit> produits = HbUtil.currentSession().createQuery("from Produit").list();
for (Produit produit : produits) {
   System.out.println(produit.getDescriptions().size());
}


Voici la pile d'exception :
Code:
org.hibernate.exception.GenericJDBCException: could not initialize a collection: [Produit.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 ProduitTest.testListProduit(ProduitTest.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 "produit0_" 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



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.