[jboss-user] [JBoss Seam] - NonUniqueObjectException and EntityManager
blabno
do-not-reply at jboss.com
Tue Jan 22 14:34:48 EST 2008
Hello, I get NonUniqueObjectException when I call addCountry() on following bean.
em.contains(country) does not find country object, however Contexts.lookupInStatefulContexts("country") does find it. But why calling em.find method invocation makes something to inert found entity into any contexts ?
I found in seam examples that db lookup is based on query result, why is that so and not based on em.find or em.contains ?
@Stateful
| @Scope(ScopeType.SESSION)
| @Name("countryCreator")
| public class CountryCreatorBean implements CountryCreatorLocal {
| @PersistenceContext
| private EntityManager em;
| @In
| private Country country;
| @EJB
| private LocationLocal locationBean;
| public void addCountry() {
| country.setId("pl");
| country.setName("Poland");
| System.out.println(em.contains(country)); //prints false
| System.out.print(Contexts.lookupInStatefulContexts("country")); //this finds object [id=pl;name=Poland]
| if( em.find(Country.class,country.getId()) != null ) //this triggers exception
| em.persist(country);
| // locationBean.add(country);
| }
| @Remove
| public void destroy() {}
| }
ERROR [GraphElement] action threw exception: javax.ejb.EJBTransactionRolledbackException: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [pl.labno.bernard.bidbull.locations.Country#pl]
| javax.el.ELException: javax.ejb.EJBTransactionRolledbackException: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [pl.labno.bernard.bidbull.locations.Country#pl]
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4122347#4122347
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4122347
More information about the jboss-user
mailing list