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
}