-->
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.  [ 7 posts ] 
Author Message
 Post subject: many to many difficulty (possibly modeling issue?)
PostPosted: Wed Nov 26, 2003 2:59 am 
Newbie

Joined: Wed Nov 26, 2003 2:47 am
Posts: 4
I hate to ask another parent/child type question as I'm sure I'm missing something obvious but here goes:

I have three objects: Person, School and Address

Person has (n) Address objects
School has (n) Address objects

I had hoped to model this with two many-to-many relationships producing two mapping tables:

person_addresses and school_addresses

In this case, an address is a 'child' of both of these other classes and I'm not sure the "give the child a reference to its parent" makes sense here. Do I need to create a 'collection class' AddressCollection and give both the Person and School objects a many-to-one relationship to it? If this *is* the case, I still can't set up a bidirectional association, right?

TIA,

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 2:25 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hum, If we forget divorces, a child can't have 3 or 4 fathers.
many-to-many is not a parent-child relationship. This is a... many-to-many

Person
set table="person_address"
many-to-many Address

and

School
set table="School _address"
many-to-many Address

without bidir will work fine

_________________
Emmanuel


Top
 Profile  
 
 Post subject: many to many question
PostPosted: Wed Nov 26, 2003 3:37 pm 
Newbie

Joined: Wed Nov 26, 2003 2:47 am
Posts: 4
Hi there. Thanks for the response. That's what I thought. My problem is this (actually it's a Person/Phone, School/Phone situation): I can save the Phone object and I can save the Person object, and the Person object has the Phone object in the appropriate property (Set phoneNumbers) and the mapping table (pphones) doesn't get updated with references to the two objects..


Top
 Profile  
 
 Post subject: more information about how I'm doing this..
PostPosted: Wed Nov 26, 2003 3:47 pm 
Newbie

Joined: Wed Nov 26, 2003 2:47 am
Posts: 4
Code:

        Place p = new Place ();
        p.setName(form.getName());
       
        Phone phone = form.getPhone();
        HashSet set = new HashSet();
        set.add(phone );
        p.setPhoneNumbers(set);

        SessionFactory sf = (SessionFactory)httpServletRequest.getSession().getAttribute("session.factory");

        System.out.println(httpServletRequest.getSession());
        try
        {
            Session s = sf.openSession();
            s.saveOrUpdate(phone);
            s.saveOrUpdate(p);
            s.close();
        }
        catch(HibernateException he)
        {
            //
        }



and, from the hbm file:

Code:
    <class name="a.b.Place" table="places">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name" type="string"/>
        <set name="phoneNumbers" lazy="false" table="pphones" cascade="all">
            <key column="place_id"/>
            <many-to-many class="a.b.Phone" column="phone_id"/>
        </set>
    </class>


The problem is that the mapping table, pphones, has zero records after my saves...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 6:29 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
Session s = sf.openSession();
s.saveOrUpdate(phone);
s.saveOrUpdate(p);
s.flush();
s.close();


and be sure to be in a tx (or autocommit=true)

_________________
Emmanuel


Top
 Profile  
 
 Post subject: err....
PostPosted: Wed Nov 26, 2003 6:47 pm 
Newbie

Joined: Wed Nov 26, 2003 2:47 am
Posts: 4
see... I knew I was being an idiot... ;) Thanks for the help and happy thanksgiving.

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 7:34 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
LOL
Thanksgiving is not a French feast. I have to find something else to celebrate. Hum, let's see... Beaujolais nouveau (some times ago) ;-)

_________________
Emmanuel


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