-->
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.  [ 1 post ] 
Author Message
 Post subject: Mapping an Associative Table that has various attributes
PostPosted: Tue Sep 14, 2010 9:00 pm 
Newbie

Joined: Tue Sep 14, 2010 8:45 pm
Posts: 2
Hi everyone,

The problem I am having can be summarized as follows: I have a table USERS, a table MEDIA (where movies and books are stored) and an associative table called USER_MEDIA where I store if a USER has "seen" a MEDIA. On table USER_MEDIA I also need to store the latest rate of this media given by the user.

The problem: mapping the rate. The approach I am using does not work, because I am mapping RATE (an attribute of table USER_MEDIA) as an attribute in the media.hbm.xml; instead it should somehow be linked to the table USER_MEDIA. Any ideas on how to implement this? Thanks!!

The following are my code snippets:

CREATE TABLES
Code:
create table USER(
   UUID bigint not null auto_increment,
   NAME text,
   primary key (UUID)
);

create table MEDIA(
   MEDIAID bigint not null auto_increment,
   NAME text,
   primary key (MEDIAID)
);

create table USER_MEDIA(
   UUID bigint not null,
   MEDIAID bigint not null,
   RATE double,
   RATE_FORMAT varchar(2),
   primary key (UUID, MEDIAID)
);

alter table USER_MEDIA add foreign key (UUID) REFERENCES USER(UUID);
alter table USER_MEDIA add foreign key (MEDIAID) REFERENCES MEDIA(MEDIAID);


USER.HBM.XML
Code:
<hibernate-mapping package="package.model">

   <class name="User" table="USER">
      <id name="id" column="UUID">
         <generator class="native" />
      </id>
      
      <property name="name" type="text" />

      <set name="medias" table="USER_MEDIA" cascade="all">
         <key column="UUID" />
         <many-to-many column="MEDIAID" class="package.model.Media" />
      </set>

   </class>
</hibernate-mapping>


MEDIA.HBM.XML
Code:
<hibernate-mapping package="package.model">
   <class name="Media" table="MEDIA">
      <id name="id" column="MEDIAID">
         <generator class="native" />
      </id>
      
      <property name="name" type="text" />
      <property name="rate" type="double" />
      <property name="rateFormat" type="text" column="RATE_FORMAT" />

      <set name="users" table="USER_MEDIA" cascade="all">
         <key column="MEDIAID" />
         <many-to-many column="UUID" class="package.model.User" />
      </set>
      
   </class>
</hibernate-mapping>


USER.JAVA
Code:
public class User {
   private Long id;
   private String name;
   private Double rating;
   private String ratingFormat;
   private Set<Media> medias = new HashSet<Media>();
   //getters e setters
}


MEDIA.JAVA
Code:
public class Media {
   private Long id;
   private String name;
   private Double rate;
   private String rateFormat;
   private Set<User> users = new HashSet<User>();
   //getters e setters
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.