我想在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 :(得分: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;
}*/
}