One-to-Many|Many-to-One(双向+自身)

时间:2021-01-03 09:58:11
package com.hibernate.ano.self;

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.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.infohold.icqm.db.dbo.Organization;

/**
 * @ClassName: Group.java
 *
 * @Description: 用户组
 *
 * @author GERRARD
 *
 * @date 2015年1月26日下午2:22:24
 * 
 */
@Entity
@Table(name = "group")
public class Group implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8694664905284095892L;
	
	/**
	 * 主键ID
	 */
	private String pid;
	
	/**
	 * 组名
	 */
	private String groupName;
	
	/**
	 * 父类(自身  Many-to-One)
	 */
	private Group parentGroup;

	/**
	 * 子集(自身  One-to-Many)
	 */
	private Set<Group> childGroup;
	
	/**
	 * 所属机构下用户
	 */
	private Set<Users> users;

	public Group() {

	}

	public Group(String pid, String groupName, Group parentGroup,
			Set<Group> childGroup) {
		this.pid = pid;
		this.groupName = groupName;
		this.parentGroup = parentGroup;
		this.childGroup = childGroup;
	}

	@Id
    @Column(name = "pid", unique = true, nullable = false, length = 32)
    @GeneratedValue(generator = "generator")
    @GenericGenerator(name = "generator", strategy = "uuid")
	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	@Column(name = "group_name", length = 64)
	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	@ManyToOne  
	@JoinColumn(name = "parent_id")
	public Group getParentGroup() {
		return parentGroup;
	}

	public void setParentGroup(Group parentGroup) {
		this.parentGroup = parentGroup;
	}

	@OneToMany(mappedBy = "parentGroup", targetEntity = Organization.class, cascade = CascadeType.ALL)
	public Set<Group> getChildGroup() {
		return childGroup;
	}

	public void setChildGroup(Set<Group> childGroup) {
		this.childGroup = childGroup;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "group")
    @NotFound(action = NotFoundAction.IGNORE)
	public Set<Users> getUsers() {
		return users;
	}

	public void setUsers(Set<Users> users) {
		this.users = users;
	}

}

package com.hibernate.ano.self;

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.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "users")
public class Users implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1668563130071655650L;
	
	/**
	 * 主键ID
	 */
	private String pid;
	
	/**
	 * 用户ID
	 */
	private String userId;
	
	/**
	 * 用户名
	 */
	private String userName;
	
	/**
	 * 所属机构
	 */
	private Group group;

	public Users() {
		
	}

	public Users(String pid, String userId, String userName, Group group) {
		this.pid = pid;
		this.userId = userId;
		this.userName = userName;
		this.group = group;
	}

	@Id
    @Column(name = "pid", unique = true, nullable = false, length = 32)
    @GeneratedValue(generator = "generator")
    @GenericGenerator(name = "generator", strategy = "uuid")
	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	@Column(name = "user_id", length = 20)
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	@Column(name = "user_name", length = 64)
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "group_id")
	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

}