0

我有两个实体 MobeeCustomer 和 CustomerRegion 我想首先从 CustomerRegion 中删除对象我将 CustomerRegion 中的 join Coloumn 为 null 然后从 entityManager 中删除对象但我得到了异常

Mobee客户:

  public class MobeeCustomer implements Serialization{

  private Long id;

  private String custName;

  private String Address;

  private String phoneNo; 

  private Set<CustomerRegion> customerRegion = new HashSet<CustomerRegion>(0);


  @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REMOVE },
   fetch =    FetchType.LAZY, mappedBy = "mobeeCustomer")     
   public Set<CustomerRegion> getCustomerRegion() { 
    return CustomerRegion;  
     }

  public void setCustomerRegion(Set<CustomerRegion> customerRegion) {
      CustomerRegion = customerRegion;    
    }

  }

客户区域

  public class CustomerRegion implements Serializable{

private Long id;

private String custName;

private String description;

private String createdBy;
private Date createdOn;

private String updatedBy;
private Date updatedOn;
   private MobeeCustomer mobeeCustomer;


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "MOBEE_CUSTOMER")
public MobeeCustomer getMobeeCustomer() {
    return mobeeCustomer;
}
public void setMobeeCustomer(MobeeCustomer mobeeCustomer) {
    this.mobeeCustomer = mobeeCustomer;
}


   }

示例代码:

   if (doUpdateRegion.getStatus().equals(UserOperations.DELETE.getType())) {

custEntity = getEntityManager().find(CustomerRegion.class,doUpdateRegion.getId());


      BeanUtils.copyProperties(custEntity, doUpdateRegion);

deletedRegionsList.add(custEntity);



}


  for (CustomerRegion region : deletedRegionList) {


            region.setMobeeCustomer(null);
      getEntityManager().remove(region);
}







StackTrace:

please suggest me how to remove the CustomerRegion Object I  am getting Exception

  javax.persistence.EntityNotFoundException: deleted entity passed to persist:          [com.manam.mobee.persist.entity.CustomerRegion#<null>]
15:46:34,614 ERROR [STDERR]     at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:613)
15:46:34,614 ERROR [STDERR]     at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:299)
15:46:34,614 ERROR [STDERR]     at org.jboss.seam.persistence.EntityManagerProxy.flush(EntityManagerProxy.java:92)
15:46:34,614 ERROR [STDERR]     at org.jboss.seam.framework.EntityHome.update(EntityHome.java:64)
4

2 回答 2

0

您正在尝试删除没有id. 因此实体管理器无法在数据库中找到它。这称为分离实体。

在删除它们之前尝试从数据库中检索区域列表。

如果你这样做了,那么这看起来是个transaction问题。您在事务之外,因此实体与会话分离。尝试在同一个事务中完成所有操作,不要调用entityManager.remove(),只需从集合中删除该项目,当您关闭事务并提交时,刷新将自动进行。

例如与Spring

@Transactional(readOnly = false)
public void deleteRegions(Set<CustomerRegion> deletedRegionList){
    revion.getMobeeCustomer().getCusotmerRegion.removeAll(deletedRegionList);
}

如果该方法结束事务,它将提交。Hibernate/JPA 结束的所有修改都会调用刷新,它将内存状态与数据库同步。

于 2012-06-12T10:42:14.703 回答
0

也许问题是 MobeeCustomer 中仍然有对 CustomerRegion 的引用。如果这是问题,这应该解决它:

for (CustomerRegion region : deletedRegionList) {
      region.getMobeeCustomer().getCustomerRegion().remove(region);
      getEntityManager().remove(region);
}
于 2012-06-12T10:42:19.937 回答