You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by Phill Moran <pj...@rogers.com> on 2007/04/24 06:55:46 UTC

Cascade question (ver 0.96)

Here is a scenario that shows odd behaviour, I want to see if it is expected or
not. The docs are not clear on it

If I have a many to one relationship for objects Store to Store Type and I
create a new Store and assign it to an existing Store type does this
relationship have to have cascasdeType.persist set when I issue a merge on the
new Store? I had recently removed this as I thought I did not want to create a
duplicate Store Type whenever I added a new  Store. It seems OpenJPA throws the
attached exception when I only have CascadeType.Refresh set. 
Alternatively, this could just be a poorly worded exception/documentation
meaning OpenJPA would check for the existence of this Store Type and not
actually persist it if it exists. The behaviour I am looking for is simply
persist the relation (i.e. the link field)

Thanks,
	Phill
<4|false|0.9.6-incubating> org.apache.openjpa.persistence.ArgumentException:
Encountered new object
"ca.BidSpec.emall.categories.Category-105603b-508b-9c6-00f4-4031ba6429e3:0" in
persistent field "ca.BidSpec.emall.stores.Store.type" of managed object
"ca.BidSpec.emall.stores.Store@56182f" during attach.  However, this field does
not allow cascade attach.  You cannot attach a reference to a new object without
cascading.
FailedObject:
ca.BidSpec.emall.categories.Category-105603b-508b-9c6-00f4-4031ba6429e3:0
	at
org.apache.openjpa.kernel.AttachStrategy.getReference(AttachStrategy.java:272)
	at
org.apache.openjpa.kernel.AttachStrategy.attachField(AttachStrategy.java:189)
	at
org.apache.openjpa.kernel.VersionAttachStrategy.attach(VersionAttachStrategy.jav
a:130)
	at
org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:236)
	at org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:97)
	at org.apache.openjpa.kernel.BrokerImpl.attach(BrokerImpl.java:3124)
	at
org.apache.openjpa.kernel.DelegatingBroker.attach(DelegatingBroker.java:1120)
	at
org.apache.openjpa.persistence.EntityManagerImpl.merge(EntityManagerImpl.java:59
1)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at
org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerIn
vocationHandler.invoke(ExtendedEntityManagerCreator.java:283)
	at $Proxy37.merge(Unknown Source)
	at
ca.BidSpec.emall.persistence.JPAPersistenceFactory.merge(JPAPersistenceFactory.j
ava:95)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils
.java:304)
	at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.j
ava:197)
	at $Proxy35.merge(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils
.java:304)
	at
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(Ref
lectiveMethodInvocation.java:172)
	at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveM
ethodInvocation.java:139)
	at
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(Transa
ctionInterceptor.java:107)
	at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveM
ethodInvocation.java:161)
	at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.j
ava:203)
	at $Proxy35.merge(Unknown Source)
	at
ca.BidSpec.testing.emall.StoreFactoryTest.testSave(StoreFactoryTest.java:69)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at junit.framework.TestCase.runTest(TestCase.java:168)
	at junit.framework.TestCase.runBare(TestCase.java:134)
	at
org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69
)
	at
org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.ac
cess$001(AbstractAnnotationAwareTransactionalTests.java:47)
	at
org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests$1.
run(AbstractAnnotationAwareTransactionalTests.java:115)
	at
org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.ru
nTest(AbstractAnnotationAwareTransactionalTests.java:180)
	at
org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.ru
nTestTimed(AbstractAnnotationAwareTransactionalTests.java:153)
	at
org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.ru
nBare(AbstractAnnotationAwareTransactionalTests.java:111)
	at
org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:159)
	at
org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:239)
	at junit.framework.TestResult$1.protect(TestResult.java:110)
	at junit.framework.TestResult.runProtected(TestResult.java:128)
	at junit.framework.TestResult.run(TestResult.java:113)
	at junit.framework.TestCase.run(TestCase.java:124)
	at junit.framework.TestSuite.runTest(TestSuite.java:232)
	at junit.framework.TestSuite.run(TestSuite.java:227)
	at
org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
	at
org.junit.internal.runners.CompositeRunner.run(CompositeRunner.java:29)
	at
org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java
:42)
	at
org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunne
r.java:34)
	at
org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
	at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReferen
ce.java:38)
	at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner
.java:460)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner
.java:673)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java
:386)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.jav
a:196)


Re: Cascade question (ver 0.96)

Posted by Craig L Russell <Cr...@Sun.COM>.
I think maybe the issue is a simple usage issue.

If you already have a persistent StoreType instance, and you store a  
reference to it in a new Store instance, then when you persist the  
Store, the StoreType instance is simply used to provide a foreign key  
in the database.

And if you have a detached Store instance and set the relationship to  
a detached StoreType instance, when you merge, the StoreType is again  
used as a reference to provide the foreign key.

Are you using a persistent or detached StoreType as the reference in  
your Store? Because if you're using a new StoreType instance and  
there is already an instance of StoreType in the database, you will  
get an error.

Craig

On Apr 24, 2007, at 11:53 AM, Phill Moran wrote:

