2012-04-04 96 views
1

我想在休眠的关联表上保存属性,不知道如何。如何使用JPA和Hibernate在关联表中保留属性?

我想出了以下示例:我有一个表USERS,一个表MEDIA(其中存储电影和书籍)以及一个名为USER_MEDIA的关联表,如果USER已经“看到”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> 
+0

我不确定你的代码有什么问题,你可以更具体一些。我认为注释要容易得多。 – mbaydar 2012-04-04 11:19:17

回答

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", 
    [email protected](name="account_id"), 
    [email protected](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", 
    [email protected](name="group_id"), 
    [email protected](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; 
    }*/ 
} 
+0

感谢您的帮助,虽然我不明白这个例子与原始问题有何关系(这是关联表中的持久属性) – Pomario 2012-04-04 11:14:12

相关问题