2013-04-16 46 views
3

我使用@JoinedColumns嘗試派生實體合併實體,不能與派生實體

讓我告訴你我的代碼,

下面

是SQL代碼,

create table TBL_EMPLOYEE_FIVE(
    EMP_ID integer , 
    NAME varchar(50), 
    COUNTRY varchar(50), 
    MGR_ID integer, 
    MGR_COUNTRY varchar(50), 
    constraint PK_COMPOSIT_001AD primary key(EMP_ID,COUNTRY), 
    constraint FK_COMPO_0foreign key(MGR_ID,MGR_COUNTRY) references TBL_EMPLOYEE_FIVE 
) 

這是映射實體的代碼如下,

package com.entities.derived; 
@Entity 
@Table(name="TBL_EMPLOYEE_FIVE") 
@IdClass(EmployeeId.class) 
public class EmployeeOne implements Serializable{ 

// contructors  

@Id  
@Column(name="EMP_ID")  
private Integer employeeId; 

@Id 
@Column(name="COUNTRY") 
private String empCountry; 

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

@ManyToOne(cascade= {CascadeType.PERSIST, CascadeType.PERSIST}, 
      fetch= FetchType.LAZY, 
      targetEntity=EmployeeOne.class) 
@JoinColumns({ 
      @JoinColumn(name="MGR_ID",referencedColumnName="EMP_ID"), 
      @JoinColumn(name="MGR_COUNTRY",referencedColumnName="COUNTRY") 
}) 
private EmployeeOne manager; 

@OneToMany(cascade={CascadeType.PERSIST, CascadeType.PERSIST},mappedBy="manager")  
private Set<EmployeeOne> employees; 

// getters, setters, hashcode and equals implementation 
} 

這是以下代碼的類ID,

@Embeddable 
public class EmployeeId implements Serializable{ 

public EmployeeId(){} 

public EmployeeId(Integer employeeId,String empCountry){ 
    this.employeeId = employeeId; 
    this.empCountry = empCountry; 
} 

private Integer employeeId; 
private String empCountry; 

// getters, hashcode and equals implementation 

    } 

寫在主方法和代碼正確地工作而不會異常如下,

以用於存留新僱員,

private static int generateId(EntityManager em,String country)throws Exception{ 

    Query q = em.createQuery("select max(e.employeeId) from EmployeeOne e where e.empCountry = '"+country+"'"); 

    List list = q.getResultList(); 
    int count = 0; 

    if(list != null && list.size() > 0){ 
     count = Integer.parseInt(String.valueOf((list.get(0) != null) ? list.get(0) : "0")); 
    } 

    count++; 
    return count; 
} 

插入新僱員,

private static void insertEmployee2(EntityManager em) throws Exception{ 
    EmployeeOne manager = new EmployeeOne("Rajkumar Bahadur", "NEPAL"); 
    manager.setEmployeeId(generateId(em, manager.getEmpCountry())); 
    Set<EmployeeOne> employees = new HashSet<EmployeeOne>(); 

    int count = generateId(em, "FIJI"); 

    employees.add(new EmployeeOne(count,"Okajima Fahim","FIJI",manager)); 
    employees.add(new EmployeeOne(++count,"Jabulani Xitwo","FIJI",manager)); 
    manager.setEmployees(employees); 
    em.persist(manager); 
} 

提取員工是,

private static void getEmployees(EntityManager em) throws Exception{ 
    EmployeeOne manager = em.find(EmployeeOne.class, new EmployeeId(3,"CHINA")); 

    Set<EmployeeOne> employees = manager.getEmployees(); 

    for(EmployeeOne emp : employees){ 

     System.out.println(emp); 
    } 
} 

以上所有方法都運行良好。

但不工作是合併的唯一代碼,代碼如下

private static void updateEmployee(EntityManager em) throws Exception{ 
    EmployeeOne manager = em.find(EmployeeOne.class, new EmployeeId(3,"CHINA"));   
    Set<EmployeeOne> employees = manager.getEmployees(); 
    int count = generateId(em, "IRAN");   
    employees.add(new EmployeeOne(count,"Zaid Khan","IRAN",manager));   
    employees.add(new EmployeeOne(++count,"Maqbool Ansari","IRAN",manager));   

    em.merge(manager); 
} 

這是我得到的是例外,

javax.persistence.EntityNotFoundException: Unable to find com.entities.derived.EmployeeOne with id [email protected] 
    at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:155) 
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:210) 
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:251) 
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148) 
.... 

能否請你告訴我,我在哪裏腳麻

回答

0

變化

@ManyToOne(cascade= {CascadeType.PERSIST, CascadeType.PERSIST}, 
      fetch= FetchType.LAZY, 
      targetEntity=EmployeeOne.class) 

@ManyToOne(cascade= {CascadeType.PERSIST, CascadeType.MERGE}, 
      fetch= FetchType.LAZY, 
      targetEntity=EmployeeOne.class) 

你需要級聯EmployeeOne實例的創建父被 「更新」 時(合併)。通過更改@ManyToOne註釋以包含CascadeType.MERGE,您應該可以實現這一點。

相關問題