-->
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.  [ 7 posts ] 
Author Message
 Post subject: Many-to-many relation with extra properties
PostPosted: Mon Feb 28, 2005 3:00 pm 
Newbie

Joined: Mon Feb 28, 2005 2:32 pm
Posts: 2
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0 beta 4

Mapping documents:

Code:
<hibernate-mapping package="p">
   <class name="User" table="users">

      <id name="id" column="id" type="integer">
         <generator class="native" />
      </id>
   
      <property name="username" column="username" type="string" length="20" not-null="true" />
      <property name="password" column="password" type="string" length="20" not-null="true" />

      <bag name="books" table="collection" lazy="true">
         <key column="user_id" />
         <many-to-many column="book_id" class="Book" />
         <element column="price" type="string" />
      </bag>

   </class>
</hibernate-mapping>


Code:
<hibernate-mapping package="p">
   <class name="Book" table="books">

      <id name="id" column="id" type="integer" >
         <generator class="native"/>
      </id>

      <property name="ISBN" column="ISBN" type="string" length="10" not-null="true" />
      <property name="title" column="title" type="string" length="250" />
      <property name="year" column="year" type="string" length="4" />
      
      <bag name="owners" table="collection">
         <key column="book_id" />
         <many-to-many column="user_id" class="User" />
         <element column="price" type="string" />
      </bag>
      
   </class>
</hibernate-mapping>


The generated SQL (show_sql=true):

Code:
alter table collectie drop foreign key FK7043CBC637DFAB81;
alter table collectie drop foreign key FK7043CBC61999E1E;
drop table if exists users;
drop table if exists collection;
drop table if exists books;
create table users (
   id integer not null auto_increment,
   username varchar(20) not null,
   password varchar(20) not null,
   primary key (id)
);
create table collection (
   book_id integer not null,
   user_id integer not null
);
create table books (
   id integer not null auto_increment,
   ISBN varchar(10) not null,
   title varchar(250),
   year varchar(4),
   primary key (id)
);
alter table collection add index FK7043CBC637DFAB81 (boek_id), add constraint FK7043CBC637DFAB81 foreign key (boek_id) references books (id);
alter table collection add index FK7043CBC61999E1E (user_id), add constraint FK7043CBC61999E1E foreign key (user_id) references users (id);


Note: I have just translated everything in English, so I might have forgoten a few things.

As you see, I have books and users and I want to set up a relation between them. The current situation isn't too bad, but in my Collection table I also want to add the fields price and date_aquired. Unfortunately I have no idea how to create my Hibernate scheme to achieve this.

Code:
create table collection (
   book_id integer not null,
   user_id integer not null,

   price integer,
   date_aquired date
);

Now that I'm asking for help: the ideal situation would be to have a User class which has a getBooks() method. This getBooks() method would return a collection of Book objects. Such a Book object would have getISBN(), getTitle() and getYear() getters and would also have a getPrice() and a getDateAquired() method, which would of course be different for every user.

Hope I'm making myself a bit clear here.

I'm afraid this situation is a bit too complicated for me at the moment, so I'd surely appreciate any help I can get!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 3:38 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
I had a similar need, with users belonging to groups and having different roles in those groups. I don't think there is a way to set fields on your Book object based on the relation, since the same object would have different properties elsewhere, perhaps in the same session. It would no longer be a coherent object. Please someone correct me if I am wrong about this.
I used an intermediate object, which holds information about the relationship. So it would be:

Code:
User
    one-to-many -> Acquisition (price, date)
          one-to-one -> Book

Now, this means lookups of acquired books are less efficient than before. Set the one-to-one to fetch="join" for Acquisition -> Book to help this out (should still be one query to get the set of Acquisitions).

Happy to hear of another way, though.

-Mike


Top
 Profile  
 
 Post subject: Re: Many-to-many relation with extra properties
PostPosted: Mon Feb 28, 2005 10:11 pm 
Newbie

Joined: Mon Feb 28, 2005 10:04 pm
Posts: 1
nephilim wrote:
Now that I'm asking for help: the ideal situation would be to have a User class which has a getBooks() method. This getBooks() method would return a collection of Book objects. Such a Book object would have getISBN(), getTitle() and getYear() getters and would also have a getPrice() and a getDateAquired() method, which would of course be different for every user.

Hope I'm making myself a bit clear here.

I'm afraid this situation is a bit too complicated for me at the moment, so I'd surely appreciate any help I can get!


Can't you make (book_id,user_id) a primary key, and then create foreign key constraints from collection into books and users? Then you can have a Collection object that has the getters you want.


Top
 Profile  
 
 Post subject: Re: Many-to-many relation with extra properties
PostPosted: Mon Feb 28, 2005 10:26 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
nwc wrote:
Can't you make (book_id,user_id) a primary key, and then create foreign key constraints from collection into books and users? Then you can have a Collection object that has the getters you want.

I don't quite get what you mean. Could you give a simple mapping example?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 6:43 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Read this:
- http://www.experts-exchange.com/Program ... 92207.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 10:24 am 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
Ah. I believe we are talking about the same solution.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 3:40 pm 
Newbie

Joined: Mon Feb 28, 2005 2:32 pm
Posts: 2
This was also a solution I had in mind but I wanted to avoid it. I'll use it now, but hopefully Hibernate will provide some means to do this in the future.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.