2

我想在休眠状态下将属性保存在关联表上,但不知道如何。

我想出了以下示例:我有一个表 USERS、一个表 MEDIA(存储电影和书籍)和一个名为 USER_MEDIA 的关联表,如果用户“看到”了 MEDIA 和用户给出的评分,我将在其中存储.

问题:映射速率。我使用 XML 来进行映射,但如果它使您更容易,我也可以使用注释。

以下是我的代码片段:

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

1 回答 1

0

我为用户和组做了同样的事情,但使用注释,检查是否对你有用

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;
    }*/
}
于 2012-04-04T11:10:09.247 回答