> That is my concern I should only have one copy of a storeType for  
> many Store
> entries. So if I add a record on the "one" side (Store) of a one-to- 
> many"
> relationship and have a relation set to an existing "Many" side  
> (StoreType) I
> don't want a new "Many" side record created as this would be a  
> duplicate.
>
> It seems unless I specify a cascade.Merge/Persist on the  
> relationship field I
> cannot persist a new Store with a relationship to a StoreType  
> record even though
> the StoreType record exists and does not need merging/persisting.  
> What I am
> looking to do is only persist data on the "one" side of the  
> relationship and
> never the "Many".
>
> Make sense or just crazy coding?
>
> Phill
> -----Original Message-----
> From: Marc Prud'hommeaux [mailto:mprudhomapache@gmail.com] On  
> Behalf Of Marc
> Prud'hommeaux
> Sent: April 24, 2007 2:23 PM
> To: open-jpa-dev@incubator.apache.org
> Subject: Re: Cascade question (ver 0.96)
>
> Phill-
>
>> The behaviour I am looking for is simply persist the relation (i.e.
>> the link field)
>
> If you specify cascade=MERGE on the StoreType relation field, and  
> you merge a
> Store instance for which the StoreType relation doesn't already  
> exists, does it
> not persist the field as if it were new?
> That's the behavior I would expect...
>
>
>
> On Apr 23, 2007, at 9:55 PM, Phill Moran wrote:
>
>> Here is a scenario that shows odd behaviour, I want to see if it is
>> expected or not. The docs are not clear on it
>>
>> If I have a many to one relationship for objects Store to Store Type
>> and I create a new Store and assign it to an existing Store type does
>> this relationship have to have cascasdeType.persist set when I  
>> issue a
>> merge on the new Store? I had recently removed this as I thought I  
>> did
>> not want to create a duplicate Store Type whenever I added a new
>> Store. It seems OpenJPA throws the attached exception when I only  
>> have
>> CascadeType.Refresh set.
>> Alternatively, this could just be a poorly worded exception/
>> documentation meaning OpenJPA would check for the existence of this
>> Store Type and not actually persist it if it exists. The behaviour I
>> am looking for is simply persist the relation (i.e. the link field)
>>
>> Thanks,
>> 	Phill
>> <4|false|0.9.6-incubating>
>> org.apache.openjpa.persistence.ArgumentException:
>> Encountered new object
>> "ca.BidSpec.emall.categories.Category-105603b-508b-9c6-00f4-4031ba642 
>> 9
>> e3:0" in
>> persistent field "ca.BidSpec.emall.stores.Store.type" of managed
>> object "ca.BidSpec.emall.stores.Store@56182f" during attach.   
>> However,
>> this field does not allow cascade attach.  You cannot attach a
>> reference to a new object without cascading.
>> FailedObject:
>> ca.BidSpec.emall.categories.Category-105603b-508b-9c6-00f4-4031ba6429 
>> e
>> 3:0
>> 	at
>> org.apache.openjpa.kernel.AttachStrategy.getReference
>> (AttachStrategy.java:272)
>> 	at
>> org.apache.openjpa.kernel.AttachStrategy.attachField
>> (AttachStrategy.java:189)
>> 	at
>> org.apache.openjpa.kernel.VersionAttachStrategy.attach
>> (VersionAttachStrategy.jav
>> a:130)
>> 	at
>> org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java: 
>> 236)
>> 	at org.apache.openjpa.kernel.AttachManager.attach
>> (AttachManager.java:97)
>> 	at org.apache.openjpa.kernel.BrokerImpl.attach(BrokerImpl.java:3124)
>> 	at
>> org.apache.openjpa.kernel.DelegatingBroker.attach
>> (DelegatingBroker.java:1120)
>> 	at
>> org.apache.openjpa.persistence.EntityManagerImpl.merge
>> (EntityManagerImpl.java:59
>> 1)
>> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>> 	at
>> sun.reflect.NativeMethodAccessorImpl.invoke
>> (NativeMethodAccessorImpl.java:39)
>> 	at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke
>> (DelegatingMethodAccessorImpl.jav
>> a:25)
>> 	at java.lang.reflect.Method.invoke(Method.java:597)
>> 	at
>> org.springframework.orm.jpa.ExtendedEntityManagerCreator
>> $ExtendedEntityManagerIn
>> vocationHandler.invoke(ExtendedEntityManagerCreator.java:283)
>> 	at $Proxy37.merge(Unknown Source)
>> 	at
>> ca.BidSpec.emall.persistence.JPAPersistenceFactory.merge
>> (JPAPersistenceFactory.j
>> ava:95)
>> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>> 	at
>> sun.reflect.NativeMethodAccessorImpl.invoke
>> (NativeMethodAccessorImpl.java:39)
>> 	at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke
>> (DelegatingMethodAccessorImpl.jav
>> a:25)
>> 	at java.lang.reflect.Method.invoke(Method.java:597)
>> 	at
>> org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflecti 
>> o
>> n(AopUtils
>> .java:304)
>> 	at
>> org.springframework.aop.framework.JdkDynamicAopProxy.invoke
>> (JdkDynamicAopProxy.j
>> ava:197)
>> 	at $Proxy35.merge(Unknown Source)
>> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>> 	at
>> sun.reflect.NativeMethodAccessorImpl.invoke
>> (NativeMethodAccessorImpl.java:39)
>> 	at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke
>> (DelegatingMethodAccessorImpl.jav
>> a:25)
>> 	at java.lang.reflect.Method.invoke(Method.java:597)
>> 	at
>> org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflecti 
>> o
>> n(AopUtils
>> .java:304)
>> 	at
>> org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJo 
>> i
>> npoint(Ref
>> lectiveMethodInvocation.java:172)
>> 	at
>> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed
>> (ReflectiveM
>> ethodInvocation.java:139)
>> 	at
>> org.springframework.transaction.interceptor.TransactionInterceptor.in 
>> v
>> oke(Transa
>> ctionInterceptor.java:107)
>> 	at
>> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed
>> (ReflectiveM
>> ethodInvocation.java:161)
>> 	at
>> org.springframework.aop.framework.JdkDynamicAopProxy.invoke
>> (JdkDynamicAopProxy.j
>> ava:203)
>> 	at $Proxy35.merge(Unknown Source)
>> 	at
>> ca.BidSpec.testing.emall.StoreFactoryTest.testSave
>> (StoreFactoryTest.java:69)
>> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>> 	at
>> sun.reflect.NativeMethodAccessorImpl.invoke
>> (NativeMethodAccessorImpl.java:39)
>> 	at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke
>> (DelegatingMethodAccessorImpl.jav
>> a:25)
>> 	at java.lang.reflect.Method.invoke(Method.java:597)
>> 	at junit.framework.TestCase.runTest(TestCase.java:168)
>> 	at junit.framework.TestCase.runBare(TestCase.java:134)
>> 	at
>> org.springframework.test.ConditionalTestCase.runBare
>> (ConditionalTestCase.java:69
>> )
>> 	at
>> org.springframework.test.annotation.AbstractAnnotationAwareTransactio 
>> n
>> alTests.ac
>> cess$001(AbstractAnnotationAwareTransactionalTests.java:47)
>> 	at
>> org.springframework.test.annotation.AbstractAnnotationAwareTransactio 
>> n
>> alTests$1.
>> run(AbstractAnnotationAwareTransactionalTests.java:115)
>> 	at
>> org.springframework.test.annotation.AbstractAnnotationAwareTransactio 
>> n
>> alTests.ru
>> nTest(AbstractAnnotationAwareTransactionalTests.java:180)
>> 	at
>> org.springframework.test.annotation.AbstractAnnotationAwareTransactio 
>> n
>> alTests.ru
>> nTestTimed(AbstractAnnotationAwareTransactionalTests.java:153)
>> 	at
>> org.springframework.test.annotation.AbstractAnnotationAwareTransactio 
>> n
>> alTests.ru
>> nBare(AbstractAnnotationAwareTransactionalTests.java:111)
>> 	at
>> org.springframework.test.jpa.AbstractJpaTests.runBare
>> (AbstractJpaTests.java:159)
>> 	at
>> org.springframework.test.jpa.AbstractJpaTests.runBare
>> (AbstractJpaTests.java:239)
>> 	at junit.framework.TestResult$1.protect(TestResult.java:110)
>> 	at junit.framework.TestResult.runProtected(TestResult.java:128)
>> 	at junit.framework.TestResult.run(TestResult.java:113)
>> 	at junit.framework.TestCase.run(TestCase.java:124)
>> 	at junit.framework.TestSuite.runTest(TestSuite.java:232)
>> 	at junit.framework.TestSuite.run(TestSuite.java:227)
>> 	at
>> org.junit.internal.runners.OldTestClassRunner.run
>> (OldTestClassRunner.java:35)
>> 	at
>> org.junit.internal.runners.CompositeRunner.run(CompositeRunner.java:
>> 29)
>> 	at
>> org.junit.internal.runners.TestClassRunner$1.runUnprotected
>> (TestClassRunner.java
>> :42)
>> 	at
>> org.junit.internal.runners.BeforeAndAfterRunner.runProtected
>> (BeforeAndAfterRunne
>> r.java:34)
>> 	at
>> org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:
>> 52)
>> 	at
>> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
>> (JUnit4TestReferen
>> ce.java:38)
>> 	at
>> org.eclipse.jdt.internal.junit.runner.TestExecution.run
>> (TestExecution.java:38)
>> 	at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
>> (RemoteTestRunner
>> .java:460)
>> 	at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
>> (RemoteTestRunner
>> .java:673)
>> 	at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
>> (RemoteTestRunner.java
>> :386)
>> 	at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
>> (RemoteTestRunner.jav
>> a:196)
>>
>

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: Cascade question (ver 0.96)

Posted by Marina Vatkina <Ma...@Sun.COM>.
Phill,

This is what the spec says:

3.2.3 Synchronization to the Database

" For any entity Y referenced by a relationship from X, where the relationship 
to Y has not been annotated with the cascade element value cascade=PERSIST or 
cascade= ALL:
...
     " If Y is detached, the semantics depend upon the ownership of the 
relationship. If X owns the relationship, any changes to the relationship are 
synchronized with the database; otherwise, if Y owns the relationships, the 
behavior is undefined.

HTH,
-marina

