关于Hibernate自关联疑问

在处理地址问题时遇到一个一对多双向自关联问题,问题描述如下:
实体类如下:

package cn.com.qycti.callcenter.platform.domain;


import java.util.Date;
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.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.acooly.core.common.domain.AbstractEntity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * 地区 Entity
 *
 * Date: 2014-01-11 13:39:38
 *
 * @author Acooly Code Generator
 */
@Entity
@Table(name = "SYS_AREA")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonIgnoreProperties(value = {"parent"}, ignoreUnknown = true)
public class Area extends AbstractEntity {
    private static final long serialVersionUID = -6976828974218421186L;

    /** 主键ID */
    private Long id;

    /** 父ID */
    private Area parent;

    /** 公司ID */
    private Long cid;

    /** 编码 */
    private String code;

    /** 名称 */
    private String name;

    /** 单层排序时间 */
    private Date orderDate = new Date();

    /** 备注 */
    private String comments;

    private Set<Area> children;

    //导入时候excel设置的父级id
    private String mypid;
    //导入时候excel设置的id
    private String myid;

    private String zipcode;

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

    public void setId(Long id){
        this.id = id;
    }
    public Long getCid(){
        return this.cid;
    }

    public void setCid(Long cid){
        this.cid = cid;
    }
    public String getCode(){
        return this.code;
    }

    public void setCode(String code){
        this.code = code;
    }
    public String getName(){
        return this.name;
    }

    public void setName(String name){
        this.name = name;
    }
    public Date getOrderDate(){
        return this.orderDate;
    }

    public void setOrderDate(Date orderDate){
        this.orderDate = orderDate;
    }
    public String getComments(){
        return this.comments;
    }

    public void setComments(String comments){
        this.comments = comments;
    }

    @JoinColumn(name = "PARENTID")
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    public Area getParent() {
        return parent;
    }

    public void setParent(Area parent) {
        this.parent = parent;
    }

    @OrderBy("orderDate desc")
    @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.MERGE})
    public Set<Area> getChildren() {
        return children;
    }

    public void setChildren(Set<Area> children) {
        this.children = children;
    }

    @Transient
    public String getMypid() {
        return mypid;
    }

    public void setMypid(String mypid) {
        this.mypid = mypid;
    }

    @Transient
    public String getMyid() {
        return myid;
    }

    public void setMyid(String myid) {
        this.myid = myid;
    }

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

}

这里对应数据库中 id与parentId 是相对应的,代表一个地区id下面的子级地区。
后来需要把这个关联关系进行了修改,将 parentId 与code 进行了关联,也就是 父子关系用code进行处理。
但是这个实体类中的自关联一直默认都是id与parentId进行关联,不知道如果修改代码,将这个关系从主键中剥离。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值