HIbernate中我没有指定id最后却有id生成
 @Entity
@DiscriminatorValue("员工")
public class Employ extends Person4 {
    private String title;
    private double salary;
    @ManyToOne(cascade=CascadeType.ALL,targetEntity=Manger.class)
    @JoinColumn(name="manger_id",nullable=true)
    private Manger manger;

    public String getTitle() {
        return title;
    }

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

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }



    public Manger getManger() {
        return manger;
    }

    public void setManger(Manger manger) {
        this.manger = manger;
    }




}
 @Entity
@DiscriminatorValue("经理")
public class Manger extends Employ {


    private String department;
    @OneToMany(cascade=CascadeType.ALL
            , mappedBy="manger" , targetEntity=Employ.class)
    private Set<Employ> employs=new HashSet<Employ>();
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public Set<Employ> getEmploys() {
        return employs;
    }
    public void setEmploys(Set<Employ> employs) {
        this.employs = employs;
    }




}

图片说明

1个回答

已解决,由于是使用父类映射所以虽然在manger类中没有指定主键id但由于是继承person类,其中有id主键,所以外键映射能成功

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问