Phill Moran wrote:
> That is my concern I should only have one copy of a storeType for many Store
> entries. So if I add a record on the "one" side (Store) of a one-to-many"
> relationship and have a relation set to an existing "Many" side (StoreType) I
> don't want a new "Many" side record created as this would be a duplicate.
> 
> It seems unless I specify a cascade.Merge/Persist on the relationship field I
> cannot persist a new Store with a relationship to a StoreType record even though
> the StoreType record exists and does not need merging/persisting. What I am
> looking to do is only persist data on the "one" side of the relationship and
> never the "Many". 
> 
> Make sense or just crazy coding?
> 
> Phill
> -----Original Message-----
> From: Marc Prud'hommeaux [mailto:mprudhomapache@gmail.com] On Behalf Of Marc
> Prud'hommeaux
> Sent: April 24, 2007 2:23 PM
> To: open-jpa-dev@incubator.apache.org
> Subject: Re: Cascade question (ver 0.96)
> 
> Phill-
> 
> 
>>The behaviour I am looking for is simply persist the relation (i.e. 
>>the link field)
> 
> 
> If you specify cascade=MERGE on the StoreType relation field, and you merge a
> Store instance for which the StoreType relation doesn't already exists, does it
> not persist the field as if it were new?  
> That's the behavior I would expect...
> 
> 
> 
> On Apr 23, 2007, at 9:55 PM, Phill Moran wrote:
> 
> 
>>Here is a scenario that shows odd behaviour, I want to see if it is 
>>expected or not. The docs are not clear on it
>>
>>If I have a many to one relationship for objects Store to Store Type 
>>and I create a new Store and assign it to an existing Store type does 
>>this relationship have to have cascasdeType.persist set when I issue a 
>>merge on the new Store? I had recently removed this as I thought I did 
>>not want to create a duplicate Store Type whenever I added a new  
>>Store. It seems OpenJPA throws the attached exception when I only have 
>>CascadeType.Refresh set.
>>Alternatively, this could just be a poorly worded exception/ 
>>documentation meaning OpenJPA would check for the existence of this 
>>Store Type and not actually persist it if it exists. The behaviour I 
>>am looking for is simply persist the relation (i.e. the link field)
>>
>>Thanks,
>>	Phill
>><4|false|0.9.6-incubating>
>>org.apache.openjpa.persistence.ArgumentException:
>>Encountered new object
>>"ca.BidSpec.emall.categories.Category-105603b-508b-9c6-00f4-4031ba6429
>>e3:0" in
>>persistent field "ca.BidSpec.emall.stores.Store.type" of managed 
>>object "ca.BidSpec.emall.stores.Store@56182f" during attach.  However, 
>>this field does not allow cascade attach.  You cannot attach a 
>>reference to a new object without cascading.
>>FailedObject:
>>ca.BidSpec.emall.categories.Category-105603b-508b-9c6-00f4-4031ba6429e
>>3:0
>>	at
>>org.apache.openjpa.kernel.AttachStrategy.getReference
>>(AttachStrategy.java:272)
>>	at
>>org.apache.openjpa.kernel.AttachStrategy.attachField
>>(AttachStrategy.java:189)
>>	at
>>org.apache.openjpa.kernel.VersionAttachStrategy.attach
>>(VersionAttachStrategy.jav
>>a:130)
>>	at
>>org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:236)
>>	at org.apache.openjpa.kernel.AttachManager.attach
>>(AttachManager.java:97)
>>	at org.apache.openjpa.kernel.BrokerImpl.attach(BrokerImpl.java:3124)
>>	at
>>org.apache.openjpa.kernel.DelegatingBroker.attach
>>(DelegatingBroker.java:1120)
>>	at
>>org.apache.openjpa.persistence.EntityManagerImpl.merge
>>(EntityManagerImpl.java:59
>>1)
>>	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>	at
>>sun.reflect.NativeMethodAccessorImpl.invoke
>>(NativeMethodAccessorImpl.java:39)
>>	at
>>sun.reflect.DelegatingMethodAccessorImpl.invoke
>>(DelegatingMethodAccessorImpl.jav
>>a:25)
>>	at java.lang.reflect.Method.invoke(Method.java:597)
>>	at
>>org.springframework.orm.jpa.ExtendedEntityManagerCreator
>>$ExtendedEntityManagerIn
>>vocationHandler.invoke(ExtendedEntityManagerCreator.java:283)
>>	at $Proxy37.merge(Unknown Source)
>>	at
>>ca.BidSpec.emall.persistence.JPAPersistenceFactory.merge
>>(JPAPersistenceFactory.j
>>ava:95)
>>	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>	at
>>sun.reflect.NativeMethodAccessorImpl.invoke
>>(NativeMethodAccessorImpl.java:39)
>>	at
>>sun.reflect.DelegatingMethodAccessorImpl.invoke
>>(DelegatingMethodAccessorImpl.jav
>>a:25)
>>	at java.lang.reflect.Method.invoke(Method.java:597)
>>	at
>>org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflectio
>>n(AopUtils
>>.java:304)
>>	at
>>org.springframework.aop.framework.JdkDynamicAopProxy.invoke
>>(JdkDynamicAopProxy.j
>>ava:197)
>>	at $Proxy35.merge(Unknown Source)
>>	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>	at
>>sun.reflect.NativeMethodAccessorImpl.invoke
>>(NativeMethodAccessorImpl.java:39)
>>	at
>>sun.reflect.DelegatingMethodAccessorImpl.invoke
>>(DelegatingMethodAccessorImpl.jav
>>a:25)
>>	at java.lang.reflect.Method.invoke(Method.java:597)
>>	at
>>org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflectio
>>n(AopUtils
>>.java:304)
>>	at
>>org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoi
>>npoint(Ref
>>lectiveMethodInvocation.java:172)
>>	at
>>org.springframework.aop.framework.ReflectiveMethodInvocation.proceed
>>(ReflectiveM
>>ethodInvocation.java:139)
>>	at
>>org.springframework.transaction.interceptor.TransactionInterceptor.inv
>>oke(Transa
>>ctionInterceptor.java:107)
>>	at
>>org.springframework.aop.framework.ReflectiveMethodInvocation.proceed
>>(ReflectiveM
>>ethodInvocation.java:161)
>>	at
>>org.springframework.aop.framework.JdkDynamicAopProxy.invoke
>>(JdkDynamicAopProxy.j
>>ava:203)
>>	at $Proxy35.merge(Unknown Source)
>>	at
>>ca.BidSpec.testing.emall.StoreFactoryTest.testSave
>>(StoreFactoryTest.java:69)
>>	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>	at
>>sun.reflect.NativeMethodAccessorImpl.invoke
>>(NativeMethodAccessorImpl.java:39)
>>	at
>>sun.reflect.DelegatingMethodAccessorImpl.invoke
>>(DelegatingMethodAccessorImpl.jav
>>a:25)
>>	at java.lang.reflect.Method.invoke(Method.java:597)
>>	at junit.framework.TestCase.runTest(TestCase.java:168)
>>	at junit.framework.TestCase.runBare(TestCase.java:134)
>>	at
>>org.springframework.test.ConditionalTestCase.runBare
>>(ConditionalTestCase.java:69
>>)
>>	at
>>org.springframework.test.annotation.AbstractAnnotationAwareTransaction
>>alTests.ac
>>cess$001(AbstractAnnotationAwareTransactionalTests.java:47)
>>	at
>>org.springframework.test.annotation.AbstractAnnotationAwareTransaction
>>alTests$1.
>>run(AbstractAnnotationAwareTransactionalTests.java:115)
>>	at
>>org.springframework.test.annotation.AbstractAnnotationAwareTransaction
>>alTests.ru
>>nTest(AbstractAnnotationAwareTransactionalTests.java:180)
>>	at
>>org.springframework.test.annotation.AbstractAnnotationAwareTransaction
>>alTests.ru
>>nTestTimed(AbstractAnnotationAwareTransactionalTests.java:153)
>>	at
>>org.springframework.test.annotation.AbstractAnnotationAwareTransaction
>>alTests.ru
>>nBare(AbstractAnnotationAwareTransactionalTests.java:111)
>>	at
>>org.springframework.test.jpa.AbstractJpaTests.runBare
>>(AbstractJpaTests.java:159)
>>	at
>>org.springframework.test.jpa.AbstractJpaTests.runBare
>>(AbstractJpaTests.java:239)
>>	at junit.framework.TestResult$1.protect(TestResult.java:110)
>>	at junit.framework.TestResult.runProtected(TestResult.java:128)
>>	at junit.framework.TestResult.run(TestResult.java:113)
>>	at junit.framework.TestCase.run(TestCase.java:124)
>>	at junit.framework.TestSuite.runTest(TestSuite.java:232)
>>	at junit.framework.TestSuite.run(TestSuite.java:227)
>>	at
>>org.junit.internal.runners.OldTestClassRunner.run
>>(OldTestClassRunner.java:35)
>>	at
>>org.junit.internal.runners.CompositeRunner.run(CompositeRunner.java: 
>>29)
>>	at
>>org.junit.internal.runners.TestClassRunner$1.runUnprotected
>>(TestClassRunner.java
>>:42)
>>	at
>>org.junit.internal.runners.BeforeAndAfterRunner.runProtected
>>(BeforeAndAfterRunne
>>r.java:34)
>>	at
>>org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java: 
>>52)
>>	at
>>org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
>>(JUnit4TestReferen
>>ce.java:38)
>>	at
>>org.eclipse.jdt.internal.junit.runner.TestExecution.run
>>(TestExecution.java:38)
>>	at
>>org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
>>(RemoteTestRunner
>>.java:460)
>>	at
>>org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
>>(RemoteTestRunner
>>.java:673)
>>	at
>>org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
>>(RemoteTestRunner.java
>>:386)
>>	at
>>org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
>>(RemoteTestRunner.jav
>>a:196)
>>
> 
> 

RE: Cascade question (ver 0.96)

Posted by Phill Moran <pj...@rogers.com>.
That is my concern I should only have one copy of a storeType for many Store
entries. So if I add a record on the "one" side (Store) of a one-to-many"
relationship and have a relation set to an existing "Many" side (StoreType) I
don't want a new "Many" side record created as this would be a duplicate.

It seems unless I specify a cascade.Merge/Persist on the relationship field I
cannot persist a new Store with a relationship to a StoreType record even though
the StoreType record exists and does not need merging/persisting. What I am
looking to do is only persist data on the "one" side of the relationship and
never the "Many". 

Make sense or just crazy coding?

Phill
-----Original Message-----
From: Marc Prud'hommeaux [mailto:mprudhomapache@gmail.com] On Behalf Of Marc
Prud'hommeaux
Sent: April 24, 2007 2:23 PM
To: open-jpa-dev@incubator.apache.org
Subject: Re: Cascade question (ver 0.96)

Phill-

> The behaviour I am looking for is simply persist the relation (i.e. 
> the link field)

If you specify cascade=MERGE on the StoreType relation field, and you merge a
Store instance for which the StoreType relation doesn't already exists, does it
not persist the field as if it were new?  
That's the behavior I would expect...



On Apr 23, 2007, at 9:55 PM, Phill Moran wrote:

