本文主要是介绍ManyToMany(基于注解)使用之进阶(中级版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
社团和成员就是多对多的关系,一个成员有多个社团,一个社团也有多个成员,这里的多对多映射采用中间表连接的映射策略,建立中间表的映射策略,建立中间表分别引入俩边的主键作为外键。通过中间表映射俩个表之间的关系。
下面就以社团类和成员类为例介绍多对多的映射关系的实例
Club实体类
package com.cn.entity;import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;/*** @ClassName: Club* @Description: 社团的实体类* @author: Administrator* @date: 2016年7月6日 下午3:27:43*/
@Entity //实体类
@Table(name="iyo_club",catalog="iyo") //哪个数据库那张表
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) //二级缓存
public class Club implements Serializable{/*** @fieldName: serialVersionUID* @fieldType: long* @Description: */private static final long serialVersionUID = -1576706279212597880L;private Integer clubId;//社团IDprivate String clubIntroction;//社团简介private String ClubName;//社团名称private Integer clubLevel;//社团等级private Integer clubNumber;//社团人数private Date clubCreateTime;//创建时间private User user;//创始人private College college;//所属学院private Set<Member> member = new HashSet<Member>();public Club() {//super();}public Club(String clubIntroction, String clubName, Integer clubLevel, Integer clubNumber, Date clubCreateTime,User user, College college) {//super();this.clubIntroction = clubIntroction;ClubName = clubName;this.clubLevel = clubLevel;this.clubNumber = clubNumber;this.clubCreateTime = clubCreateTime;this.user = user;this.college = college;}@Id //主键@GeneratedValue(strategy = GenerationType.IDENTITY) //数据库自增public Integer getClubId() {return clubId;}public void setClubId(Integer clubId) {this.clubId = clubId;}public String getClubIntroction() {return clubIntroction;}public void setClubIntroction(String clubIntroction) {this.clubIntroction = clubIntroction;}public String getClubName() {return ClubName;}public void setClubName(String clubName) {ClubName = clubName;}public Integer getClubLevel() {return clubLevel;}public void setClubLevel(Integer clubLevel) {this.clubLevel = clubLevel;}public Integer getClubNumber() {return clubNumber;}public void setClubNumber(Integer clubNumber) {this.clubNumber = clubNumber;}public Date getClubCreateTime() {return clubCreateTime;}public
这篇关于ManyToMany(基于注解)使用之进阶(中级版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!