-->
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.  [ 3 posts ] 
Author Message
 Post subject: Collections in Components
PostPosted: Mon May 16, 2005 9:02 am 
Newbie

Joined: Fri Mar 11, 2005 11:39 am
Posts: 5
Location: London
Hibernate version: 3.0.3

Hi all,

Having a bit of a problem understanding collections within components, here is my problem, I'd like two of my classes (Company & User) to have a 'Contact' component, the Contact component then has a collection of Address(es)
ie:
Company - has a Contact component - which contains a set of Addresses
User - has a Contact component - which contains a set of Addresses

Bit stumped, I've had a go but it didn't work and the docs didn't shed much light on collections in components.

Appreciate any help,

Adam.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 16, 2005 1:09 pm 
Newbie

Joined: Fri Mar 11, 2005 11:39 am
Posts: 5
Location: London
Hibernate version: 3.0.3

Mapping documents:
Code:
<class name="Company">
   <id name="id" column="id" type="java.lang.Long">
      <generator class="native"/>
   </id>

   <component name="contact" class="Contact">

   <set name="addresses" lazy="false" inverse="true" cascade="all-delete-orphan" sort="unsorted">

   <key column="companyid"/>

   <one-to-many class="Address"/>

   </set>

   [...]
</class>

<class name="User">
   <id name="id" column="id" type="java.lang.Long">
      <generator class="native"/>
   </id>

   <component name="contact" class="Contact">

   <set name="addresses" lazy="false" inverse="true" cascade="all-delete-orphan" sort="unsorted">

   <key column="userid"/>

   <one-to-many class="Address"/>

   </set>

   [...]
</class>

Code between sessionFactory.openSession() and session.close():
Code:
Company c = new Company();
        c.setCompanyType("Golf");
       
       
       
        Contact con = new Contact();
       
        con.setOrganisation("Test Company");
        con.setCompany(true);
       
        companyManager.saveCompany(c);
       
        Address a = new Address();
        a.setPostcode("SL6 0HE");
        a.setLabel("Home");
       
        con.setAddresses(new HashSet());
       
        con.getAddresses().add(a);
       
        c.setContact(con);
       
        companyManager.saveCompany(c);


Full stack trace of any exception that occurs:

Name and version of the database you are using:MySQL 4.1.10

The generated SQL (show_sql=true):
Code:
Hibernate: insert into Company (companyType, department, dob, firstName, company, jobTitle, lastName, middleName, organisation, suffix, title, modificationDate, creationDate) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Address (address1, address2, address3, city, country, county, label, postcode) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update Company set companyType=?, department=?, dob=?, firstName=?, company=?, jobTitle=?, lastName=?, middleName=?, organisation=?, suffix=?, title=?, modificationDate=?, creationDate=? where id=?


Debug level Hibernate log excerpt:
Code:
DEBUG - CompanyDAOHibernate.saveCompany(36) | companyId set to: 4
DEBUG - CompanyDAOHibernate.saveCompany(36) | companyId set to: 4


.. a bit more info.

So this is what I have at the moment but when I save the company the address is stored but the companyid fk in Address is null, what do I need to add to the mapping the associate the Company with the Address?

Many thanks,
Adam


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 16, 2005 8:24 pm 
Newbie

Joined: Fri Mar 11, 2005 11:39 am
Posts: 5
Location: London
Solved: Removing the inverse="true" attribute from the "addresses" set declaration solved the problem, and now it works as expected, well so far anyway.


Fixed Mapping:
Code:
<class name="Company">
   <id name="id" column="id" type="java.lang.Long">
      <generator class="native"/>
   </id>

   <component name="contact" class="Contact">

   <set name="addresses" lazy="false" cascade="all-delete-orphan" sort="unsorted">

   <key column="companyid"/>

   <one-to-many class="Address"/>

   </set>

   [...]
</class>

<class name="User">
   <id name="id" column="id" type="java.lang.Long">
      <generator class="native"/>
   </id>

   <component name="contact" class="Contact">

   <set name="addresses" lazy="false" cascade="all-delete-orphan" sort="unsorted">

   <key column="userid"/>

   <one-to-many class="Address"/>

   </set>

   [...]
</class>


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