> Here is a scenario that shows odd behaviour, I want to see if it is 
> expected or not. The docs are not clear on it
>
> If I have a many to one relationship for objects Store to Store Type 
> and I create a new Store and assign it to an existing Store type does 
> this relationship have to have cascasdeType.persist set when I issue a 
> merge on the new Store? I had recently removed this as I thought I did 
> not want to create a duplicate Store Type whenever I added a new  
> Store. It seems OpenJPA throws the attached exception when I only have 
> CascadeType.Refresh set.
> Alternatively, this could just be a poorly worded exception/ 
> documentation meaning OpenJPA would check for the existence of this 
> Store Type and not actually persist it if it exists. The behaviour I 
> am looking for is simply persist the relation (i.e. the link field)
>
> Thanks,
> 	Phill
> <4|false|0.9.6-incubating>
> org.apache.openjpa.persistence.ArgumentException:
> Encountered new object
> "ca.BidSpec.emall.categories.Category-105603b-508b-9c6-00f4-4031ba6429
> e3:0" in
> persistent field "ca.BidSpec.emall.stores.Store.type" of managed 
> object "ca.BidSpec.emall.stores.Store@56182f" during attach.  However, 
> this field does not allow cascade attach.  You cannot attach a 
> reference to a new object without cascading.
> FailedObject:
> ca.BidSpec.emall.categories.Category-105603b-508b-9c6-00f4-4031ba6429e
> 3:0
> 	at
> org.apache.openjpa.kernel.AttachStrategy.getReference
> (AttachStrategy.java:272)
> 	at
> org.apache.openjpa.kernel.AttachStrategy.attachField
> (AttachStrategy.java:189)
> 	at
> org.apache.openjpa.kernel.VersionAttachStrategy.attach
> (VersionAttachStrategy.jav
> a:130)
> 	at
> org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:236)
> 	at org.apache.openjpa.kernel.AttachManager.attach
> (AttachManager.java:97)
> 	at org.apache.openjpa.kernel.BrokerImpl.attach(BrokerImpl.java:3124)
> 	at
> org.apache.openjpa.kernel.DelegatingBroker.attach
> (DelegatingBroker.java:1120)
> 	at
> org.apache.openjpa.persistence.EntityManagerImpl.merge
> (EntityManagerImpl.java:59
> 1)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at
> sun.reflect.NativeMethodAccessorImpl.invoke
> (NativeMethodAccessorImpl.java:39)
> 	at
> sun.reflect.DelegatingMethodAccessorImpl.invoke
> (DelegatingMethodAccessorImpl.jav
> a:25)
> 	at java.lang.reflect.Method.invoke(Method.java:597)
> 	at
> org.springframework.orm.jpa.ExtendedEntityManagerCreator
> $ExtendedEntityManagerIn
> vocationHandler.invoke(ExtendedEntityManagerCreator.java:283)
> 	at $Proxy37.merge(Unknown Source)
> 	at
> ca.BidSpec.emall.persistence.JPAPersistenceFactory.merge
> (JPAPersistenceFactory.j
> ava:95)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at
> sun.reflect.NativeMethodAccessorImpl.invoke
> (NativeMethodAccessorImpl.java:39)
> 	at
> sun.reflect.DelegatingMethodAccessorImpl.invoke
> (DelegatingMethodAccessorImpl.jav
> a:25)
> 	at java.lang.reflect.Method.invoke(Method.java:597)
> 	at
> org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflectio
> n(AopUtils
> .java:304)
> 	at
> org.springframework.aop.framework.JdkDynamicAopProxy.invoke
> (JdkDynamicAopProxy.j
> ava:197)
> 	at $Proxy35.merge(Unknown Source)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at
> sun.reflect.NativeMethodAccessorImpl.invoke
> (NativeMethodAccessorImpl.java:39)
> 	at
> sun.reflect.DelegatingMethodAccessorImpl.invoke
> (DelegatingMethodAccessorImpl.jav
> a:25)
> 	at java.lang.reflect.Method.invoke(Method.java:597)
> 	at
> org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflectio
> n(AopUtils
> .java:304)
> 	at
> org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoi
> npoint(Ref
> lectiveMethodInvocation.java:172)
> 	at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed
> (ReflectiveM
> ethodInvocation.java:139)
> 	at
> org.springframework.transaction.interceptor.TransactionInterceptor.inv
> oke(Transa
> ctionInterceptor.java:107)
> 	at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed
> (ReflectiveM
> ethodInvocation.java:161)
> 	at
> org.springframework.aop.framework.JdkDynamicAopProxy.invoke
> (JdkDynamicAopProxy.j
> ava:203)
> 	at $Proxy35.merge(Unknown Source)
> 	at
> ca.BidSpec.testing.emall.StoreFactoryTest.testSave
> (StoreFactoryTest.java:69)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at
> sun.reflect.NativeMethodAccessorImpl.invoke
> (NativeMethodAccessorImpl.java:39)
> 	at
> sun.reflect.DelegatingMethodAccessorImpl.invoke
> (DelegatingMethodAccessorImpl.jav
> a:25)
> 	at java.lang.reflect.Method.invoke(Method.java:597)
> 	at junit.framework.TestCase.runTest(TestCase.java:168)
> 	at junit.framework.TestCase.runBare(TestCase.java:134)
> 	at
> org.springframework.test.ConditionalTestCase.runBare
> (ConditionalTestCase.java:69
> )
> 	at
> org.springframework.test.annotation.AbstractAnnotationAwareTransaction
> alTests.ac
> cess$001(AbstractAnnotationAwareTransactionalTests.java:47)
> 	at
> org.springframework.test.annotation.AbstractAnnotationAwareTransaction
> alTests$1.
> run(AbstractAnnotationAwareTransactionalTests.java:115)
> 	at
> org.springframework.test.annotation.AbstractAnnotationAwareTransaction
> alTests.ru
> nTest(AbstractAnnotationAwareTransactionalTests.java:180)
> 	at
> org.springframework.test.annotation.AbstractAnnotationAwareTransaction
> alTests.ru
> nTestTimed(AbstractAnnotationAwareTransactionalTests.java:153)
> 	at
> org.springframework.test.annotation.AbstractAnnotationAwareTransaction
> alTests.ru
> nBare(AbstractAnnotationAwareTransactionalTests.java:111)
> 	at
> org.springframework.test.jpa.AbstractJpaTests.runBare
> (AbstractJpaTests.java:159)
> 	at
> org.springframework.test.jpa.AbstractJpaTests.runBare
> (AbstractJpaTests.java:239)
> 	at junit.framework.TestResult$1.protect(TestResult.java:110)
> 	at junit.framework.TestResult.runProtected(TestResult.java:128)
> 	at junit.framework.TestResult.run(TestResult.java:113)
> 	at junit.framework.TestCase.run(TestCase.java:124)
> 	at junit.framework.TestSuite.runTest(TestSuite.java:232)
> 	at junit.framework.TestSuite.run(TestSuite.java:227)
> 	at
> org.junit.internal.runners.OldTestClassRunner.run
> (OldTestClassRunner.java:35)
> 	at
> org.junit.internal.runners.CompositeRunner.run(CompositeRunner.java: 
> 29)
> 	at
> org.junit.internal.runners.TestClassRunner$1.runUnprotected
> (TestClassRunner.java
> :42)
> 	at
> org.junit.internal.runners.BeforeAndAfterRunner.runProtected
> (BeforeAndAfterRunne
> r.java:34)
> 	at
> org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java: 
> 52)
> 	at
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
> (JUnit4TestReferen
> ce.java:38)
> 	at
> org.eclipse.jdt.internal.junit.runner.TestExecution.run
> (TestExecution.java:38)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
> (RemoteTestRunner
> .java:460)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
> (RemoteTestRunner
> .java:673)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
> (RemoteTestRunner.java
> :386)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
> (RemoteTestRunner.jav
> a:196)
>


Re: Cascade question (ver 0.96)

Posted by Marc Prud'hommeaux <mp...@apache.org>.
Phill-

> The behaviour I am looking for is simply
> persist the relation (i.e. the link field)

If you specify cascade=MERGE on the StoreType relation field, and you  
merge a Store instance for which the StoreType relation doesn't  
already exists, does it not persist the field as if it were new?  
That's the behavior I would expect...



On Apr 23, 2007, at 9:55 PM, Phill Moran wrote:

