2014-04-14 10:29:54  浏览:2021

JPA manyToMany

看了好多资料,终于弄出来了

 

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;

import cn.yulezu.orm.hibernate.BaseEntity;

@Entity
public class Module extends BaseEntity {

 private String title;
 private String descn;
 private String path;
 @ManyToMany(mappedBy = "modules")//modules为administator里面的字段
 private Set<Administator> administators;

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public String getDescn() {
  return descn;
 }

 public void setDescn(String descn) {
  this.descn = descn;
 }

 public String getPath() {
  return path;
 }

 public void setPath(String path) {
  this.path = path;
 }

 public Set<Administator> getAdministators() {
  return administators;
 }

 public void setAdministators(Set<Administator> administators) {
  this.administators = administators;
 }

}

------------------------------------------administator----------------


import java.util.Set;

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 cn.yulezu.orm.hibernate.BaseEntity;

@Entity
public class Administator extends BaseEntity {

 // columns START
 private String account;
 private String password;

 @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
 @JoinTable(name = "Administator_Module", joinColumns = { @JoinColumn(name = "Administator_ID", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "Module_ID", referencedColumnName = "id") })//id分别为这两张表的主键
 private Set<Module> modules;

 public Set<Module> getModules() {
  return modules;
 }

 public void setModules(Set<Module> modules) {
  this.modules = modules;
 }

 public String getAccount() {
  return account;
 }

 public void setAccount(String account) {
  this.account = account;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 

}

-----------------------------baseentity-------------------

@MappedSuperclass
public class BaseEntity
{
 @Id
 //主键自动生成策略:数据库自动增长
 @GeneratedValue
 //主键
 protected Integer id;
 //是否显示
 @Column(nullable=false)
 protected boolean visible = true;
 //插入时间
 @Temporal(TemporalType.TIMESTAMP)
 @Column(nullable=false,updatable=false)
 protected Date insertTime;
 //最后一次修改时间
 @Temporal(TemporalType.TIMESTAMP)
 protected Date lastUpdateTime;

 public Integer getId()
 {
  return id;
 }

 public void setId(Integer id)
 {
  this.id = id;
 }

 public boolean isVisible()
 {
  return visible;
 }

 public void setVisible(boolean visible)
 {
  this.visible = visible;
 }

 public Date getInsertTime()
 {
  return insertTime;
 }

 public void setInsertTime(Date insertTime)
 {
  this.insertTime = insertTime;
 }

 public Date getLastUpdateTime()
 {
  return lastUpdateTime;
 }

 public void setLastUpdateTime(Date lastUpdateTime)
 {
  this.lastUpdateTime = lastUpdateTime;
 }

 /**
  * 是否是新对象
  * @return
  */
 public boolean isNew()
 {
  return (this.id == null);
 }

}

就这样,完成管理员与模块之间的多对多关系映射

返回首页