一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

jpa manyTOMany映射笔记

时间:2022-06-29 00:43:07 编辑:袖梨 来源:一聚教程网

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

 

 代码如下 复制代码

 

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 administators;

 public String getTitle() {
  return title;
 }

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

 public String getDescn() {
  return descn;
 } www.111Cn.net

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

 public String getPath() {
  return path;
 }

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

 public Set getAdministators() {
  return administators;
 }

 public void setAdministators(Set 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 modules;

 public Set getModules() {
  return modules;
 }

 public void setModules(Set 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;
 }

www.111com.net

 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);
 }

}

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

热门栏目