【问题标题】:hibernate one to many relationship, child table foriegn key table setting 0 value休眠一对多关系,子表外键表设置0值
【发布时间】:2016-09-18 20:15:20
【问题描述】:

大家好,创建一对多关系。 我有 2 个表,一个是 Emp,另一个是 Project。 一个 Emp 可以有多个 Project。 这些是我的 bean 类。

 public class Emp {

    public List<Project> getProjectList() {
        return projectList;
    }

    public void setProjectList(List<Project> projectList) {
        this.projectList = projectList;
    }

    @Generated(value = { "id" })
    @Id
    private int id;

    @Column(name="name")
    private String name;

    @Column(name="city")
    private String  city;

    @OneToMany( cascade=CascadeType.ALL , fetch=FetchType.LAZY, mappedBy="emp",orphanRemoval=true)
    private List<Project> projectList=new ArrayList<Project>();


    public void addProject(Project project) {
        this.projectList.add(project);
        if (project.getEmp() != this) {
            project.setEmployer(this);
        }
    }

    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 String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

}

这是项目类

 @Entity
        public class Project {




        @Generated(value = { "id" })
        @Id
        private int id;

        @Column(name="name")
        private String name;

        @ManyToOne(cascade=CascadeType.ALL)
        @JoinColumn(name="empid", nullable=false)
        private Emp emp;


        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 Emp getEmp() {
            return emp;
        }


        public void setEmp(Emp emp) {
            this.emp = emp;
        }


        public void setEmployer(Emp emp2) {
             this.emp = emp2;
                if (!emp2.getProjectList().contains(this)) { // warning this may cause performance issues if you have a large data set since this operation is O(n)
                    emp.getProjectList().add(this);
                }


        }

    }

这是我保存父母和孩子的代码

public class MainTest {

public static void main(String[] args) {
    Configuration configuration = new Configuration();
    configuration.addAnnotatedClass(org.hibernate.model.Emp.class);
    configuration.addAnnotatedClass(org.hibernate.model.Project.class);
    configuration.addAnnotatedClass(org.hibernate.model.Stu.class);
    configuration.addAnnotatedClass(org.hibernate.model.Address.class);



    configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
    configuration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/test");
    configuration.setProperty("hibernate.connection.password", "123456789");
    configuration.setProperty("hibernate.connection.username", "root");
    configuration.setProperty("hibernate.show_sql", "true");
    configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
    SessionFactory sessionFactory = configuration.buildSessionFactory();


Session session =   sessionFactory.openSession();
org.hibernate.Transaction transaction =session.beginTransaction();

Emp emp = new Emp();
emp.setCity("nagaland");
emp.setName("divyffa");

Project project1=new Project();
project1.setName("dotedxvc");
emp.addProject(project1);
session.save(emp);
transaction.commit();
session.close();



}


}

这是我的表的结构

      CREATE TABLE `emp` (

      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(45) DEFAULT NULL,
      `city` varchar(45) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;


    CREATE TABLE `project` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(45) DEFAULT NULL,
      `empid` int(11) NOT NULL,
      PRIMARY KEY (`id`),

  KEY `forgn-project_idx` (`empid`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;

每当我保存我的父类时,我的子类也会被保存,但 empid 列值被保存为 0。每次它被保存为 0。

请问我不知道是什么问题,感谢您的帮助。 我有关于这个问题的搜索,但没有找到任何结果。

【问题讨论】:

    标签: java mysql hibernate jpa jpa-2.0


    【解决方案1】:

    看来你只设置了一方的关系。 我认为这里需要像下面这样的东西

    project1.setEmp(emp);

    【讨论】:

    • 你看到我的pojo了吗。我有add方法在两边都添加了我的对象。它同步了两个对象
    • 对不起,我的错误..另一个重点是使用提取类型@ManyToOne(fetch=FetchType.LAZY)
    【解决方案2】:

    我认为您的 FOREIGN KEY 列应该是指父表的意思是“emp”表的 PRIMARY KEY 列。我已经修改了子表的意思是“项目”结构,比如-

    创建表`项目`(
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(45) 默认为空,
    `empid` int(11) 非空,
     主键(`id`),
     外键 (empid) 参考 emp (id)
      ) 引擎=InnoDB AUTO_INCREMENT=22 默认字符集=utf8; 

    在 Emp 实体类中添加 @OneToMany 映射为-

    @OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL,mappedBy="emp") 私有列表项目列表;

    并且还在Project实体类中修改@ManyToOne映射为-

    @ManyToOne(optional = false)
    @JoinColumn(name = "empid", insertable = true)  
    private Emp emp;
    

    我想这对你有用。

    【讨论】:

      猜你喜欢
      • 2012-02-05
      • 2013-05-27
      • 2020-07-02
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      相关资源
      最近更新 更多