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: Modeling many to many relationship as a bidirectional list
PostPosted: Fri Aug 27, 2004 7:02 pm 
Newbie

Joined: Fri Aug 27, 2004 6:53 pm
Posts: 4
Hibernate version: 2.1.6

A ==> AxB <== B

1. A can be related to many B's;
2. B can be related to many A's;
3. A needs to have indexed collection (list) of B's.
4. B need not have indexed collection of A's.
5. The combination of A_ID, B_ID, and the A_B_IDX in the AxB table needs to be unique.

How do I build the above relationship? I have tried a lot of ways but just not able to capture the above relationships using hibernate.

Thanks a lot,
Irfan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 8:59 pm 
Newbie

Joined: Fri Aug 27, 2004 6:53 pm
Posts: 4
Attached is the javabean code and the generated schema. Please look at how in the generated schema, the primary key is the combination of just the BAR_ID and BAR_IDX.

How do I get the primary key to be FOO_ID, BAR_ID and BAR_IDX?

I may be totally off track here in defining the relationships. Please let me know if there is a better way of modelling the two entities?

Thanks a lot,
Irfan

Foo2:
----------------------------------------------------------------------------
package com.samples.foobar;

import com.samples.core.impl.AbstractCoreObject;

import java.util.ArrayList;
import java.util.List;


/**
* Foo2.
*
* @hibernate.class table="FOO2" dynamic_update="true"
*/
public class Foo2 extends AbstractCoreObject {
/** name */
private String name_;

/** Id */
private String id_;

/** uri */
private String uri_;

/** list of bars */
private List barList_ = new ArrayList(10);

/**
* {@inheritDoc}
*/
public boolean equals(Object obj) {
boolean isEqual = (this == obj);

if (!isEqual && (obj instanceof Foo2)) {
Foo2 that = (Foo2) obj;
isEqual = this.uri_.equals(that.uri_);
}

return isEqual;
}

/**
* {@inheritDoc}
*/
public int hashCode() {
return this.uri_.hashCode();
}

/**
* returns the list of bars.
*
* @return the list of bars.
*
* @hibernate.list table="FOO_BAR2" lazy="true" cascade="all"
* @hibernate.collection-key column="FOO_ID"
* @hibernate.collection-index column="BAR_IDX"
* @hibernate.collection-many-to-many column="BAR_ID" class="com.samples.foobar.Bar2"
*/
public List getBarList() {
return this.barList_;
}

/**
* returns the id.
*
* @return the id.
*
* @hibernate.id column="ID" not-null="true" unsaved-value="null" type="string" length="32" generator-class="uuid.hex"
*/
public String getId() {
return this.id_;
}

/**
* returns the name.
*
* @return the name.
*
* @hibernate.property column="NAME" not-null="true" unsaved-value="null" type="string" length="255"
*/
public String getName() {
return this.name_;
}

/**
* sets the list of bars.
*
* @param barList The bar List to set.
*/
public void setBarList(List barList) {
this.barList_ = barList;
}

/**
* sets the id.
*
* @param id The id to set.
*/
public void setId(String id) {
this.id_ = id;
}

/**
* sets the name.
*
* @param name The name to set.
*/
public void setName(String name) {
this.name_ = name;
}

/**
* adds the bar.
*
* @param bar bar
*/
public void addBar(Bar2 bar) {
this.barList_.add(bar);
}

/**
* removes the bar.
*
* @param bar bar
*/
public void removeBar(Bar2 bar) {
this.barList_.remove(bar);
}

/**
* returns the uri.
*
* @return the uri.
*
* @hibernate.property column="URI" not-null="true" unsaved-value="null" type="string" length="255"
*/
public String getUri() {
return this.uri_;
}

/**
* sets the uri.
*
* @param uri The uri to set.
*/
public void setUri(String uri) {
this.uri_ = uri;
}
}

Bar2:
----------------------------------------------------------------------------
package com.samples.foobar;

