views:

173

answers:

1

I use Hibernate3 and Hibernate Tools 3.2.4 to generate hbm.xml and java files and I want to use List instead of HashSet(...). I've tried to modify the hbm.xml files, putting list instead of set. Is there any way to specify to hibernate tools that I want to generate automatically a list not a HashSet? This is an exemple:

Java class

public class Test implements java.io.Serializable {  

     private Long testId;  
     private Course course;  
     private String testName;  
     private Set<Question> questions = new HashSet<Question>( 0 );  
}

Test.hbm.xml:

<set name="questions" inverse="true" lazy="true" table="questions" fetch="select">  
  <key>  
    <column name="test_id" not-null="true" />  
  </key>  
  <one-to-many class="com.app.objects.Question" />
  ...
</set>

I thought that I could find a clue in the "reveng.xml" file, but I failed.

A: 

Well, instead of using a <set> in your hbm.xml, did you try to use a <list>? Note that you'll need an index column in the collection table to use <list> (since List is an ordered collection).

Try something like that:

<list name="questions" 
      inverse="true" 
      lazy="true" 
      table="questions" 
      fetch="select">  
  <key column name="test_id" not-null="true" />  
  <list-index column="sortOrder"/>
  <one-to-many class="com.app.objects.Question" />
</list>

Refer to the section 6.2. Collection mappings in the documentation for full details. Pay a special attention to the section 6.2.3. Indexed collections.

Pascal Thivent