Hi James,
I am still back to square one after so many weeks of toiling on this issue despite having resolved the issue of populating the correct number of schools. Ie a total of 29 unique records for both counties 1 & 2. However, the duplicate records (when adding multiple of the same records) remained un-resolved. I have since simplified the
testing process by supplying only a single identical school A (e.g. Local Secondary School (5 miles away) for both counties with the following outcomes:
COUNTY
ID Name
1 County 1
2 County 2
COUNTY_SCHOOL
ID COUNTY_ID SCHOOL_ID
1 County 2 1
2 County 1 2
SCHOOL
ID Name
1 School A
2 School A
Yet I wanted the following data to be generated instead:
COUNTY
ID Name
1 County 1
2 County 2
COUNTY_SCHOOL
ID COUNTY_ID SCHOOL_ID
1 County 1 1
2 County 2 1
SCHOOL
ID Name
1 School A
I am not clear on why the following behavior is taking place within JPA:
( i ) The ID for County 1 in COUNTY_SCHOOL table is 2 instead of 1 even though it was the first to have been added. Hence, ID for County 2 should be 2 instead of 1 for the same reason.
( ii ) More importantly, why is School A being populated twice even though the data/name is identical.
There appears to be some transactional issue/delay using the container managed JTA. Another intriguing symptom is the following exception occurred after having taken out the @GeneratedValue(strategy = GenerationType.IDENTITY) in School.java in the hope that only a single record is generated:
EJB5018: An exception was thrown during an ejb invocation on [CountyBean]
javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean; nested exception is: javax.persistence.EntityExistsException:
Exception Description: Cannot persist detached object [domain.School@ce9fa6].
Class> domain.School Primary Key> [0]
javax.persistence.EntityExistsException:
Exception Description: Cannot persist detached object [domain.School@ce9fa6].
Class> domain.School Primary Key> [0]
at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.persist(EntityManagerImpl.java:224)
at com.sun.enterprise.util.EntityManagerWrapper.persist(EntityManagerWrapper.java:440)
at ejb.CountyBean.createCounty(CountyBean.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
oracle.toplink.essentials.exceptions.ValidationException
... 38 more
On the other hand, another database exception occurred when directly persisting County & School using remoteCountybean.createCounty(county) as opposed to sending it through a message queue with sendJMSMessageToMyQueue(county) which works:
02/02/2011 5:22:14 PM com.sun.enterprise.appclient.MainWithModuleSupport <init>
WARNING: ACC003: Application threw an exception.
javax.ejb.EJBException: nested exception is: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.RemoteException: null; nested exception is:
javax.persistence.EntityExistsException:
Exception Description: Cannot persist detached object [domain.School@1fcea34].
Class> domain.School Primary Key> [1]
Caused by: javax.ejb.EJBException: nested exception is: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.RemoteException: null; nested exception is:
javax.persistence.EntityExistsException:
Exception Description: Cannot persist detached object [domain.School@1fcea34].
Class> domain.School Primary Key> [1]
at ejb._CountyRemote_Wrapper.createCounty(ejb/_CountyRemote_Wrapper.java)
at addCounty(localImportCounty.java:296)
EJB5018: An exception was thrown during an ejb invocation on [CountyBean]
javax.ejb.EJBException
at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:3894)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:3794)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:3596)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1379)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1316)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:210)
at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:117)
at $Proxy82.createCounty(Unknown Source)
Caused by: javax.persistence.EntityExistsException:
Exception Description: Cannot persist detached object [domain.School@1fcea34].
Class> domain.School Primary Key> [1]
at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.persist(EntityManagerImpl.java:224)
at com.sun.enterprise.util.EntityManagerWrapper.persist(EntityManagerWrapper.java:440)
at ejb.CountyBean.createCounty(CountyBean.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.enterprise.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1011)
at com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:175)
at com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseContainer.java:2920)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4011)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:203)
... 17 more
The findSchool(name) method is working fine. In fact, all the code is working fine during debugging.
I am exhausted on guessing where the issue lies still and would appreciate your suggestion once again.
Many thanks,
Jack