【问题标题】:unable to merge entity with derived entities无法将实体与派生实体合并
【发布时间】:2013-04-16 11:42:35
【问题描述】:

我正在尝试使用 @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_00123 foreign 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

   }

main方法中编写的无异常正常运行的代码如下,

对于坚持新员工,

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 com.entities.derived.EmployeeId@226b19
    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)
....

你能告诉我哪里出错了

【问题讨论】:

    标签: hibernate jakarta-ee jpa jpa-2.0 java-ee-6


    【解决方案1】:

    改变

    @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,您应该可以实现这一点。

    【讨论】:

    • 我知道你已经有一段时间没有回答了,但这也对我有用!
    猜你喜欢
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 2012-12-26
    相关资源
    最近更新 更多