-->
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.  [ 6 posts ] 
Author Message
 Post subject: set mapping with composite-id
PostPosted: Thu Jun 23, 2005 1:14 am 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
I'm a newbie at hibernate, so if I've done something obvious and stupid, I apologize. I don't understand what I've done wrong here. I have a a Person class which is associated with 0..* addresses. Each Address has a key composed of the associated person (a number, in the database) and a "tag" (basically a name for the address). Everything seems to work fine until I attempt to iterate through the returned address collection. The collection (which indicated its size was 1 immediately before this) returns a NoSuchElementException.

Can anyone help?

Hibernate version: 305

Mapping documents:
Code:
<hibernate-mapping package="net.vickerscraven.learning.hibernate">
        <class name="Person" table="person">
                <id name="id" column="per_id" unsaved-value="null">
                        <generator class="native"/>
                </id>
                <property name="firstName" column="per_first_name" not-null="true"/>
                <property name="lastName" column="per_last_name" not-null="true"/>
                <property name="birthDate" column="per_birth_date" not-null="true"/>
                <property name="weightInKilograms" column="per_weight_kg" not-null="true"/>
                <property name="heightInMeters" column="per_height_m" not-null="true"/>
            <set name="addresses" lazy="false" inverse="true">
               <key column="per_id"/>
               <one-to-many class="Address"/>
            </set>
        </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="net.vickerscraven.learning.hibernate">
    <class name="Address" table="address">
       <composite-id>
          <key-many-to-one name="owner" class="Person" column="per_id"/>
          <key-property name="tag" column="tag"/>
       </composite-id>
       <property name="address" column="address" not-null="true"/>
       <property name="address2" column="address2" not-null="false"/>
       <property name="city" column="city" not-null="true"/>
       <property name="state" column="state" not-null="true"/>
       <property name="zip" column="zip"/>
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
         Query q = session.createQuery("from Person p where p.firstName = 'Aaron'");
         List results = q.list();
         
         System.out.println(results.size() + " result(s) found.");
         
         Iterator iter = results.iterator();
         
         while (iter.hasNext()) {
            person = (Person)iter.next();
            
            System.out.println(person.getFirstName() + " " + person.getLastName());

            addresses = person.getAddresses();
            
            addressIter = addresses.iterator();
            System.out.println(person.getFirstName() + " has " + addresses.size() + " associated addresses:");
            while (addressIter.hasNext()) {
               address = (Address)iter.next();
               
               System.out.println(address.getTag());
            }
         }



Full stack trace of any exception that occurs:
Code:
java.util.NoSuchElementException
   at java.util.AbstractList$Itr.next(Unknown Source)
   at net.vickerscraven.learning.hibernate.TestHibernate.main(TestHibernate.java:56)


Name and version of the database you are using:
PostgreSQL 8.?

The generated SQL (show_sql=true):
select person0_.per_id as per1_, person0_.per_first_name as per2_1_, person0_.per_last_name as per3_1_, person0_.per_birth_date as per4_1_, person0_.per_weight_kg as per5_1_, person0_.per_height_m as per6_1_ from person person0_ where person0_.per_first_name='Aaron'

followed by

select addresses0_.per_id as per1_1_, addresses0_.tag as tag1_, addresses0_.per_id as per1_0_, addresses0_.tag as tag0_, addresses0_.address as address0_0_, addresses0_.address2 as address4_0_0_, addresses0_.city as city0_0_, addresses0_.state as state0_0_, addresses0_.zip as zip0_0_ from address addresses0_ where addresses0_.per_id=?

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 4:59 am 
Newbie

Joined: Mon Jun 20, 2005 6:20 am
Posts: 7
Hi,

Your code looks like this:

while (addressIter.hasNext()) {
address = (Address)iter.next();
System.out.println(address.getTag());
}

it should look like this:

while (addressIter.hasNext()) {
address = (Address)addressIter.next();
System.out.println(address.getTag());
}


Top
 Profile  
 
 Post subject: that was embarassing...
PostPosted: Thu Jun 23, 2005 8:16 am 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
*smacks head*

That was just plain embarassing... reminds me of taking math in college. Always the stupid mistakes...

Sorry for wasting everybody's time with a mistake like this.


Top
 Profile  
 
 Post subject: critique of mapping
PostPosted: Thu Jun 23, 2005 8:24 am 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
While I've got your attention, could I ask that someone critique the mappings I've written? Assume a J2EE environment with an EJB session facade acting as the interface to the model. Hibernate would be used as the persistence framework for the SLSBs, which would tend to retrieve objects from the database, disconnect them (make them transient to use hibernate terminology), and return them for display on the view (using Struts as the controller). Updates would be performed in reverse, with the SLSBs looking the objects back up (not reconnecting the objects looked up before, as these objects would have passed out of scope at the end of the previous request), and updating them (within a session, of course).


Top
 Profile  
 
 Post subject: Re: set mapping with composite-id
PostPosted: Wed Jul 06, 2005 3:57 am 
Newbie

Joined: Wed Jul 06, 2005 3:00 am
Posts: 1
Just out of curiousity, how do you actually populate the tables.
We have a similar table structure, but have some problems with having hibernate populating the parent id within the child table.
How have you solved this ?

Henrik


[quote="eagle79"]
[b]Hibernate version:[/b] 305

[b]Mapping documents:[/b]
[code]
<hibernate-mapping package="net.vickerscraven.learning.hibernate">
<class name="Person" table="person">
<id name="id" column="per_id" unsaved-value="null">
<generator class="native"/>
</id>
<property name="firstName" column="per_first_name" not-null="true"/>
<property name="lastName" column="per_last_name" not-null="true"/>
<property name="birthDate" column="per_birth_date" not-null="true"/>
<property name="weightInKilograms" column="per_weight_kg" not-null="true"/>
<property name="heightInMeters" column="per_height_m" not-null="true"/>
<set name="addresses" lazy="false" inverse="true">
<key column="per_id"/>
<one-to-many class="Address"/>
</set>
</class>
</hibernate-mapping>
[/code]

[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="net.vickerscraven.learning.hibernate">
<class name="Address" table="address">
<composite-id>
<key-many-to-one name="owner" class="Person" column="per_id"/>
<key-property name="tag" column="tag"/>
</composite-id>
<property name="address" column="address" not-null="true"/>
<property name="address2" column="address2" not-null="false"/>
<property name="city" column="city" not-null="true"/>
<property name="state" column="state" not-null="true"/>
<property name="zip" column="zip"/>
</class>
</hibernate-mapping>
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 06, 2005 11:53 am 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
Can you post your mappings/code/other information?

I no longer have this code in this state. I was (and still am) learning hibernate when I wrote these, so in the process I may have changed what things look like from what you see above. In fact, the code above may have had the same problem you did, as my first tests were read-only tests :-). I've since expanded things a bit, and also tested a number of different CRUD scenarios.

I'll try to help if I can.


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