[jboss-user] [JBoss Seam] - Re: NonUniqueObjectException and EntityManager

blabno do-not-reply at jboss.com
Wed Jan 23 14:51:47 EST 2008


Abrakadabra !

I have stateful CountryCreatorBean, that has a reference to stateless LocationBean. When CountryCreatorBean.addCountry() is called then in the body of that method LocationBean.addCountry(country) is called. Country variable is injected into CountryCreatorBean by the SEAM, by the way.
It is responsibility of LocationBean, who plays role of location manager, to check if entity already exists in DB. If such a country already exists then some exception should be thrown by LocationBean. 

Now is the fun part. At first I've decided to use org.hibernate.NonUniqueObjectException, since it fits the purpose perfectly. Of course in CountryCreatorBean.addCountry method I prepaired try/catch for such an ocasion. But guess what ! Transaction gets rolled back and exception is not caught, and web page gets filled with stack trace.
After a while I figured that in stead of org.hibernate.NonUniqueObjectException the javax.ejb.EJBTransactionRolledbackException gets to try/catch block.

Remember to read stack trace carefully ! By the way, what do you think about such approach to move persistance to stateless bean ? I chose this way to write business logic independently from web-framework.

@Stateful
  | @Scope(ScopeType.SESSION)
  | @Name("countryCreator")
  | public class CountryCreatorBean implements CountryCreatorLocal {
  | 
  |     @In(create = true)
  |     private Country country;
  |     @EJB
  |     private LocationLocal locationBean;
  | 
  |     public void addCountry() {
  |         try {
  |             locationBean.add(country);
  |         } catch (javax.ejb.EJBTransactionRolledbackException exc) {
  |             System.out.println("RuntimeException caught !"+exc);
  |             FacesMessages.instance().add("#{messages.countryAlreadyExists}");
  |         }
  |     }
  | 
  |     @Remove
  |     public void destroy() {
  |     }
  | }

@Stateless
  | public class LocationBean implements LocationLocal {
  | 
  |     @PersistenceContext
  |     private EntityManager em;
  | 
  |     public void add(Country country) throws NonUniqueObjectException {
  |             if( em.find(Country.class,country.getId()) == null )
  |                 em.persist(country);
  |             else throw new NonUniqueObjectException(country.getId(),Country.class.getCanonicalName());
  |     }
  | }

And one more thing, nobody noticed stupid condition from previous post

if( em.find(Country.class,country.getId()) != null )    //this triggers exception
  |                 em.persist(country);

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4122780#4122780

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4122780



More information about the jboss-user mailing list