> Here is a scenario that shows odd behaviour, I want to see if it is  
> expected or
> not. The docs are not clear on it
>
> If I have a many to one relationship for objects Store to Store  
> Type and I
> create a new Store and assign it to an existing Store type does this
> relationship have to have cascasdeType.persist set when I issue a  
> merge on the
> new Store? I had recently removed this as I thought I did not want  
> to create a
> duplicate Store Type whenever I added a new  Store. It seems  
> OpenJPA throws the
> attached exception when I only have CascadeType.Refresh set.
> Alternatively, this could just be a poorly worded exception/ 
> documentation
> meaning OpenJPA would check for the existence of this Store Type  
> and not
> actually persist it if it exists. The behaviour I am looking for is  
> simply
> persist the relation (i.e. the link field)
>
> Thanks,
> 	Phill
> <4|false|0.9.6-incubating>  
> org.apache.openjpa.persistence.ArgumentException:
> Encountered new object
> "ca.BidSpec.emall.categories.Category-105603b-508b-9c6-00f4-4031ba6429 
> e3:0" in
> persistent field "ca.BidSpec.emall.stores.Store.type" of managed  
> object
> "ca.BidSpec.emall.stores.Store@56182f" during attach.  However,  
> this field does
> not allow cascade attach.  You cannot attach a reference to a new  
> object without
> cascading.
> FailedObject:
> ca.BidSpec.emall.categories.Category-105603b-508b-9c6-00f4-4031ba6429e 
> 3:0
> 	at
> org.apache.openjpa.kernel.AttachStrategy.getReference 
> (AttachStrategy.java:272)
> 	at
> org.apache.openjpa.kernel.AttachStrategy.attachField 
> (AttachStrategy.java:189)
> 	at
> org.apache.openjpa.kernel.VersionAttachStrategy.attach 
> (VersionAttachStrategy.jav
> a:130)
> 	at
> org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:236)
> 	at org.apache.openjpa.kernel.AttachManager.attach 
> (AttachManager.java:97)
> 	at org.apache.openjpa.kernel.BrokerImpl.attach(BrokerImpl.java:3124)
> 	at
> org.apache.openjpa.kernel.DelegatingBroker.attach 
> (DelegatingBroker.java:1120)
> 	at
> org.apache.openjpa.persistence.EntityManagerImpl.merge 
> (EntityManagerImpl.java:59
> 1)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at
> sun.reflect.NativeMethodAccessorImpl.invoke 
> (NativeMethodAccessorImpl.java:39)
> 	at
> sun.reflect.DelegatingMethodAccessorImpl.invoke 
> (DelegatingMethodAccessorImpl.jav
> a:25)
> 	at java.lang.reflect.Method.invoke(Method.java:597)
> 	at
> org.springframework.orm.jpa.ExtendedEntityManagerCreator 
> $ExtendedEntityManagerIn
> vocationHandler.invoke(ExtendedEntityManagerCreator.java:283)
> 	at $Proxy37.merge(Unknown Source)
> 	at
> ca.BidSpec.emall.persistence.JPAPersistenceFactory.merge 
> (JPAPersistenceFactory.j
> ava:95)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at
> sun.reflect.NativeMethodAccessorImpl.invoke 
> (NativeMethodAccessorImpl.java:39)
> 	at
> sun.reflect.DelegatingMethodAccessorImpl.invoke 
> (DelegatingMethodAccessorImpl.jav
> a:25)
> 	at java.lang.reflect.Method.invoke(Method.java:597)
> 	at
> org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflectio 
> n(AopUtils
> .java:304)
> 	at
> org.springframework.aop.framework.JdkDynamicAopProxy.invoke 
> (JdkDynamicAopProxy.j
> ava:197)
> 	at $Proxy35.merge(Unknown Source)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at
> sun.reflect.NativeMethodAccessorImpl.invoke 
> (NativeMethodAccessorImpl.java:39)
> 	at
> sun.reflect.DelegatingMethodAccessorImpl.invoke 
> (DelegatingMethodAccessorImpl.jav
> a:25)
> 	at java.lang.reflect.Method.invoke(Method.java:597)
> 	at
> org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflectio 
> n(AopUtils
> .java:304)
> 	at
> org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoi 
> npoint(Ref
> lectiveMethodInvocation.java:172)
> 	at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed 
> (ReflectiveM
> ethodInvocation.java:139)
> 	at
> org.springframework.transaction.interceptor.TransactionInterceptor.inv 
> oke(Transa
> ctionInterceptor.java:107)
> 	at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed 
> (ReflectiveM
> ethodInvocation.java:161)
> 	at
> org.springframework.aop.framework.JdkDynamicAopProxy.invoke 
> (JdkDynamicAopProxy.j
> ava:203)
> 	at $Proxy35.merge(Unknown Source)
> 	at
> ca.BidSpec.testing.emall.StoreFactoryTest.testSave 
> (StoreFactoryTest.java:69)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at
> sun.reflect.NativeMethodAccessorImpl.invoke 
> (NativeMethodAccessorImpl.java:39)
> 	at
> sun.reflect.DelegatingMethodAccessorImpl.invoke 
> (DelegatingMethodAccessorImpl.jav
> a:25)
> 	at java.lang.reflect.Method.invoke(Method.java:597)
> 	at junit.framework.TestCase.runTest(TestCase.java:168)
> 	at junit.framework.TestCase.runBare(TestCase.java:134)
> 	at
> org.springframework.test.ConditionalTestCase.runBare 
> (ConditionalTestCase.java:69
> )
> 	at
> org.springframework.test.annotation.AbstractAnnotationAwareTransaction 
> alTests.ac
> cess$001(AbstractAnnotationAwareTransactionalTests.java:47)
> 	at
> org.springframework.test.annotation.AbstractAnnotationAwareTransaction 
> alTests$1.
> run(AbstractAnnotationAwareTransactionalTests.java:115)
> 	at
> org.springframework.test.annotation.AbstractAnnotationAwareTransaction 
> alTests.ru
> nTest(AbstractAnnotationAwareTransactionalTests.java:180)
> 	at
> org.springframework.test.annotation.AbstractAnnotationAwareTransaction 
> alTests.ru
> nTestTimed(AbstractAnnotationAwareTransactionalTests.java:153)
> 	at
> org.springframework.test.annotation.AbstractAnnotationAwareTransaction 
> alTests.ru
> nBare(AbstractAnnotationAwareTransactionalTests.java:111)
> 	at
> org.springframework.test.jpa.AbstractJpaTests.runBare 
> (AbstractJpaTests.java:159)
> 	at
> org.springframework.test.jpa.AbstractJpaTests.runBare 
> (AbstractJpaTests.java:239)
> 	at junit.framework.TestResult$1.protect(TestResult.java:110)
> 	at junit.framework.TestResult.runProtected(TestResult.java:128)
> 	at junit.framework.TestResult.run(TestResult.java:113)
> 	at junit.framework.TestCase.run(TestCase.java:124)
> 	at junit.framework.TestSuite.runTest(TestSuite.java:232)
> 	at junit.framework.TestSuite.run(TestSuite.java:227)
> 	at
> org.junit.internal.runners.OldTestClassRunner.run 
> (OldTestClassRunner.java:35)
> 	at
> org.junit.internal.runners.CompositeRunner.run(CompositeRunner.java: 
> 29)
> 	at
> org.junit.internal.runners.TestClassRunner$1.runUnprotected 
> (TestClassRunner.java
> :42)
> 	at
> org.junit.internal.runners.BeforeAndAfterRunner.runProtected 
> (BeforeAndAfterRunne
> r.java:34)
> 	at
> org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java: 
> 52)
> 	at
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run 
> (JUnit4TestReferen
> ce.java:38)
> 	at
> org.eclipse.jdt.internal.junit.runner.TestExecution.run 
> (TestExecution.java:38)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests 
> (RemoteTestRunner
> .java:460)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests 
> (RemoteTestRunner
> .java:673)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run 
> (RemoteTestRunner.java
> :386)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main 
> (RemoteTestRunner.jav
> a:196)
>


Re: Funky Query from entityManager.find(Class,id)

Posted by Marc Prud'hommeaux <mp...@apache.org>.
Phill-

On Apr 24, 2007, at 11:42 AM, Phill Moran wrote:

> Maybe I am wrong here let me ask a clarifying question. When  
> executing a find on
> an object that has a related object (like this example) does  
> OpenJPA create one
> select statement and grab all fields for both objects then parse  
> the results to
> build both objects?

Whenever possible, yes. For all fetch=EAGER relations (which  
@OneToOne and @ManyToOne default to), OpenJPA will try to eagerly  
select all of the instances in as few statements as possible. Most of  
the time it will just be a single statement.

This is discussed in some detail at:

   http://incubator.apache.org/openjpa/docs/latest/manual/ 
manual.html#ref_guide_perfpack_eager


