多对多关系操作对象
FORUM自定义论坛表
FORUM_TOPIC论坛帖子表
FORUM_TOPIC_RELATION论坛主题关系表
其中一个自定义的论坛对应多个论坛帖子
一个帖子也可以存在于多个主题的论坛中
这里以相对比较方便的jpa注解方式 anotation
Hibernate中对多多关系所产生的尸体主要有两种方式,一种是产生两个实体,一种是产生三个实体将关系表也产生实体
产生两个实体配置方式和查询:
package com.tkbs.domain.forum;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* Forum entity.<br>
* 社区/论坛
*
* @author zxg
*/
@Entity
@Table(name ="FORUM")
publicclass Forum implements java.io.Serializable {
privatestaticfinallong serialVersionUID =-2129436454756881543L;
private Long id; //主键
private String title; //自定义论坛名称
private Date cdate; //自定义论坛创建时间
private String description; //论坛描述
private Set<ForumTopic> forumTopic=new HashSet<ForumTopic>(); //帖子的集合
@Id
@GeneratedValue
@Column(name ="ID")
public Long getId() {
return id;
}
publicvoid setId(Long id) {
this.id = id;
}
@Column(name ="TITLE")
public String getTitle() {
return title;
}
publicvoid setTitle(String title) {
this.title = title;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name ="cdate")
public Date getCdate() {
return cdate;
}
publicvoid setCdate(Date cdate) {
this.cdate = cdate;
}
@Column(name ="DESCRIPTION")
public String getDescription() {
return description;
}
publicvoid setDescription(String description) {
this.description = description;
}
/**
* inverseJoinColumns: inverse反转的意思,在JPA中可以理解为被维护端
* inverseJoinColumns中的@JoinColumn: 被维护端外键的定义 forum_id:指中间表那个外键字段与学生表关联
* joinColumns: 关系维护端的定义
*/
@ManyToMany(cascade = CascadeType.REFRESH, targetEntity = ForumTopic.class, fetch = FetchType.LAZY)
@JoinTable(name ="forum_topic_relation", inverseJoinColumns = @JoinColumn(name ="topic_id"), joinColumns = @JoinColumn(name ="forum_id"))
public Set<ForumTopic> getForumTopic() {
return forumTopic;
}
publicvoid setForumTopic(Set<ForumTopic> forumTopic) {
this.forumTopic = forumTopic;
}
}
FORUM-_TOPIC对应entity、
package com.tkbs.domain.forum;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* ForumTopic entity.<br>
* 社区帖子
*
* @author zxg
*/
@Entity
@Table(name ="FORUM_TOPIC")
publicclass ForumTopic implements java.io.Serializable {
privatestaticfinallong serialVersionUID =6327101690347988478L;
private Long id; //主键
private String title; //帖子标题
private Date cdate; //帖子创建时间
private String content; //帖子内容
private Long accessTimes;//帖子访问次数
private Long commentTimes;//帖子评论时间
private Integer setTop; //置顶
private Integer setPerfect;//加精
private Set<Forum> forum =new HashSet<Forum>(); //自定义论坛板块集合
@Id
@GeneratedValue
@Column(name ="ID")
public Long getId() {
return id;
}
publicvoid setId(Long id) {
this.id = id;
}
@Column(name ="TITLE")
public String getTitle() {
return title;
}
publicvoid setTitle(String title) {
this.title = title;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name ="cdate")
public Date getCdate() {
return cdate;
}
publicvoid setCdate(Date cdate) {
this.cdate = cdate;
}
@Lob
@Column(name ="CONTENT")
public String getContent() {
return content;
}
publicvoid setContent(String content) {
this.content = content;
}
@Column(name ="ACCESS_TIMES")
public Long getAccessTimes() {
return accessTimes;
}
publicvoid setAccessTimes(Long accessTimes) {
this.accessTimes = accessTimes;
}
@Column(name ="COMMENT_TIMES")
public Long getCommentTimes() {
return commentTimes;
}
publicvoid setCommentTimes(Long commentTimes) {
this.commentTimes = commentTimes;
}
@Column(name ="SET_TOP")
public Integer getSetTop() {
return setTop;
}
publicvoid setSetTop(Integer setTop) {
this.setTop = setTop;
}
@Column(name ="SET_PERFECT")
public Integer getSetPerfect() {
return setPerfect;
}
publicvoid setSetPerfect(Integer setPerfect) {
this.setPerfect = setPerfect;
}
/**
* inverseJoinColumns: inverse反转的意思,在JPA中可以理解为被维护端
* inverseJoinColumns中的@JoinColumn: 被维护端外键的定义 topic_id:指中间表那个外键字段与帖子表关联
* joinColumns: 关系维护端的定义
*/
@ManyToMany(cascade = CascadeType.PERSIST, targetEntity = ForumTopic.class, fetch = FetchType.LAZY)
@JoinTable(name ="forum_topic_relation", inverseJoinColumns = @JoinColumn(name ="forum_id"), joinColumns = @JoinColumn(name ="topic_id"))
public Set<Forum> getForum() {
return forum;
}
publicvoid setForum(Set<Forum> forum) {
this.forum = forum;
}
}
多对多关系表的操作: