-->
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.  [ 4 posts ] 
Author Message
 Post subject: Many to Many Mapping Concerns
PostPosted: Fri Apr 23, 2004 3:32 pm 
Newbie

Joined: Tue Nov 04, 2003 4:20 pm
Posts: 13
I have an existing database relationship which I'm trying to convert to Hibernate relationships. This is in brief how my relatioships are,

The Database tables
Code:

CREATE TABLE Author (
  BOOK_ID         VARCHAR2 (1)      NOT NULL,
  BOOK_NAME       VARCHAR2 (2)  NOT NULL,
  PRIMARY KEY ( BOOK_ID)) ;

CREATE TABLE Person (
  PERSON_ID       VARCHAR2 (1)      NOT NULL,
  PERSON_NAME       VARCHAR2 (2)  NOT NULL,
  PRIMARY KEY ( PERSON_ID)) ;

CREATE TABLE Academics (
  ACADEMY_ID       VARCHAR2 (1)      NOT NULL,
  ACADEMY_NAME       VARCHAR2 (2)  NOT NULL,
  PRIMARY KEY ( ACADEMIC_ID)) ;

// Many to Many Relationship

CREATE TABLE WORKS (
  BOOK_ID         VARCHAR2 (1)      NOT NULL,
  PERSON_ID       VARCHAR2 (1)  NOT NULL,
  ACADEMY_ID        VARCHAR2 (1)  NOT NULL,
  PRIMARY KEY ( BOOK_ID, PERSON_ID, ACADEMY_ID) ) ;



The Objects

Code:
public class Author{
   private String bookId;
   private String bookName;
   private Set workSet; // Initialized with all Works Objects at startup.

}

public class Person{
   private String bookId;
   private String bookName;
}

public class Academics{
   private String academyId;
   private String academyName;
}

public class Works{
   private String bookId;
   private String academyId;
   private String personId;
}



The Mappings

Code:
<hibernate-mapping>
<class name="Works" table="Works">
<composite-id>
<key-property column="BOOK_ID" length="1" name="bookId" type="java.lang.String"/>
<key-property column="PERSON_ID" length="1" name="personId" type="java.lang.String"/>
<key-property column="ACADEMY_ID" length="1" name="academyId" type="java.lang.String"/>
</composite-id>
</class>
</hibernate-mapping>

<hibernate-mapping>

<class name="Author" table="AUTHOR">
<id column="AUTHOR_ID" name="authorId" type="java.lang.String">
<generator class="assigned"/>
</id>

<set name="workSet" table="WORKS" inverse="true" lazy="false" cascade="none" sort="unsorted">
   <key column="AUTHOR_ID" />
   <one-to-many class="Works" />
</set>

</class>
</hibernate-mapping>



Now my questions,

1. Is there any way that I can specify the classes Book, Person, Academy as the composite keys in the Works Mapping.
2. Upon a search from the database, I get a list of Author Objects which has a list of Works Objects which could have
duplicate entries for Person, Academy. Is there any way to retreive only the distinct entries for Person and Academy
Objects (I can programatically retreive them in the Author class but is there a better way) ?

The Catch :
- The database is preset and I have no flexibility to change it. So whatever I do appears as a workaround as opposed to
fixing the domain model.

Thanks for looking and any thoughts on this would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 25, 2004 6:39 am 
Beginner
Beginner

Joined: Tue Mar 30, 2004 5:54 am
Posts: 22
Location: Bangalore
Hi!

Hibernates provides composite-id to solve this kind of problem look at www.hibernate.org/hib_docs/reference/ht ... pping.html
5.1.5 composit-id

Cheers!

_________________
Thanks
Irfani


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 2:09 am 
Newbie

Joined: Tue Nov 04, 2003 4:20 pm
Posts: 13
hi- Thanks for your response. I already 'am using the composite-id and have questions based on that. Please scroll down to my questions and throw in some suggestions based on that. Anybody please!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 4:46 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
key-many-to-one

_________________
Emmanuel


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