2014-09-24 1 views
0

나는 다음과 같은 클래스가 있습니다.Hibernate 부모/자식 연관. 업데이트 부모 만들기 자식 관련된 모든 개체를 잃었습니다

public class Employee { 
    private int id; 
    private String firstName; 
    private String lastName; 
    private Address address; 
    private Employer employer; 
} 

public class Employer { 
    private int id; 
    private String name; 
    private Set<Employee> employees; 
} 

public class Address { 
    private int id; 
} 
public class Project{ 
    private int id; 
} 

<hibernate-mapping> 
    <class name="me.hibernate.basic.xml.Employee" table="EMPLOYEE"> 
     <id name="id" type="int" column="id"> 
      <generator class="native" /> 
     </id> 
     <property name="firstName" column="first_name" type="string" /> 
     <property name="lastName" column="last_name" type="string" /> 
     <many-to-one name="address" column="address" unique="true" class="me.hibernate.basic.xml.Address"/> 
     <set name="projects" cascade="save-update, delete-orphan" sort="natural"> 
      <key column="employee_proj_id" /> 
      <one-to-many class="me.hibernate.basic.xml.Project" /> 
     </set> 
     <many-to-one name="employer" column="employer" class="me.hibernate.basic.xml.Employer"/> 
    </class> 
</hibernate-mapping> 



<hibernate-mapping> 
    <class name="me.hibernate.basic.xml.Employer" table="EMPLOYER"> 
     <id name="id" type="int" column="id"> 
      <generator class="native" /> 
     </id> 
     <property name="name" column="name" type="string" /> 
     <set name="employees" cascade="save-update, delete-orphan" table="EMPLOYEE"> 
      <key column="employer"/> 
      <one-to-many class="me.hibernate.basic.xml.Employee" /> 
     </set> 
    </class> 
</hibernate-mapping> 

<hibernate-mapping> 
    <class name="me.hibernate.basic.xml.Address" table="ADDRESS"> 
     <meta attribute="class-description"> This class contains the address detail. </meta> 
     <id name="id" type="int" column="id"> 
      <generator class="native" /> 
     </id> 
    </class> 
</hibernate-mapping> 

    <hibernate-mapping> 
     <class name="me.hibernate.basic.xml.Project" table="PROJECT"> 
      <meta attribute="class-description"> This class contains the project detail. </meta> 
      <id name="id" type="int" column="id"> 
       <generator class="native" /> 
      </id> 
     </class> 
    </hibernate-mapping> 

제 애플리케이션에서는 직원을 거의 생성하지 않고 주소를 지정합니다. 일부 프로젝트를 추가하십시오. 그런 다음 고용주를 만들고 모든 직원을 고용주에게 추가합니다. 직원을 추가하고 고용주를 업데이트하면 모든 직원이 주소와 프로젝트를 잃게됩니다. 게으른 로딩 기능을 유지하려면 어떻게해야합니까? 나는 lazy = "false"를 설정할 필요가 없다.

Employee emp1 = ME.addEmployee("Zara", "Ali"); 
Employee emp2 = ME.addEmployee("Daisy", "Das"); 

Address addr1 = ME.addAddress(35, "xxxxxx Street", "XXXXX", "XY7 0ZZ"); 
Address addr2 = ME.addAddress(42, "xxxxxx Street", "XXXXX", "XY7 7ZZ"); 
ME.setAddress(emp1.getId(), addr1); 
ME.setAddress(emp2.getId(), addr2); 

Set<Project> proj = new HashSet<Project>(); 
proj.add(new Project("NOVA")); 
proj.add(new Project("GTA Simplify")); 
proj.add(new Project("Jazz")); 

ME.addProjects(emp.getId(), proj); 
ME.addProjects(emp.getId(), proj); 

모두 지금까지 작업 중입니다. 내가 직원을 추가하면

Set<Employee> emps = new HashSet<Employee>(); 
emps.add(emp1); emps.add(emp2); 

//Add existing employees to employer - Many-to-one bidirectional 
Employer employer = ME.addEmployer("XYZ"); 
ME.addEmployees(employer.getId(), emps); 

public Integer addEmployees(Integer employerID, Set<Employee> employees) { 
Session session = factory.openSession(); 
Transaction tx = null; 
    try { 
     tx = session.beginTransaction(); 
     Employer employer = (Employer) session.get(Employer.class,employerID); 
     employer.getEmployees().clear(); 
     employer.getEmployees().addAll(employees); 
     session.update(employer); 
     tx.commit(); 
    } catch (HibernateException e) { 
    if (tx != null) 
    tx.rollback(); 
    e.printStackTrace(); 
    } finally { 
    session.close(); 
    } 
    return employerID; 
} 

, 모든 외래 키 참조는 PROJECT.employee_proj_id 및 EMPLOYEE.address에 손실됩니다.

최대 절전 로그 :

->Hibernate: update EMPLOYEE set first_name=?, last_name=?, basic=?, address=?, employer=? where id=? 
->Hibernate: update EMPLOYEE set first_name=?, last_name=?, basic=?, address=?, employer=? where id=? 
->Hibernate: update PROJECT set employee_proj_id=null where employee_proj_id=? 
->Hibernate: update PROJECT set employee_proj_id=null where employee_proj_id=? 
->Hibernate: update EMPLOYEE set employer=? where id=? ->Hibernate: update EMPLOYEE set employer=? where id=? 
+0

아래에서 볼 수있는 sql 쿼리가 트리거됩니다. -> 최대 절전 모드 : EMPLOYEE 세트를 first_name =?, last_name = ?, basic =?, address = ?, employer =? 여기서 id =? -> 최대 절전 모드 : EMPLOYEE 세트를 first_name =?, last_name = ?, basic =?, address = ?, employer =? 여기서 id =? -> 최대 절전 모드 : PROJECT 세트를 업데이트하십시오. employee_proj_id = null 여기서 employee_proj_id =? -> 최대 절전 모드 : PROJECT 세트를 업데이트하십시오. employee_proj_id = null 여기서 employee_proj_id =? -> 최대 절전 모드 : EMPLOYEE 설정 고용주 =? 여기서 id =? -> 최대 절전 모드 : EMPLOYEE 설정 고용주 =? 여기서 id =? – Rasika

+0

Employer를 Employer에 추가하는 대신 Employer를 Employee로 설정하면 올바르게 작동합니다. 두 경우 모두 최대 절전 모드로 직원 기록을 업데이트합니다. 그러나 직원이 고용주에 추가 될 때만 직원은 프로젝트를 잃게됩니다. – Rasika

답변

0

난 당신이 모든 것의 근본 원인 인 명 이상의 직원을 추가하기 전에 직원을 삭제하는 것을 알 수있다. 나는 당신이 주소, 프로젝트, 고용주 등을 추가 할 때 무엇을하고 있는지 잘 모르겠다. 직원들을 정리할 필요가 없다. 직원에게 cascadetype.all을 설정 한 경우 고용주를 저장하면 새로 추가 된 직원이 업데이트됩니다.

employer.getEmployees().clear();//remove this line. 
employer.getEmployees().addAll(employees); 
관련 문제