-->
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.  [ 2 posts ] 
Author Message
 Post subject: One-to-many list mapping question
PostPosted: Mon May 02, 2005 3:57 am 
Newbie

Joined: Mon May 02, 2005 3:32 am
Posts: 1
Hi,

I'm rather new to Hibernate so please bare with me.

I have a User class with a collection of Group objects in it. In the database I have a USER table, a GROUP table and a USER_GROUP table defining which groups are associated with which user. This all worked using OJB, however I don't understand how to configure the <list> mapping for Hibernate. I've placed below example code, db schemas and the mappings, can somebody tell me how I should write this <list> mapping please? My version below just doesn't work!


The Java
Code:
public class User
{
    private Collection groups;
...
}

public class Group
{
...
}


The DB Schema
Code:
CREATE TABLE USER(
ID     INT NOT NULL,
...
);

CREATE TABLE GROUP(
ID     INT NOT NULL,
...
);

CREATE TABLE USER_GROUP(
    USER_ID     INT(11),
    GROUP_ID    INT(11)
);


The Mappings

Code:
<class name="Group" table="GROUP">
    <id name="id"
        type="integer"
        column="ID">
        <generator class="assigned"/>
    </id>
...
</class>

<class name="User" table="USER"
    >
    <id name="id"
        type="integer"
        unsaved-value="null"
        column="ID">
        <generator class="assigned"/>
    </id>

...
   <list name="groups"
        table="USER_GROUP"
        inverse="true"
        lazy="true">
        <key column="USER_ID"/>
        <one-to-many
            column="GROUP_ID"
            class="Group"
        />
    </list>
</class>

_________________
Many thanks,

Nick West.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 02, 2005 3:59 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
hi,
change
Code:
   <list name="groups"
        table="USER_GROUP"
        inverse="true"
        lazy="true">
        <key column="USER_ID"/>
        <one-to-many
            column="GROUP_ID"
            class="Group"
        />
    </list>

to
Code:
   <bag name="groups"
        table="USER_GROUP"
        inverse="true"
        lazy="true">
        <key column="USER_ID"/>
        <one-to-many
            column="GROUP_ID"
            class="Group"
        />
    </bag>


if you don't have an index column.
(no change in your java code)

You should read the reference guide about different collection mapping.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


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