How to persisting attributes in an Associative Table using JPA and Hibernate?

910 Views Asked by At

I want to save attributes on an associative table in hibernate and don't know how.

I came up with the following example: 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 and the rating given by the user.

The problem: mapping the rate. I used XMLs to do the mapping, though I can also use annotations if it makes it easier to you.

The following are my code snippets:

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);

JAVA

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
}

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
}

hbm.xml

<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>

   <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>
1

There are 1 best solutions below

1
On

I did the same thing for user and groups but using annotations, check if is useful to you

package net.smanne.account.domain;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.xml.bind.annotation.XmlRootElement;

import org.codehaus.jackson.annotate.JsonIgnore;

import net.smanne.core.domain.CoreEntity;

@Entity(name="accounts")
@XmlRootElement
public class Account extends CoreEntity {

    private String name;
    private String password;
    @ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinTable(name="account_groups",
    joinColumns=@JoinColumn(name="account_id"),
    inverseJoinColumns=@JoinColumn(name="group_id"))
    private List<Group> group; 

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<Group> getGroups() {
        return group;
    }

    public void setGroups(List<Group> groups) {
        this.group = groups;
    }
}

//Group.java
package net.smanne.account.domain;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.xml.bind.annotation.XmlRootElement;

import net.smanne.core.domain.CoreEntity;

@Entity(name="groups")
@XmlRootElement
public class Group extends CoreEntity {

    private String name;
    private String description;

    /*@ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name="account_groups",
    joinColumns=@JoinColumn(name="group_id"),
    inverseJoinColumns=@JoinColumn(name="account_id"))
    private List<Account> accounts;*/

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    /*public List<Account> getAccounts() {
        return accounts;
    }

    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
    }*/
}