import com.samples.core.impl.AbstractCoreObject;

import java.util.ArrayList;
import java.util.List;


/**
* Bar2.
*
* @hibernate.class table="BAR2" dynamic_update="true"
*/
public class Bar2 extends AbstractCoreObject {
/** name */
private String name_;

/** Id */
private String id_;

/** uri */
private String uri_;

/** list of foos */
private List fooList_ = new ArrayList(10);

/**
* {@inheritDoc}
*/
public boolean equals(Object obj) {
boolean isEqual = (this == obj);

if (!isEqual && (obj instanceof Bar2)) {
Bar2 that = (Bar2) obj;
isEqual = this.uri_.equals(that.uri_);
}

return isEqual;
}

/**
* {@inheritDoc}
*/
public int hashCode() {
return this.uri_.hashCode();
}

/**
* returns the list of foos.
*
* @return the list of foos.
*
* @hibernate.list table="FOO_BAR2" lazy="true" cascade="all" inverse="true"
* @hibernate.collection-key column="BAR_ID"
* @hibernate.collection-index column="BAR_IDX"
* @hibernate.collection-many-to-many column="FOO_ID" class="com.samples.foobar.Foo2"
*/
public List getFooList() {
return this.fooList_;
}

/**
* returns the id.
*
* @return the id.
*
* @hibernate.id column="ID" not-null="true" unsaved-value="null" type="string" length="32" generator-class="uuid.hex"
*/
public String getId() {
return this.id_;
}

/**
* returns the name.
*
* @return the name.
*
* @hibernate.property column="NAME" not-null="true" unsaved-value="null" type="string" length="255"
*/
public String getName() {
return this.name_;
}

/**
* sets the list of foos.
*
* @param fooList The foo List to set.
*/
public void setFooList(List fooList) {
this.fooList_ = fooList;
}

/**
* sets the id.
*
* @param id The id to set.
*/
public void setId(String id) {
this.id_ = id;
}

/**
* sets the name.
*
* @param name The name to set.
*/
public void setName(String name) {
this.name_ = name;
}

/**
* adds the foo.
*
* @param foo foo
*/
public void addFoo(Bar2 foo) {
this.fooList_.add(foo);
}

/**
* removes the foo.
*
* @param foo foo
*/
public void removeFoo(Bar2 foo) {
this.fooList_.remove(foo);
}

/**
* returns the uri.
*
* @return the uri.
*
* @hibernate.property column="URI" not-null="true" unsaved-value="null" type="string" length="255"
*/
public String getUri() {
return this.uri_;
}

/**
* sets the uri.
*
* @param uri The uri to set.
*/
public void setUri(String uri) {
this.uri_ = uri;
}
}

Generated Schema:
----------------------------------------------------------------------------
alter table FOO_BAR2 drop constraint FK46C71E987BF1D354;
alter table FOO_BAR2 drop constraint FK46C71E98745A84E7;
drop table FOO2 cascade constraints;
drop table BAR2 cascade constraints;
drop table FOO_BAR2 cascade constraints;
create table FOO2 (
ID VARCHAR2(32) not null,
NAME VARCHAR2(255) not null,
URI VARCHAR2(255) not null,
primary key (ID)
);
create table BAR2 (
ID VARCHAR2(32) not null,
NAME VARCHAR2(255) not null,
URI VARCHAR2(255) not null,
primary key (ID)
);
create table FOO_BAR2 (
FOO_ID VARCHAR2(32) not null,
BAR_ID VARCHAR2(32) not null,
BAR_IDX NUMBER(10,0) not null,
primary key (BAR_ID, BAR_IDX)
);
alter table FOO_BAR2 add constraint FK46C71E987BF1D354 foreign key (FOO_ID) references FOO2;
alter table FOO_BAR2 add constraint FK46C71E98745A84E7 foreign key (BAR_ID) references BAR2;


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.