> I assumed it makes one select per object. So in this case I
> would get two selects (which I thought I saw).
>
> Phill
>
> -----Original Message-----
> From: Marc Prud'hommeaux [mailto:mprudhomapache@gmail.com] On  
> Behalf Of Marc
> Prud'hommeaux
> Sent: April 24, 2007 2:20 PM
> To: open-jpa-dev@incubator.apache.org
> Subject: Re: Funky Query from entityManager.find(Class,id)
>
> Phill-
>
>> It should not be selecting from two tables as it cannot create the
>> object. As I mentioned before (in the other thread) this causes the
>> looked for object to be null as if the object was not found. Here is
>> my call statement
>
> ManyToOne and OneToOne relations default to being eagerly fetched.
> You can override this by specifying fetch=LAZY in the @ManyToOne  
> annotation.
>
> That being said, I don't understand that problem. Are you saying  
> that the lookup
> fails because there is no related attributetype row? That shouldn't  
> prevent the
> lookup from happening, since we are using an outer join.
>
>
>
> On Apr 23, 2007, at 11:20 PM, Phill Moran wrote:
>
>> I posted this question before but I am now seeing it in several  
>> places
>> and can produce a test case for it. Here are two related classes
>> (one-to-
>> many) attribute
>> and attributeType, each with table/class inheritance.
>>
>>
>> Attribute class:
>> package ca.BidSpec.emall.categories;
>>
>> import javax.persistence.Basic;
>> import javax.persistence.CascadeType;
>> import javax.persistence.Column;
>> import javax.persistence.Entity;
>> import javax.persistence.Inheritance;
>> import javax.persistence.InheritanceType;
>> import javax.persistence.JoinColumn;
>> import javax.persistence.ManyToOne;
>> import javax.persistence.NamedQueries; import
>> javax.persistence.NamedQuery; import javax.persistence.Table;
>>
>> import ca.BidSpec.emall.commonServices.PrimaryKey;
>> import ca.BidSpec.emall.persistence.Persistable;
>>
>> @Entity
>> @Table(name = "attributes")
>> @NamedQueries( {
>> 		@NamedQuery(name = "AttributeFXPK", query = "SELECT a FROM
> Attribute
>> a WHERE a.id = :primaryKey"),
>> 		@NamedQuery(name = "AttributeFXDescription", query = "SELECT a
> FROM
>> Attribute a WHERE a.value = :description ORDER BY a.value") })
>> @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class
>> Attribute extends Persistable {
>>
>> 	private AttributeType type;
>>
>> 	private String value = "";
>>
>> 	/*
>> 	 * required for persistence
>> 	 */
>> 	private Attribute() {
>> 	}
>>
>> 	public Attribute(PrimaryKey pk) {
>> 		this.setPrimaryKey(pk);
>> 	}
>>
>> 	/**
>> 	 * @param pk
>> 	 *            this objects primary key
>> 	 * @param type
>> 	 *            this object attribute type
>> 	 * @param value
>> 	 *            the value for this attribute given it's type
>> 	 */
>> 	public Attribute(PrimaryKey pk, AttributeType type, String value) {
>> 		this.setPrimaryKey(pk);
>> 		this.setType(type);
>> 		this.setValue(value);
>> 	}
>>
>> 	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
>> CascadeType.MERGE })
>> 	@JoinColumn(name = "attributeTypeFK")
>> 	public AttributeType getType() {
>> 		return type;
>> 	}
>>
>> 	public void setType(AttributeType type) {
>> 		this.type = type;
>> 	}
>>
>> 	@Basic()
>> 	@Column(name = "value", nullable = false, length = 50)
>> 	public String getValue() {
>> 		return value;
>> 	}
>>
>> 	public void setValue(String value) {
>> 		this.value = value;
>> 	}
>> }
>>
>>
>> AttributeType Class
>> package ca.BidSpec.emall.categories;
>>
>> import javax.persistence.Basic;
>> import javax.persistence.Column;
>> import javax.persistence.Entity;
>> import javax.persistence.Inheritance;
>> import javax.persistence.InheritanceType;
>> import javax.persistence.NamedQueries; import
>> javax.persistence.NamedQuery; import javax.persistence.Table;
>>
>> import ca.BidSpec.emall.commonServices.PrimaryKey;
>> import ca.BidSpec.emall.persistence.Persistable;
>>
>> @Entity
>> @Table(name = "attributetype")
>> @NamedQueries( {
>> 		@NamedQuery(name = "AttributeTypeFXPK", query = "SELECT a FROM
>> AttributeType a WHERE a.id = :primaryKey"),
>> 		@NamedQuery(name = "AttributeTypeFXDescription", query = "SELECT
> a
>> FROM AttributeType a WHERE a.description = :description ORDER BY
>> a.description") })
>> @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class
>> AttributeType extends Persistable {
>> 	private String description = "";
>>
>> 	/*
>> 	 * required for persistence
>> 	 */
>> 	private AttributeType() {
>> 	}
>>
>> 	public AttributeType(PrimaryKey pk) {
>> 		this.setPrimaryKey(pk);
>> 	}
>>
>> 	public AttributeType(PrimaryKey pk, String description) {
>> 		this.setPrimaryKey(pk);
>> 		this.setDescription(description);
>> 	}
>>
>> 	@Basic()
>> 	@Column(name = "description", length = 50)
>> 	public String getDescription() {
>> 		return description;
>> 	}
>>
>> 	/*
>> 	 * Set this attribute type's description
>> 	 */
>> 	public void setDescription(String description) {
>> 		this.description = description;
>> 	}
>> }
>>
>> Persistable is my based persistable class of all Entities:
>>
>> package ca.BidSpec.emall.persistence;
>>
>> import java.io.Serializable;
>> import java.sql.Timestamp;
>> import java.util.Date;
>>
>> import javax.persistence.Basic;
>> import javax.persistence.Column;
>> import javax.persistence.Id;
>> import javax.persistence.MappedSuperclass;
>> import javax.persistence.Temporal;
>> import javax.persistence.TemporalType; import
>> javax.persistence.Transient;
>>
>> import ca.BidSpec.emall.commonServices.PrimaryKey;
>>
>> @MappedSuperclass
>> public abstract class Persistable implements Serializable {
>>
>> 	/**
>> 	 * Comment for <code>id</code> This is a persistable objects Primary
>> id
>> 	 */
>>
>> 	private String id;
>>
>> 	/**
>> 	 * User record last updated
>> 	 */
>>
>> 	private Timestamp lastUpdated = new Timestamp((new Date()).getTime
>> ());
>>
>> 	/**
>> 	 * @return Returns the persistable objects id.
>> 	 */
>> 	@Id
>> 	@Column(name = "id")
>> 	protected String getId() {
>> 		return this.id;
>> 	}
>>
>> 	protected void setId(String theId) {
>> 		this.id = theId;
>> 	}
>>
>> 	/**
>> 	 * @return this object id in a primary id object
>> 	 */
>> 	@Transient
>> 	public PrimaryKey getPrimaryKey() {
>> 		return new PrimaryKey(this.getId());
>> 	}
>>
>> 	/**
>> 	 * @param id
>> 	 *            The id to set.
>> 	 */
>> 	public void setPrimaryKey(PrimaryKey primaryKey) {
>> 		this.setId(primaryKey.getKey());
>> 	}
>>
>> 	/**
>> 	 * @return Returns the lastUpdated date for this object.
>> 	 */
>> 	@Basic
>> 	@Temporal(TemporalType.TIMESTAMP)
>> 	@Column(name = "lastUpdated")
>> 	public Timestamp getLastUpdated() {
>> 		return this.lastUpdated;
>> 	}
>>
>> 	public void setLastUpdated(Timestamp lastUpdated) {
>> 		this.lastUpdated = lastUpdated;
>> 	}
>>
>> 	public boolean equals(Object obj) {
>> 		if ((obj != null) && (obj instanceof Persistable)) {
>> 			if (((Persistable) obj).getId().equals(this.getId())) {
>> 				return true;
>> 			}
>> 		}
>> 		return false;
>> 	}
>>
>> 	public int hashCode() {
>> 		return this.getId().hashCode();
>> 	}
>>
>> Here is the query generated:
>>
>> 16360  TRACE  [main] openjpa.jdbc.SQL - <t 28346522, conn 30686131>
>> executing prepstmnt 17313093 SELECT t0.id, t0.lastUpdated, t1.id,
>> t1.lastUpdated, t1.description, t0.value FROM attributes t0 LEFT  
>> OUTER
>> JOIN attributetype t1 ON t0.attributeTypeFK = t1.id WHERE t0.id = ?
>> [params=(String) 111e3a22-728a-86ce-0126-11a7acec229a:0]
>>
>> It should not be selecting from two tables as it cannot create the
>> object. As I mentioned before (in the other thread) this causes the
>> looked for object to be null as if the object was not found. Here is
>> my call statement
>>
>> 		Attribute deleteObj = (Attribute)
>> categoryFactory.getObjectByPK(Attribute.class,
>> 				"111e3a22-728a-86ce-0126-11a7acec229a:0"); <-
> this ID exists in
>> the table
>> 		assertNotNull(deleteObj); <-- fails here
>>
>> getObjectBYPK is
>>
>> 	public Persistable getObjectByPK(Class<? extends Persistable>
>> objectTypeBeingSought, String pk) {
>> 		Persistable per = null;
>> 		per = (Persistable)
>> getEntityManager().find(objectTypeBeingSought, pk);
>> 		return per;
>> 	}
>>
>> }
>>
>> I think this may be a bug as I see it in several classes - but
>> frustratingly not all. Any suggestions
>>
>> Phill
>>
>


RE: Funky Query from entityManager.find(Class,id)

Posted by Phill Moran <pj...@rogers.com>.
Maybe I am wrong here let me ask a clarifying question. When executing a find on
an object that has a related object (like this example) does OpenJPA create one
select statement and grab all fields for both objects then parse the results to
build both objects? I assumed it makes one select per object. So in this case I
would get two selects (which I thought I saw).

Phill

-----Original Message-----
From: Marc Prud'hommeaux [mailto:mprudhomapache@gmail.com] On Behalf Of Marc
Prud'hommeaux
Sent: April 24, 2007 2:20 PM
To: open-jpa-dev@incubator.apache.org
Subject: Re: Funky Query from entityManager.find(Class,id)

Phill-

> It should not be selecting from two tables as it cannot create the 
> object. As I mentioned before (in the other thread) this causes the 
> looked for object to be null as if the object was not found. Here is 
> my call statement

ManyToOne and OneToOne relations default to being eagerly fetched.  
You can override this by specifying fetch=LAZY in the @ManyToOne annotation.

That being said, I don't understand that problem. Are you saying that the lookup
fails because there is no related attributetype row? That shouldn't prevent the
lookup from happening, since we are using an outer join.



On Apr 23, 2007, at 11:20 PM, Phill Moran wrote:

> I posted this question before but I am now seeing it in several places 
> and can produce a test case for it. Here are two related classes 
> (one-to-
> many) attribute
> and attributeType, each with table/class inheritance.
>
>
> Attribute class:
> package ca.BidSpec.emall.categories;
>
> import javax.persistence.Basic;
> import javax.persistence.CascadeType;
> import javax.persistence.Column;
> import javax.persistence.Entity;
> import javax.persistence.Inheritance;
> import javax.persistence.InheritanceType;
> import javax.persistence.JoinColumn;
> import javax.persistence.ManyToOne;
> import javax.persistence.NamedQueries; import 
> javax.persistence.NamedQuery; import javax.persistence.Table;
>
> import ca.BidSpec.emall.commonServices.PrimaryKey;
> import ca.BidSpec.emall.persistence.Persistable;
>
> @Entity
> @Table(name = "attributes")
> @NamedQueries( {
> 		@NamedQuery(name = "AttributeFXPK", query = "SELECT a FROM
Attribute 
> a WHERE a.id = :primaryKey"),
> 		@NamedQuery(name = "AttributeFXDescription", query = "SELECT a
FROM 
> Attribute a WHERE a.value = :description ORDER BY a.value") }) 
> @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class 
> Attribute extends Persistable {
>
> 	private AttributeType type;
>
> 	private String value = "";
>
> 	/*
> 	 * required for persistence
> 	 */
> 	private Attribute() {
> 	}
>
> 	public Attribute(PrimaryKey pk) {
> 		this.setPrimaryKey(pk);
> 	}
>
> 	/**
> 	 * @param pk
> 	 *            this objects primary key
> 	 * @param type
> 	 *            this object attribute type
> 	 * @param value
> 	 *            the value for this attribute given it's type
> 	 */
> 	public Attribute(PrimaryKey pk, AttributeType type, String value) {
> 		this.setPrimaryKey(pk);
> 		this.setType(type);
> 		this.setValue(value);
> 	}
>
> 	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.REFRESH, 
> CascadeType.MERGE })
> 	@JoinColumn(name = "attributeTypeFK")
> 	public AttributeType getType() {
> 		return type;
> 	}
>
> 	public void setType(AttributeType type) {
> 		this.type = type;
> 	}
>
> 	@Basic()
> 	@Column(name = "value", nullable = false, length = 50)
> 	public String getValue() {
> 		return value;
> 	}
>
> 	public void setValue(String value) {
> 		this.value = value;
> 	}
> }
>
>
> AttributeType Class
> package ca.BidSpec.emall.categories;
>
> import javax.persistence.Basic;
> import javax.persistence.Column;
> import javax.persistence.Entity;
> import javax.persistence.Inheritance;
> import javax.persistence.InheritanceType;
> import javax.persistence.NamedQueries; import 
> javax.persistence.NamedQuery; import javax.persistence.Table;
>
> import ca.BidSpec.emall.commonServices.PrimaryKey;
> import ca.BidSpec.emall.persistence.Persistable;
>
> @Entity
> @Table(name = "attributetype")
> @NamedQueries( {
> 		@NamedQuery(name = "AttributeTypeFXPK", query = "SELECT a FROM 
> AttributeType a WHERE a.id = :primaryKey"),
> 		@NamedQuery(name = "AttributeTypeFXDescription", query = "SELECT
a 
> FROM AttributeType a WHERE a.description = :description ORDER BY
> a.description") })
> @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class 
> AttributeType extends Persistable {
> 	private String description = "";
>
> 	/*
> 	 * required for persistence
> 	 */
> 	private AttributeType() {
> 	}
>
> 	public AttributeType(PrimaryKey pk) {
> 		this.setPrimaryKey(pk);
> 	}
>
> 	public AttributeType(PrimaryKey pk, String description) {
> 		this.setPrimaryKey(pk);
> 		this.setDescription(description);
> 	}
>
> 	@Basic()
> 	@Column(name = "description", length = 50)
> 	public String getDescription() {
> 		return description;
> 	}
>
> 	/*
> 	 * Set this attribute type's description
> 	 */
> 	public void setDescription(String description) {
> 		this.description = description;
> 	}
> }
>
> Persistable is my based persistable class of all Entities:
>
> package ca.BidSpec.emall.persistence;
>
> import java.io.Serializable;
> import java.sql.Timestamp;
> import java.util.Date;
>
> import javax.persistence.Basic;
> import javax.persistence.Column;
> import javax.persistence.Id;
> import javax.persistence.MappedSuperclass;
> import javax.persistence.Temporal;
> import javax.persistence.TemporalType; import 
> javax.persistence.Transient;
>
> import ca.BidSpec.emall.commonServices.PrimaryKey;
>
> @MappedSuperclass
> public abstract class Persistable implements Serializable {
>
> 	/**
> 	 * Comment for <code>id</code> This is a persistable objects Primary 
> id
> 	 */
>
> 	private String id;
>
> 	/**
> 	 * User record last updated
> 	 */
>
> 	private Timestamp lastUpdated = new Timestamp((new Date()).getTime 
> ());
>
> 	/**
> 	 * @return Returns the persistable objects id.
> 	 */
> 	@Id
> 	@Column(name = "id")
> 	protected String getId() {
> 		return this.id;
> 	}
>
> 	protected void setId(String theId) {
> 		this.id = theId;
> 	}
>
> 	/**
> 	 * @return this object id in a primary id object
> 	 */
> 	@Transient
> 	public PrimaryKey getPrimaryKey() {
> 		return new PrimaryKey(this.getId());
> 	}
>
> 	/**
> 	 * @param id
> 	 *            The id to set.
> 	 */
> 	public void setPrimaryKey(PrimaryKey primaryKey) {
> 		this.setId(primaryKey.getKey());
> 	}
>
> 	/**
> 	 * @return Returns the lastUpdated date for this object.
> 	 */
> 	@Basic
> 	@Temporal(TemporalType.TIMESTAMP)
> 	@Column(name = "lastUpdated")
> 	public Timestamp getLastUpdated() {
> 		return this.lastUpdated;
> 	}
>
> 	public void setLastUpdated(Timestamp lastUpdated) {
> 		this.lastUpdated = lastUpdated;
> 	}
>
> 	public boolean equals(Object obj) {
> 		if ((obj != null) && (obj instanceof Persistable)) {
> 			if (((Persistable) obj).getId().equals(this.getId())) {
> 				return true;
> 			}
> 		}
> 		return false;
> 	}
>
> 	public int hashCode() {
> 		return this.getId().hashCode();
> 	}
>
> Here is the query generated:
>
> 16360  TRACE  [main] openjpa.jdbc.SQL - <t 28346522, conn 30686131> 
> executing prepstmnt 17313093 SELECT t0.id, t0.lastUpdated, t1.id, 
> t1.lastUpdated, t1.description, t0.value FROM attributes t0 LEFT OUTER 
> JOIN attributetype t1 ON t0.attributeTypeFK = t1.id WHERE t0.id = ? 
> [params=(String) 111e3a22-728a-86ce-0126-11a7acec229a:0]
>
> It should not be selecting from two tables as it cannot create the 
> object. As I mentioned before (in the other thread) this causes the 
> looked for object to be null as if the object was not found. Here is 
> my call statement
>
> 		Attribute deleteObj = (Attribute)
> categoryFactory.getObjectByPK(Attribute.class,
> 				"111e3a22-728a-86ce-0126-11a7acec229a:0"); <-
this ID exists in 
> the table
> 		assertNotNull(deleteObj); <-- fails here
>
> getObjectBYPK is
>
> 	public Persistable getObjectByPK(Class<? extends Persistable> 
> objectTypeBeingSought, String pk) {
> 		Persistable per = null;
> 		per = (Persistable)
> getEntityManager().find(objectTypeBeingSought, pk);
> 		return per;
> 	}
>
> }
>
> I think this may be a bug as I see it in several classes - but 
> frustratingly not all. Any suggestions
>
> Phill
>


Re: Funky Query from entityManager.find(Class,id)

Posted by Marc Prud'hommeaux <mp...@apache.org>.
Phill-

> It should not be selecting from two tables as it cannot create the  
> object. As I
> mentioned before (in the other thread) this causes the looked for  
> object to be
> null as if the object was not found. Here is my call statement

ManyToOne and OneToOne relations default to being eagerly fetched.  
You can override this by specifying fetch=LAZY in the @ManyToOne  
annotation.

That being said, I don't understand that problem. Are you saying that  
the lookup fails because there is no related attributetype row? That  
shouldn't prevent the lookup from happening, since we are using an  
outer join.



On Apr 23, 2007, at 11:20 PM, Phill Moran wrote:

> I posted this question before but I am now seeing it in several  
> places and can
> produce a test case for it. Here are two related classes (one-to- 
> many) attribute
> and attributeType, each with table/class inheritance.
>
>
> Attribute class:
> package ca.BidSpec.emall.categories;
>
> import javax.persistence.Basic;
> import javax.persistence.CascadeType;
> import javax.persistence.Column;
> import javax.persistence.Entity;
> import javax.persistence.Inheritance;
> import javax.persistence.InheritanceType;
> import javax.persistence.JoinColumn;
> import javax.persistence.ManyToOne;
> import javax.persistence.NamedQueries;
> import javax.persistence.NamedQuery;
> import javax.persistence.Table;
>
> import ca.BidSpec.emall.commonServices.PrimaryKey;
> import ca.BidSpec.emall.persistence.Persistable;
>
> @Entity
> @Table(name = "attributes")
> @NamedQueries( {
> 		@NamedQuery(name = "AttributeFXPK", query = "SELECT a FROM
> Attribute a WHERE a.id = :primaryKey"),
> 		@NamedQuery(name = "AttributeFXDescription", query = "SELECT a
> FROM Attribute a WHERE a.value = :description ORDER BY a.value") })
> @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
> public class Attribute extends Persistable {
>
> 	private AttributeType type;
>
> 	private String value = "";
>
> 	/*
> 	 * required for persistence
> 	 */
> 	private Attribute() {
> 	}
>
> 	public Attribute(PrimaryKey pk) {
> 		this.setPrimaryKey(pk);
> 	}
>
> 	/**
> 	 * @param pk
> 	 *            this objects primary key
> 	 * @param type
> 	 *            this object attribute type
> 	 * @param value
> 	 *            the value for this attribute given it's type
> 	 */
> 	public Attribute(PrimaryKey pk, AttributeType type, String value) {
> 		this.setPrimaryKey(pk);
> 		this.setType(type);
> 		this.setValue(value);
> 	}
>
> 	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
> CascadeType.MERGE })
> 	@JoinColumn(name = "attributeTypeFK")
> 	public AttributeType getType() {
> 		return type;
> 	}
>
> 	public void setType(AttributeType type) {
> 		this.type = type;
> 	}
>
> 	@Basic()
> 	@Column(name = "value", nullable = false, length = 50)
> 	public String getValue() {
> 		return value;
> 	}
>
> 	public void setValue(String value) {
> 		this.value = value;
> 	}
> }
>
>
> AttributeType Class
> package ca.BidSpec.emall.categories;
>
> import javax.persistence.Basic;
> import javax.persistence.Column;
> import javax.persistence.Entity;
> import javax.persistence.Inheritance;
> import javax.persistence.InheritanceType;
> import javax.persistence.NamedQueries;
> import javax.persistence.NamedQuery;
> import javax.persistence.Table;
>
> import ca.BidSpec.emall.commonServices.PrimaryKey;
> import ca.BidSpec.emall.persistence.Persistable;
>
> @Entity
> @Table(name = "attributetype")
> @NamedQueries( {
> 		@NamedQuery(name = "AttributeTypeFXPK", query = "SELECT a FROM
> AttributeType a WHERE a.id = :primaryKey"),
> 		@NamedQuery(name = "AttributeTypeFXDescription", query = "SELECT
> a FROM AttributeType a WHERE a.description = :description ORDER BY
> a.description") })
> @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
> public class AttributeType extends Persistable {
> 	private String description = "";
>
> 	/*
> 	 * required for persistence
> 	 */
> 	private AttributeType() {
> 	}
>
> 	public AttributeType(PrimaryKey pk) {
> 		this.setPrimaryKey(pk);
> 	}
>
> 	public AttributeType(PrimaryKey pk, String description) {
> 		this.setPrimaryKey(pk);
> 		this.setDescription(description);
> 	}
>
> 	@Basic()
> 	@Column(name = "description", length = 50)
> 	public String getDescription() {
> 		return description;
> 	}
>
> 	/*
> 	 * Set this attribute type's description
> 	 */
> 	public void setDescription(String description) {
> 		this.description = description;
> 	}
> }
>
> Persistable is my based persistable class of all Entities:
>
> package ca.BidSpec.emall.persistence;
>
> import java.io.Serializable;
> import java.sql.Timestamp;
> import java.util.Date;
>
> import javax.persistence.Basic;
> import javax.persistence.Column;
> import javax.persistence.Id;
> import javax.persistence.MappedSuperclass;
> import javax.persistence.Temporal;
> import javax.persistence.TemporalType;
> import javax.persistence.Transient;
>
> import ca.BidSpec.emall.commonServices.PrimaryKey;
>
> @MappedSuperclass
> public abstract class Persistable implements Serializable {
>
> 	/**
> 	 * Comment for <code>id</code> This is a persistable objects  
> Primary id
> 	 */
>
> 	private String id;
>
> 	/**
> 	 * User record last updated
> 	 */
>
> 	private Timestamp lastUpdated = new Timestamp((new Date()).getTime 
> ());
>
> 	/**
> 	 * @return Returns the persistable objects id.
> 	 */
> 	@Id
> 	@Column(name = "id")
> 	protected String getId() {
> 		return this.id;
> 	}
>
> 	protected void setId(String theId) {
> 		this.id = theId;
> 	}
>
> 	/**
> 	 * @return this object id in a primary id object
> 	 */
> 	@Transient
> 	public PrimaryKey getPrimaryKey() {
> 		return new PrimaryKey(this.getId());
> 	}
>
> 	/**
> 	 * @param id
> 	 *            The id to set.
> 	 */
> 	public void setPrimaryKey(PrimaryKey primaryKey) {
> 		this.setId(primaryKey.getKey());
> 	}
>
> 	/**
> 	 * @return Returns the lastUpdated date for this object.
> 	 */
> 	@Basic
> 	@Temporal(TemporalType.TIMESTAMP)
> 	@Column(name = "lastUpdated")
> 	public Timestamp getLastUpdated() {
> 		return this.lastUpdated;
> 	}
>
> 	public void setLastUpdated(Timestamp lastUpdated) {
> 		this.lastUpdated = lastUpdated;
> 	}
>
> 	public boolean equals(Object obj) {
> 		if ((obj != null) && (obj instanceof Persistable)) {
> 			if (((Persistable) obj).getId().equals(this.getId())) {
> 				return true;
> 			}
> 		}
> 		return false;
> 	}
>
> 	public int hashCode() {
> 		return this.getId().hashCode();
> 	}
>
> Here is the query generated:
>
> 16360  TRACE  [main] openjpa.jdbc.SQL - <t 28346522, conn 30686131>  
> executing
> prepstmnt 17313093 SELECT t0.id, t0.lastUpdated, t1.id,  
> t1.lastUpdated,
> t1.description, t0.value FROM attributes t0 LEFT OUTER JOIN  
> attributetype t1 ON
> t0.attributeTypeFK = t1.id WHERE t0.id = ? [params=(String)
> 111e3a22-728a-86ce-0126-11a7acec229a:0]
>
> It should not be selecting from two tables as it cannot create the  
> object. As I
> mentioned before (in the other thread) this causes the looked for  
> object to be
> null as if the object was not found. Here is my call statement
>
> 		Attribute deleteObj = (Attribute)
> categoryFactory.getObjectByPK(Attribute.class,
> 				"111e3a22-728a-86ce-0126-11a7acec229a:0"); <-
> this ID exists in the table
> 		assertNotNull(deleteObj); <-- fails here
>
> getObjectBYPK is
>
> 	public Persistable getObjectByPK(Class<? extends Persistable>
> objectTypeBeingSought, String pk) {
> 		Persistable per = null;
> 		per = (Persistable)
> getEntityManager().find(objectTypeBeingSought, pk);
> 		return per;
> 	}
>
> }
>
> I think this may be a bug as I see it in several classes - but  
> frustratingly not
> all. Any suggestions
>
> Phill
>


Funky Query from entityManager.find(Class,id)

Posted by Phill Moran <pj...@rogers.com>.
I posted this question before but I am now seeing it in several places and can
produce a test case for it. Here are two related classes (one-to-many) attribute
and attributeType, each with table/class inheritance.


Attribute class:
package ca.BidSpec.emall.categories;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import ca.BidSpec.emall.commonServices.PrimaryKey;
import ca.BidSpec.emall.persistence.Persistable;

@Entity
@Table(name = "attributes")
@NamedQueries( {
		@NamedQuery(name = "AttributeFXPK", query = "SELECT a FROM
Attribute a WHERE a.id = :primaryKey"),
		@NamedQuery(name = "AttributeFXDescription", query = "SELECT a
FROM Attribute a WHERE a.value = :description ORDER BY a.value") })
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Attribute extends Persistable {

	private AttributeType type;

	private String value = "";

	/*
	 * required for persistence
	 */
	private Attribute() {
	}

	public Attribute(PrimaryKey pk) {
		this.setPrimaryKey(pk);
	}

	/**
	 * @param pk
	 *            this objects primary key
	 * @param type
	 *            this object attribute type
	 * @param value
	 *            the value for this attribute given it's type
	 */
	public Attribute(PrimaryKey pk, AttributeType type, String value) {
		this.setPrimaryKey(pk);
		this.setType(type);
		this.setValue(value);
	}

	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
CascadeType.MERGE })
	@JoinColumn(name = "attributeTypeFK")
	public AttributeType getType() {
		return type;
	}

	public void setType(AttributeType type) {
		this.type = type;
	}

	@Basic()
	@Column(name = "value", nullable = false, length = 50)
	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
}


AttributeType Class
package ca.BidSpec.emall.categories;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import ca.BidSpec.emall.commonServices.PrimaryKey;
import ca.BidSpec.emall.persistence.Persistable;

@Entity
@Table(name = "attributetype")
@NamedQueries( {
		@NamedQuery(name = "AttributeTypeFXPK", query = "SELECT a FROM
AttributeType a WHERE a.id = :primaryKey"),
		@NamedQuery(name = "AttributeTypeFXDescription", query = "SELECT
a FROM AttributeType a WHERE a.description = :description ORDER BY
a.description") })
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class AttributeType extends Persistable {
	private String description = "";

	/*
	 * required for persistence
	 */
	private AttributeType() {
	}

	public AttributeType(PrimaryKey pk) {
		this.setPrimaryKey(pk);
	}

	public AttributeType(PrimaryKey pk, String description) {
		this.setPrimaryKey(pk);
		this.setDescription(description);
	}

	@Basic()
	@Column(name = "description", length = 50)
	public String getDescription() {
		return description;
	}

	/*
	 * Set this attribute type's description
	 */
	public void setDescription(String description) {
		this.description = description;
	}
}

Persistable is my based persistable class of all Entities:

package ca.BidSpec.emall.persistence;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import ca.BidSpec.emall.commonServices.PrimaryKey;

@MappedSuperclass
public abstract class Persistable implements Serializable {

	/**
	 * Comment for <code>id</code> This is a persistable objects Primary id
	 */

	private String id;

	/**
	 * User record last updated
	 */

	private Timestamp lastUpdated = new Timestamp((new Date()).getTime());

	/**
	 * @return Returns the persistable objects id.
	 */
	@Id
	@Column(name = "id")
	protected String getId() {
		return this.id;
	}

	protected void setId(String theId) {
		this.id = theId;
	}

	/**
	 * @return this object id in a primary id object
	 */
	@Transient
	public PrimaryKey getPrimaryKey() {
		return new PrimaryKey(this.getId());
	}

	/**
	 * @param id
	 *            The id to set.
	 */
	public void setPrimaryKey(PrimaryKey primaryKey) {
		this.setId(primaryKey.getKey());
	}

	/**
	 * @return Returns the lastUpdated date for this object. 
	 */
	@Basic
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "lastUpdated")
	public Timestamp getLastUpdated() {
		return this.lastUpdated;
	}

	public void setLastUpdated(Timestamp lastUpdated) {
		this.lastUpdated = lastUpdated;
	}

	public boolean equals(Object obj) {
		if ((obj != null) && (obj instanceof Persistable)) {
			if (((Persistable) obj).getId().equals(this.getId())) {
				return true;
			}
		}
		return false;
	}

	public int hashCode() {
		return this.getId().hashCode();
	}

Here is the query generated:

16360  TRACE  [main] openjpa.jdbc.SQL - <t 28346522, conn 30686131> executing
prepstmnt 17313093 SELECT t0.id, t0.lastUpdated, t1.id, t1.lastUpdated,
t1.description, t0.value FROM attributes t0 LEFT OUTER JOIN attributetype t1 ON
t0.attributeTypeFK = t1.id WHERE t0.id = ? [params=(String)
111e3a22-728a-86ce-0126-11a7acec229a:0]

It should not be selecting from two tables as it cannot create the object. As I
mentioned before (in the other thread) this causes the looked for object to be
null as if the object was not found. Here is my call statement

		Attribute deleteObj = (Attribute)
categoryFactory.getObjectByPK(Attribute.class,
				"111e3a22-728a-86ce-0126-11a7acec229a:0"); <-
this ID exists in the table
		assertNotNull(deleteObj); <-- fails here 

getObjectBYPK is 

	public Persistable getObjectByPK(Class<? extends Persistable>
objectTypeBeingSought, String pk) {
		Persistable per = null;
		per = (Persistable)
getEntityManager().find(objectTypeBeingSought, pk);
		return per;
	}

}

I think this may be a bug as I see it in several classes - but frustratingly not
all. Any suggestions

Phill