欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

组合主键——实体类——Oracle数据库

程序员文章站 2022-06-02 16:48:44
...
@Entity
@Table(name = "XXXX")
public class RolePrivilegeMap implements CmsMainBo {

    private RolePrivilegeMapId id;

    @EmbeddedId
    public RolePrivilegeMapId getId() {
        return id;
    }

    public void setId(RolePrivilegeMapId id) {
        this.id = id;
    }
    public RolePrivilegeMap() {
    }
}
@Entity
@Embeddable
public class RolePrivilegeMapId implements XXXXBo {

    private static final long serialVersionUID = 2L;// 序列化ID

    private Integer roleId;

    private Integer privilegeInfoId;

    public RolePrivilegeMapId(Integer roleId, Integer privilegeInfoId) {
        super();
        this.roleId = roleId;
        this.privilegeInfoId = privilegeInfoId;
    }

    @Column(name = "role_id", unique = false, nullable = false)
    public Integer getRoleId() {
        return roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    @Column(name = "privilege_info_id", unique = false, nullable = false)
    public Integer getPrivilegeInfoId() {
        return privilegeInfoId;
    }

    public void setPrivilegeInfoId(Integer privilegeInfoId) {
        this.privilegeInfoId = privilegeInfoId;
    }

    @Override
    public int hashCode() {
        final int prime = 32;
        int result = 1;
        result = prime * result + ((roleId == null) ? 0 : roleId.hashCode());
        result = prime * result + ((privilegeInfoId == null) ? 0 : privilegeInfoId.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return false;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        RolePrivilegeMapId other = (RolePrivilegeMapId)obj;
        if (roleId == null) {
            if (other.roleId != null)
                return false;
        } else if (!roleId.equals(other.roleId))
            return false;
        if (privilegeInfoId == null) {
            if (other.privilegeInfoId != null)
                return false;
        } else if (!privilegeInfoId.equals(other.privilegeInfoId))
            return false;
        return true;
    }
}
if (privilegeInfos != null && rolePrivilegeMaps != null) {
	privilegeInfoImpl.saveAll(privilegeInfos);
	privilegeInfoImpl.flush();
	for (PrivilegeInfo info: privilegeInfos){
		RolePrivilegeMap rolePrivilegeMap = new RolePrivilegeMap();
		rolePrivilegeMap.setId(new RolePrivilegeMapId(roleId,info.getPrivilegeInfoId()));
		rolePrivilegeMaps.add(rolePrivilegeMap);
	}
	privilegeInfoImpl.saveAll(rolePrivilegeMaps);
	privilegeInfoImpl.flush();
}