在休眠状态下,使用外键将数据插入一个表,而不会干扰另一表

问题描述

我这里有2节课

  1. 部门(did,dname)
  2. 员工[身份证,姓名,地址,部门(did)] 这里部门列是部门表中的外键。
  • 一个员工只有一个部门
  • 员工表将外键作为部门映射到部门的部门
  • 部门不应有重复的条目

enter image description here

enter image description here

当我将数据插入到员工表中时,我也将数据插入到部门表中,我不想要那个。

问题是当我将数据插入到employee表中时,我应该在雇员表中插入“ did”'',而不要在部门表中插入任何内容。即,如果要在employee表中插入的数据(dname)已经存在于department表中,那我在控制台中应该不会出现任何错误并在department表中进行更新。

在以下类和映射策略中要进行哪些更改

Department.java

    @Entity
public class Department1 
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int did;
    @Column(unique=true)
    private String dname;




public int getDid() {
    return did;
}
public void setDid(int did) {
    this.did = did;
}
public String getDname() {
    return dname;
}
public void setDname(String dname) {
    this.dname = dname;
}    

Employee.java

        @Entity
    public class Employe1

 
{
    @Id
    @GeneratedValue
    private int id;
    private String name;
    
    @ManyToOne
    @JoinColumn(name="department",referencedColumnName="did")
    private Department1 department;
    private String dob;
    private long phone;
    private String email;
    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Department1 getDepartment() {
        return department;
    }
    public void setDepartment(Department1 department) {
        this.department = department;
    }
    public String getDob() {
        return dob;
    }
    public void setDob(String dob) {
        this.dob = dob;
    }
    public long getPhone() {
        return phone;
    }
    public void setPhone(long phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...