-->
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: Problem creating many-to-many association
PostPosted: Tue Apr 18, 2006 4:20 pm 
Newbie

Joined: Tue Apr 18, 2006 3:05 pm
Posts: 9
I am tring to do a many-to-many mapping exactly like the Category/Item mapping described in Chapter 6 of the Manning book (page 226). In my situation, a Category has a Set of Item objects and the Item class has a Set of Category objects. What makes my situation different, and where I cannot find an answer in any of the forums or google, is that my association table has 3 columns (1 surrogate key and the 2 FKs). Is there a way to map the Category and Item classes without creating an Association class? here's how it is now (which is wrong b/c the surrogate primary key is not mentioned anywhere):

Hibernate version: 3

Mapping documents:
---------------------------------
Item.hbm.xml
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="com.Item" table="ITEM" dynamic-update="true" dynamic-insert="true">

    <id name="id" column="ITEM_ID">
        <generator class="sequence">
            <param name="sequence">CATEGORY_SEQ</param>
        </generator>
    </id>

    <version name="version" type="long" column="VERSION" unsaved-value="null"/>

    <property name="name" column="NAME"/>
    <property name="description" column="DESCRIPTION"/>

    <set name="categories" table="CATEGORY_ITEM" inverse="true" lazy="false">
        <key column="ITEM_ID"/>
        <one-to-many class="com.Category"/>
    </set>

  </class>
</hibernate-mapping>

----------------------------------------------
Category.hbm.xml
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="com.Category" table="CATEGORY" dynamic-update="true" dynamic-insert="true">

    <id name="id" column="CATEGORY_ID">
        <generator class="sequence">
            <param name="sequence">CATEGORY_SEQ</param>
        </generator>
    </id>

    <version name="version" type="long" column="VERSION" unsaved-value="null"/>

    <property name="name" column="NAME"/>

    <set name="items" table="CATEGORY_ITEM" lazy="false" inverse="false">
        <key column="CATEGORY_ID" />
        <many-to-many class="com.Item"/>
    </set>
  </class>

</hibernate-mapping>

----------------------------
Category.java
Code:
package com;
import java.util.Set;
import java.util.HashSet;

public class Category
{
    private String id, name;
    private Set items = new HashSet();
    private Long version;

    //public getter and setters omitted for brevity

    public void addItem(Item item)
    {
        items.add(item);
        item.addCategory(this);
    }
}


--------------------
Item.java
Code:
package com;

import java.util.Set;
import java.util.HashSet;

public class Item
{
    private String id;
    private String name;
    private String description;
    private Set categories = new HashSet();
    private Long version;

//public getter and setters omitted for brevity

    public void addCategory(Category category)
    {
        categories.add(category);
        category.addItem(this);
    }

}


Table Schema:

CREATE TABLE CATEGORY (
CATEGORY_ID VARCHAR2(30) NOT NULL,
CATEGORY_NAME VARCHAR2(30) NULL,
VERSION NUMBER(6) NULL
);


ALTER TABLE CATEGORY
ADD ( PRIMARY KEY (CATEGORY_ID) ) ;


CREATE TABLE CATEGORY_ITEM (
CATEGORY_ITEM_ID VARCHAR2(30) NOT NULL,
CATEGORY_ID VARCHAR2(30) NULL,
ITEM_ID VARCHAR2(30) NULL
);

ALTER TABLE CATEGORY_ITEM
ADD ( PRIMARY KEY (CATEGORY_ITEM_ID) ) ;


CREATE TABLE ITEM (
ITEM_ID VARCHAR2(30) NOT NULL,
ITEM_NAME VARCHAR2(30) NULL,
ITEM_DESC VARCHAR2(30) NULL,
VERSION NUMBER(6) NULL
);


ALTER TABLE ITEM
ADD ( PRIMARY KEY (ITEM_ID) ) ;


ALTER TABLE CATEGORY_ITEM
ADD ( FOREIGN KEY (ITEM_ID)
REFERENCES ITEM ) ;


ALTER TABLE CATEGORY_ITEM
ADD ( FOREIGN KEY (CATEGORY_ID)
REFERENCES CATEGORY ) ;


Full stack trace of any exception that occurs:
The entire stack trace is: java.lang.StackOverflowError

This makes sense because obviously Hibernate has no way of knowing what the true priamry key is on the association table (i.e. CATEGORY_ITEM_ID). I know I could remove the surrogate primary key on the association table and make a constraint that the 2 foreign keys are the unique primary key, but due to politcal reasons, I cannot change the existing association table.

Database: Oracle 10G Express


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 6:35 pm 
Regular
Regular

Joined: Wed Jul 07, 2004 2:00 pm
Posts: 64
How do you set the value of the primary key of the join table? If you can figure this out, then here is a possibility:
In Category:
<idbag name="items" table="category_item">
<collection-id type="long" column="category_item_id">
<generator class="increment"/>
</collection-id>
<key column="category_id"/>
<many-to-many class="Item" column="item_id"/>
</idbag>

In Item:
<bag name="categories" inverse="true" table="category_item">
<key column="item_id"/>
<many-to-many class="Category" column="category_id"/>
</bag>

The <collection-id> will need work. Though it looks like you use sequences, even though your columns are defined as varchar. I assume then that you will use a sequence for the collection-id too.


Top
 Profile  
 
 Post subject: Re: Problem creating many-to-many association
PostPosted: Wed Apr 19, 2006 8:49 am 
Newbie

Joined: Tue Apr 18, 2006 3:05 pm
Posts: 9
Even if change the generator to "assigned" (versus sequence) and manually set my Ids...I still get the same error. I think what I'm trying to do can't be done.


Top
 Profile  
 
 Post subject: Re: Problem creating many-to-many association
PostPosted: Wed Apr 19, 2006 9:20 am 
Newbie

Joined: Tue Apr 18, 2006 3:05 pm
Posts: 9
DWright-
Thanks for your response. I tried your suggestion but still received the same error. I'm just going to create an associate class since it seems it can't be done.

Thanks for your help.
Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 3:25 pm 
Regular
Regular

Joined: Wed Jul 07, 2004 2:00 pm
Posts: 64
The sample mapping worked for me (using MySQL). I don't see how you could change the generator for the collection-id to assigned, since Hibernate doesn't give you access to a class to represent the category_item table. I originally tried the native generator, but the doc indicates that this is not supported for idbag.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 2:49 pm 
Newbie

Joined: Tue Apr 18, 2006 2:50 pm
Posts: 10
Looks to me like you are using the same sequence generater for both Category and Item. Don't you have a ITEM_SEQ?

Thanks.

-Eric Trull


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.