0

I have two entitys MobeeCustomer and CustomerRegion i want to remove the object from CustomerRegion first Im put join Coloumn in CustomerRegion is null then Remove the Object from the entityManager but Iam getting Exception

MobeeCustomer:

  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;    
    }

  }

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;
}


   }

sample code:

   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)
nag
  • 647
  • 6
  • 25
  • 43

2 Answers2

0

You are trying to remove an entity that has no id. Thus the entity manager cannot locate it in the DB. This is call a detached entity.

Try retrieve the list of regions from the DB before delete them.

If you did, then This looks like a transaction problem. You are outside a transaction and thus the entity is detached from the session. Try do it all in the same transaction and don't call entityManager.remove(), just remove from the collection the item and the flush will be automatic when you close the transaction and commit.

For example with Spring

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

If that method ends the transaction it will commit. All modifications Hibernate/JPA ends calls a flush, which sync the state of the memory with the DB.

ssedano
  • 8,322
  • 9
  • 60
  • 98
  • How is he going to delete the entity from the database if he doesn't call remove? – Pablo Jun 12 '12 at 10:45
  • because when the transaction ends all modifications JPA calls a flush, which sync the state of the memory with the DB. When those entities becomes orphan, if the configuration is the appropriate the object will be deleted – ssedano Jun 12 '12 at 10:47
  • In CustomerRegion, mobeeCustomer can be null and the id is a long. It seems to me like CustomerRegion can exists without a MobeeCustomer, so they would not become orphans. – Pablo Jun 12 '12 at 10:53
  • then those won't be deleted, only the relation – ssedano Jun 12 '12 at 10:54
  • Yes, the problem is that he seems to want to delete one CustomerRegion. – Pablo Jun 12 '12 at 11:02
0

Perhaps the problem is that there is still a reference to CustomerRegion in MobeeCustomer. If that is the problem, this should fix it:

for (CustomerRegion region : deletedRegionList) {
      region.getMobeeCustomer().getCustomerRegion().remove(region);
      getEntityManager().remove(region);
}
Pablo
  • 3,655
  • 2
  • 30
  • 44