mappedby 详解

mappedby=?,后面的这个问号跟拥有方,以下是实例。


Role.java


package com.nbg.nuskin.oa.domain;

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.Id;
import javax.persistence.ManyToMany;

/**
 * 
 * 
 * @author Administrator
 * 
 */
@Entity
public class Role {
	private long id;
	
	private Set<Privilege> privileges = new HashSet<Privilege>();

	@Id
	@GeneratedValue
	public long getId() {
		return id;
	}

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

	//大家一定要注意这里,没有mappedby,拥有方是属于privilege的
	@ManyToMany
	public Set<Privilege> getPrivileges() {
		return privileges;
	}

	public void setPrivileges(Set<Privilege> privileges) {
		this.privileges = privileges;
	}

}




Privilege.java


package com.nbg.nuskin.oa.domain;

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.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;

@Entity
public class Privilege {
	private Long id;
	private Set<Role> roles = new HashSet<Role>();

	@Id
	@GeneratedValue
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	//拥有方是Privilege.java
	@ManyToMany(mappedBy = "privileges")
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

}


下面是RoleAction.java对Role的操作


package com.nbg.nuskin.oa.action;

import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import com.nbg.nuskin.oa.domain.Privilege;
import com.nbg.nuskin.oa.domain.Role;
import com.nbg.nuskin.oa.service.PrivilegeService;
import com.nbg.nuskin.oa.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class RoleAction extends ActionSupport implements ModelDriven<Role> {
	private RoleService roleService;
	
	protected Role model = new Role();

	public RoleService getRoleService() {
		return roleService;
	}

	@Resource(name = "roleService")
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * 设置权限
	 * 
	 * @return
	 */
	public String setPrivilege() {
		Role role = roleService.getById(model.getId());
		System.out.println(role.getName()+";"+role.getDescription());
		List<Privilege> privileges = privilegeService.getByIds(privilegeIds);
		
		role.setPrivileges(new HashSet<Privilege>(privileges));
		roleService.update(role);
		return "toList";
	}

	public Long[] getPrivilegeIds() {
		return privilegeIds;
	}

	public void setPrivilegeIds(Long[] privilegeIds) {
		this.privilegeIds = privilegeIds;
	}

}
只有这样设置mappeby,在上面的action中才能够操作相应的privilege


mappedby原理:被拥有方改变,即拥有方主动关联操作关联关系;拥有方改变,不一定操作相关的关联关系

上面的mappedby如果设在roles这边,privileges就不会进行sql的update操作。

你可能感兴趣的:(mappedby 详解)