-->
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.  [ 8 posts ] 
Author Message
 Post subject: Mapping: Have a Set return a List
PostPosted: Thu Apr 06, 2006 3:20 pm 
Beginner
Beginner

Joined: Wed Apr 05, 2006 3:19 pm
Posts: 43
Hibernate version:
version 3.1.3, March 20, 2006

Mapping documents:
The one for Unittype
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="objects.Unittype" table="unittypes">
      <id name="typeID" column="typeID">
            <generator class="native" />
        </id>
        <property name="typeName" />

      <set name="units" inverse="true" cascade="all-delete-orphan">
          <key column="unittypeID" />
          <one-to-many class="objects.Unit" />
      </set>
    </class>
</hibernate-mapping>


The one for Unit

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="objects.Unit" table="units">
      <id name="unitID" column="unitID">
            <generator class="native" />
        </id>
        <property name="unitName" />
        <property name="typeID" />

      <many-to-one name="unittype" class="objects.Unittype" column="unittypeID" not-null="true" />
    </class>
</hibernate-mapping>


How do I return a "List" of units instead of a "Set"? I tried to change set to list, but that gave an error...

Code:
<set name="units" inverse="true" cascade="all-delete-orphan">
   <key column="unittypeID" />
   <one-to-many class="objects.Unit" />
</set>

to

<list name="units" inverse="true" cascade="all-delete-orphan">
   <key column="unittypeID" />
   <one-to-many class="objects.Unit" />
</list>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 06, 2006 3:51 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
list requires an index be set

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 06, 2006 4:04 pm 
Beginner
Beginner

Joined: Wed Apr 05, 2006 3:19 pm
Posts: 43
Thanks a lot. I did a google search for "hibernate list id index" and found an example. First I tried searching for "hibernate set list" but that was just too generic to find a good result. I couldn't even find it in the documentation. (maybe I didn't look well enough?)

Anyway, right now it works. :) Maybe I'll be back for some more questions. I'm trying this for the first time and I must say: I like it!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 7:11 am 
Beginner
Beginner

Joined: Wed Apr 05, 2006 3:19 pm
Posts: 43
I had to add "update=false insert=false" to my "Unit" mapping because there was a duplicate field. I changed my mapping to scenario (1) and scenario (2). They both work.

Quote:
<?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="objects.Unit" table="units">
<id name="unitID" column="unitID">
<generator class="native" />
</id>
<property name="unitName" />
<property name="typeID" update="false" insert="false" (1) />

<many-to-one name="unittype" class="objects.Unittype" column="typeID" not-null="true" update="false" insert="false" (2) />
</class>
</hibernate-mapping>


The many-to-one mapping is because Unit has a parent -> Unittype. I wonder where the "update=fal....." should go: place (1) or place (2) (because they both work).

I also wonder if the property "typeID" is still needed, because it is already included in its parent: the object Unittype. If that's the case, should I still need to include the "update=....." piece?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 9:24 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
place 2

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 08, 2006 2:00 pm 
Beginner
Beginner

Joined: Wed Apr 05, 2006 3:19 pm
Posts: 43
I'm getting a nullpointer if I try to do this:

Code:
Unittype ut = .......findbyid(some-correct-id);
@SuppressWarnings("unchecked")
List<Unit> list = ut.getUnits();

for(Unit unit : list){
   do something with unit (e.g. unit.getName() ) <-- NullpointerException
}


I know that the units don't get initialized in "ut.getUnits()", but my question is: how do you say to hibernate: "initialize these objects now!"?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 08, 2006 4:31 pm 
Beginner
Beginner

Joined: Wed Apr 05, 2006 3:19 pm
Posts: 43
Quote:
I know that the units don't get initialized in "ut.getUnits()", but my question is: how do you say to hibernate: "initialize these objects now!"?


I want to correct myself: they DO get initialized when I do this:

Code:
List<..... list = (Lis....)ut.getList()

for(Unit unit : list){
  list.get(1).getName(); <-- does not throw any Exception
  unit.getName(); <-- Throws NullPointerException
}


Is it possible for a developer to look into this?
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 09, 2006 6:47 am 
Beginner
Beginner

Joined: Wed Apr 05, 2006 3:19 pm
Posts: 43
I tried a little debugging... I did the following:

Code:
int i = 0;
for(Unit unit : list){
   System.err.println("Listnr: "+ i++);
   if(unit != null) {
      System.err.println("Unitnr: "+ unit.getUnitID() +" - Unitname: "+ unit.getUnitName());   
   }
}


That gave me the following result:
Code:
Listnr: 0
Listnr: 1
Unitnr: 1 - Unitname: Unit 1
Listnr: 2
Listnr: 3
Listnr: 4
Listnr: 5
Listnr: 6
Listnr: 7
Unitnr: 7 - Unitname: Unit 2
Listnr: 8
Listnr: 9
Listnr: 10
Unitnr: 10 - Unitname: Unit 3
Listnr: 11
Unitnr: 11 - Unitname: Unit 4
Listnr: 12
Unitnr: 12 - Unitname: Unit 5


So basically, it means that the list gets filled with null values like this:

Code:
list(null,unit1,null,null,....)


because the database says: 1-unit1, 7-unit2, ... but I don't want that. I only want the units to be in the list, not null values. So like this:

Code:
list(unit1,unit2,...,unit12)


How do I do that?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.