坚持实体参考已经存在的实体

时间:2013-11-20 09:39:03

标签: java jpa eclipselink

我是jpa中的新手并且我想要持有一个实体(leaveTotal - 它保留休假信息,例如分配的休假天数,上一年的b / f,当前月份以及对员工和typeofleave类的引用)员工和typeofleave实体已经坚持了下来。所以,现在我想将LeaveTotal与两个类相关联,然后保持它。

使用eclipselink 2.1作为库及其桌面应用程序。

我正在尝试的是这个

          Dept dp = getDeptByName(em, departmentComboBox.getSelectedItem().toString());
            Employee emp = new Employee();
            emp.setId(id1);
            emp.setIdnum(idTextField.getText());
            emp.setName(empnameTextField.getText());
            emp.setAddress(addressTextField.getText());
            emp.setEmail(emailTextField.getText());
            emp.setGender(genderComboBox.getSelectedItem().toString());
            emp.setPhone(phoneTextField.getText());
            emp.setStaffcol(staffNoTextField.getText());
            emp.setDept(dp);

            em.getTransaction().begin();
            dp.getEmployeeCollection().add(emp);
            em.persist(dp);
            em.flush();
            em.getTransaction().commit();

            Leavetype lp;
            lp = em.createQuery("SELECT c FROM Leavetype c WHERE c.name = :name", Leavetype.class).setParameter("name", "annual").getSingleResult();
            Leavetotal lt = new Leavetotal();
            lt.setId(id1);
            lt.setAllocated(Integer.parseInt(allocatedDaysTextField.getText()));
            lt.setBalbf(Integer.parseInt(balBFTextField.getText()));
            lt.setTotal(Integer.parseInt(totalTextField.getText()));
            lt.setRem(Integer.parseInt(remainingTextField.getText()));
            lt.setMonthly(Integer.parseInt(monthlyTextField.getText()));
            lt.setMonth(getMonthForInt(monthNumber));
            lt.setStaff(emp);
            lt.setTypeName(lp);


            em.getTransaction().begin();
            emp.getLeavetotalCollection().add(lt);
            lp.getLeavetotalCollection().add(lt);
            em.persist(emp);
            em.persist(lp);
            em.flush();
            em.getTransaction().commit();

0 个答案:

没有答案