-->
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.  [ 5 posts ] 
Author Message
 Post subject: Delicate multiple parent-child relationship situation
PostPosted: Wed Oct 29, 2003 1:04 pm 
Beginner
Beginner

Joined: Wed Oct 29, 2003 11:52 am
Posts: 37
Location: Gothenburg, Sweden
Hi all. Being a beginner I've ran into a rather delicate situation I simply can't set straight. Using PostgreSQL 7.3 tables looking like this:
<code>
-- Nested categories, one parent <-> many children

CREATE TABLE Categories (
id SERIAL PRIMARY KEY,
name VARCHAR (32) NOT NULL,
parent INTEGER
);
ALTER TABLE ADD CONSTRAINT Category_p_FK FOREIGN KEY (parent) REFERENCES Category (id);
</code>

In the Java class I'd like to be able to use it both-ways:
<code>
public Category getParent() { return this.parent; }
public Set getChildren() { return this.children; }
</code>

For this, the Category.hbm.xml looks like:
<code>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="se.barnvagnsfabriken.shop.beans.Category" table="Categories">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" not-null="true" />

<!-- the parent category, or null -->
<one-to-many name="parent" class="se.barnvagnsfabriken.shop.beans.Category" />

<!-- the child categories -->
<set name="children" lazy="true">
<key column="parent" />
<one-to-many class="se.barnvagnsfabriken.shop.beans.Category" />
</set>
</code>

Somehow, I make no sense from the reference manual getting this right. To add up, the categories are filled with products (which may reside anywhere and at multiple places in the "category tree"):

<code>
CREATE TABLE Product (
id SERIAL PRIMARY KEY,
key VARCHAR (32) NOT NULL,
name VARCHAR(32) NOT NULL,
description TEXT,
price NUMBER(12, 2) NOT NULL,
vat NUMBER(12 ,2) NOT NULL
);

CREATE TABLE CPM (
id SERIAL PRIMARY KEY,
category INTEGER NOT NULL,
product INTEGER NOT NULL
);
ALTER TABLE CPM ADD CONSTRAINT CPM_c_FK FOREIGN KEY (category) REFERENCES Categories (id);
ALTER TABLE CPM ADD CONSTRAINT CPM_p_FK FOREIGN KEY (product) REFERENCES Products (id);
ALTER TABLE CPM ADD CONSTRAINT CPM_Unique UNIQUE (category, product);
</code>

In java, I'd like to skip the in-between-mapping-object, and have
<code>
In Category: public Set getProducts() { return this.products; }
In Product: public Set getCategories() { return this.categories; }
</code>

Any help would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 1:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Given the table definition as-is:
Code:
CREATE TABLE CPM (
    id             SERIAL     PRIMARY KEY,
    category   INTEGER   NOT NULL,
    product     INTEGER   NOT NULL
);


You would only be able to handle this using the <idbag> collection mapper:
Code:
    <class name="Category" table="Categories" ...>
        ...
        <idbag name="products" table="CPM" ...>
            <collection-id column="ID" type="long">
                <generator .../>
            </collection-id>
            <key column="category"/>
            <many-to-many class="Product" column="product"/>
        </idbag>
        ...
    </class>


The reason is the PK constraint definition on CPM. Without that, you could map this in Hibernate using any type of <many-to-many>:
Code:
    <class name="Category" table="Categories" ...>
        ...
        <set name="products" table="CPM" ...>
            <key column="category"/>
            <many-to-many class="Product" column="product"/>
        </set>
        ...
    </class>


Top
 Profile  
 
 Post subject: Re: Delicate multiple parent-child relationship situation
PostPosted: Wed Oct 29, 2003 1:22 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
fwendt wrote:
<!-- the parent category, or null -->
<one-to-many name="parent"


Should be
Code:
<!-- the parent category, or null -->
<many-to-one name="parent" column="..." />

many children to 1 parent


CPM must be the table of the many-to-many relationship between Category and Product

Have a look at the last example of the 5.12 section of hib ref guide

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 1:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Oh yeah, for the category hierarchy, there's even a section on the desing pattern Wiki describing how to set this exact mapping up:
http://www.hibernate.org/86.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 2:45 pm 
Beginner
Beginner

Joined: Wed Oct 29, 2003 11:52 am
Posts: 37
Location: Gothenburg, Sweden
Thanks to you all. I'll remove the PRIMARY KEY constraint as well as the entire id column in CPM. The unique constraint is enough, of course.


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