You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Michael Pflueger <Mi...@sma.de> on 2011/08/04 14:51:08 UTC

NullpointerException when accessing a lazy association after merging the object without a fetch of the lazy entities

Hi,

I'm digging through some of my OpenJPA problems and came across this issue.
I get a NPE when I access te2.getChildren().xyz(), but only when I merge the object before the access AND the lazy association is not yet fetched AND I set Cascadetype.MERGE on the association.

		List<TestEntity> entities = query.getResultList();
		for (TestEntity te : entities) {
			TestEntity te2 = em.merge(te);
			System.out.println(te2.getChildren().size());
		}

I'm using OpenJPA 2.1.0 with the MySQL connector.
Attached is the full source and persistence.xml.

Can anybody reproduce this and is this a bug?

Regards,
Michael
___________________________________________________

SMA Solar Technology AG
Aufsichtsrat: Guenther Cramer (Vorsitzender)
Vorstand: Juergen Dolle, Roland Grebe, Uwe Hertel, Pierre-Pascal Urbon, Marko Werner
Handelsregister: Amtsgericht Kassel HRB 3972
Sitz der Gesellschaft: 34266 Niestetal
USt-ID-Nr. DE 113 08 59 54
WEEE-Reg.-Nr. DE 95881150
___________________________________________________

AW: NullpointerException when accessing a lazy association after merging the object without a fetch of the lazy entities

Posted by Michael Pflueger <Mi...@sma.de>.
Hi,

Running on 2.2 trunk indeed fixes this problem for me on first sight.

A backport to 2.1.x probably would be nice unless 2.2 is close to a releasable/production ready state, I guess.
___________________________________________________

SMA Solar Technology AG
Aufsichtsrat: Guenther Cramer (Vorsitzender)
Vorstand: Juergen Dolle, Roland Grebe, Uwe Hertel, Pierre-Pascal Urbon, Marko Werner
Handelsregister: Amtsgericht Kassel HRB 3972
Sitz der Gesellschaft: 34266 Niestetal
USt-ID-Nr. DE 113 08 59 54
WEEE-Reg.-Nr. DE 95881150
___________________________________________________


Re: NullpointerException when accessing a lazy association after merging the object without a fetch of the lazy entities

Posted by Rick Curtis <cu...@gmail.com>.
Well I found that something was fixed in trunk that hasn't been pulled back
to 2.1.x. Not sure what the change was, but that is why I couldn't recreate
your testcase. Unfortunately I'm getting pulled into other things this
morning and can't dig in to see what changed. Would it be possible to see if
running on trunk fixes this issue for you?

On Thu, Aug 4, 2011 at 9:39 AM, Michael Pflueger <Mi...@sma.de>wrote:

> Hi,
>
> > Agreed. For giggles, try removing the em.merge call. I seem to remember
> some
> > changes being made in this area. I'm been running on trunk, but I'll try
> to
> > get a 2.1.x testcase going.
>
> Well, as I said, it works without the merge, and it even works with the
> merge but without the merge being cascaded to the not yet fetched lazy
> children. (ie with removing Cascadetype.MERGE)
>
> Regards,
> Michael
> ___________________________________________________
>
> SMA Solar Technology AG
> Aufsichtsrat: Guenther Cramer (Vorsitzender)
> Vorstand: Juergen Dolle, Roland Grebe, Uwe Hertel, Pierre-Pascal Urbon,
> Marko Werner
> Handelsregister: Amtsgericht Kassel HRB 3972
> Sitz der Gesellschaft: 34266 Niestetal
> USt-ID-Nr. DE 113 08 59 54
> WEEE-Reg.-Nr. DE 95881150
> ___________________________________________________
>
>


-- 
*Rick Curtis*

AW: NullpointerException when accessing a lazy association after merging the object without a fetch of the lazy entities

Posted by Michael Pflueger <Mi...@sma.de>.
Hi,

> Agreed. For giggles, try removing the em.merge call. I seem to remember some
> changes being made in this area. I'm been running on trunk, but I'll try to
> get a 2.1.x testcase going.

Well, as I said, it works without the merge, and it even works with the merge but without the merge being cascaded to the not yet fetched lazy children. (ie with removing Cascadetype.MERGE)

Regards,
Michael
___________________________________________________

SMA Solar Technology AG
Aufsichtsrat: Guenther Cramer (Vorsitzender)
Vorstand: Juergen Dolle, Roland Grebe, Uwe Hertel, Pierre-Pascal Urbon, Marko Werner
Handelsregister: Amtsgericht Kassel HRB 3972
Sitz der Gesellschaft: 34266 Niestetal
USt-ID-Nr. DE 113 08 59 54
WEEE-Reg.-Nr. DE 95881150
___________________________________________________


Re: NullpointerException when accessing a lazy association after merging the object without a fetch of the lazy entities

Posted by Rick Curtis <cu...@gmail.com>.
> While it should be perfectly fine if not all testentities have children
(size() should just return 0)
correct... I need another cup of coffee :)

> Weird that it works for you...is there anything further I can do to debug
this?
Agreed. For giggles, try removing the em.merge call. I seem to remember some
changes being made in this area. I'm been running on trunk, but I'll try to
get a 2.1.x testcase going.

On Thu, Aug 4, 2011 at 9:12 AM, Michael Pflueger <Mi...@sma.de>wrote:

> While it should be perfectly fine if not all testentities have children
> (size() should just return 0), I have dropDB in my persistence.xml, so
> tables are dropped and recreated with every run.
>
> In this example, the call to merge() is indeed unnecessary, but in the real
> program I need to read a huge table and thus have to repeatedly clear() the
> persistence context so I can keep the memory footprint low. The clear() is
> causing all objects to be detached. Im reading this huge table within an
> outer loop which traverses another table, and I need to merge those entities
> so I can be sure I can access their lazy associations.
>
> Weird that it works for you...is there anything further I can do to debug
> this?
>
> It seems it fetches the children collection before the NPE, btw... here's a
> log:
>
>
> 2344  openjpa  TRACE  [main] openjpa.Runtime - Query "SELECT * FROM
> TestEntity t" is cached."
> 2344  openjpa  TRACE  [main] openjpa.Query - Executing query: Query:
> org.apache.openjpa.kernel.QueryImpl@d22462; candidate class: class
> test.TestEntity; query: null
> 2391  openjpa  TRACE  [main] openjpa.jdbc.SQL - <t 17284365, conn 16645208>
> executing prepstmnt 29806874 SELECT t0.id FROM TestEntity t0
> 2391  openjpa  TRACE  [main] openjpa.jdbc.SQL - <t 17284365, conn 16645208>
> [0 ms] spent
> 2406  openjpa  TRACE  [main] openjpa.jdbc.JDBC - <t 17284365, conn 0> [0
> ms] close
> I got 1 entities...
> 2422  openjpa  TRACE  [main] openjpa.jdbc.SQLDiag - load field: 'children'
> for oid=328601 class test.TestEntity
> 2438  openjpa  TRACE  [main] openjpa.jdbc.SQL - <t 17284365, conn 16645208>
> executing prepstmnt 1497769 SELECT t0.id FROM TestEntity2 t0 WHERE
> t0.PARENT_ID = ? [params=(int) 328601]
> 2438  openjpa  TRACE  [main] openjpa.jdbc.SQL - <t 17284365, conn 16645208>
> [0 ms] spent
> 2438  openjpa  TRACE  [main] openjpa.jdbc.JDBC - <t 17284365, conn 0> [0
> ms] close
> Exception in thread "main" java.lang.NullPointerException
>        at test.Test.main(Test.java:41)
>
>
>
> regards,
> Michael
>
>
>
> -----Ursprüngliche Nachricht-----
> Von: Rick Curtis [mailto:curtisr7@gmail.com]
> Gesendet: Donnerstag, 4. August 2011 16:00
> An: users@openjpa.apache.org
> Betreff: Re: NullpointerException when accessing a lazy association after
> merging the object without a fetch of the lazy entities
>
> I don't see any issues. Is it possible that you have a TestEntity in your
> DB
> that doesn't have any children (bad data) in your DB from a previous run?
>
> Also, why are you calling em.merge(..)? That call is unnecessary as the
> results from your query already are a part of your persistence context.
>
> Thanks,
> Rick
>
> ___________________________________________________
>
> SMA Solar Technology AG
> Aufsichtsrat: Guenther Cramer (Vorsitzender)
> Vorstand: Juergen Dolle, Roland Grebe, Uwe Hertel, Pierre-Pascal Urbon,
> Marko Werner
> Handelsregister: Amtsgericht Kassel HRB 3972
> Sitz der Gesellschaft: 34266 Niestetal
> USt-ID-Nr. DE 113 08 59 54
> WEEE-Reg.-Nr. DE 95881150
> ___________________________________________________
>
>


-- 
*Rick Curtis*

AW: NullpointerException when accessing a lazy association after merging the object without a fetch of the lazy entities

Posted by Michael Pflueger <Mi...@sma.de>.
While it should be perfectly fine if not all testentities have children (size() should just return 0), I have dropDB in my persistence.xml, so tables are dropped and recreated with every run.

In this example, the call to merge() is indeed unnecessary, but in the real program I need to read a huge table and thus have to repeatedly clear() the persistence context so I can keep the memory footprint low. The clear() is causing all objects to be detached. Im reading this huge table within an outer loop which traverses another table, and I need to merge those entities so I can be sure I can access their lazy associations.

Weird that it works for you...is there anything further I can do to debug this?

It seems it fetches the children collection before the NPE, btw... here's a log:


2344  openjpa  TRACE  [main] openjpa.Runtime - Query "SELECT * FROM TestEntity t" is cached."	
2344  openjpa  TRACE  [main] openjpa.Query - Executing query: Query: org.apache.openjpa.kernel.QueryImpl@d22462; candidate class: class test.TestEntity; query: null
2391  openjpa  TRACE  [main] openjpa.jdbc.SQL - <t 17284365, conn 16645208> executing prepstmnt 29806874 SELECT t0.id FROM TestEntity t0
2391  openjpa  TRACE  [main] openjpa.jdbc.SQL - <t 17284365, conn 16645208> [0 ms] spent
2406  openjpa  TRACE  [main] openjpa.jdbc.JDBC - <t 17284365, conn 0> [0 ms] close
I got 1 entities...
2422  openjpa  TRACE  [main] openjpa.jdbc.SQLDiag - load field: 'children' for oid=328601 class test.TestEntity
2438  openjpa  TRACE  [main] openjpa.jdbc.SQL - <t 17284365, conn 16645208> executing prepstmnt 1497769 SELECT t0.id FROM TestEntity2 t0 WHERE t0.PARENT_ID = ? [params=(int) 328601]
2438  openjpa  TRACE  [main] openjpa.jdbc.SQL - <t 17284365, conn 16645208> [0 ms] spent
2438  openjpa  TRACE  [main] openjpa.jdbc.JDBC - <t 17284365, conn 0> [0 ms] close
Exception in thread "main" java.lang.NullPointerException
	at test.Test.main(Test.java:41)



regards,
Michael



-----Ursprüngliche Nachricht-----
Von: Rick Curtis [mailto:curtisr7@gmail.com] 
Gesendet: Donnerstag, 4. August 2011 16:00
An: users@openjpa.apache.org
Betreff: Re: NullpointerException when accessing a lazy association after merging the object without a fetch of the lazy entities

I don't see any issues. Is it possible that you have a TestEntity in your DB
that doesn't have any children (bad data) in your DB from a previous run?

Also, why are you calling em.merge(..)? That call is unnecessary as the
results from your query already are a part of your persistence context.

Thanks,
Rick

___________________________________________________

SMA Solar Technology AG
Aufsichtsrat: Guenther Cramer (Vorsitzender)
Vorstand: Juergen Dolle, Roland Grebe, Uwe Hertel, Pierre-Pascal Urbon, Marko Werner
Handelsregister: Amtsgericht Kassel HRB 3972
Sitz der Gesellschaft: 34266 Niestetal
USt-ID-Nr. DE 113 08 59 54
WEEE-Reg.-Nr. DE 95881150
___________________________________________________


Re: NullpointerException when accessing a lazy association after merging the object without a fetch of the lazy entities

Posted by Rick Curtis <cu...@gmail.com>.
I don't see any issues. Is it possible that you have a TestEntity in your DB
that doesn't have any children (bad data) in your DB from a previous run?

Also, why are you calling em.merge(..)? That call is unnecessary as the
results from your query already are a part of your persistence context.

Thanks,
Rick

On Thu, Aug 4, 2011 at 7:51 AM, Michael Pflueger <Mi...@sma.de>wrote:

> Hi,
>
> I'm digging through some of my OpenJPA problems and came across this issue.
> I get a NPE when I access te2.getChildren().xyz(), but only when I merge
> the object before the access AND the lazy association is not yet fetched AND
> I set Cascadetype.MERGE on the association.
>
>                List<TestEntity> entities = query.getResultList();
>                for (TestEntity te : entities) {
>                        TestEntity te2 = em.merge(te);
>                        System.out.println(te2.getChildren().size());
>                }
>
> I'm using OpenJPA 2.1.0 with the MySQL connector.
> Attached is the full source and persistence.xml.
>
> Can anybody reproduce this and is this a bug?
>
> Regards,
> Michael
> ___________________________________________________
>
> SMA Solar Technology AG
> Aufsichtsrat: Guenther Cramer (Vorsitzender)
> Vorstand: Juergen Dolle, Roland Grebe, Uwe Hertel, Pierre-Pascal Urbon,
> Marko Werner
> Handelsregister: Amtsgericht Kassel HRB 3972
> Sitz der Gesellschaft: 34266 Niestetal
> USt-ID-Nr. DE 113 08 59 54
> WEEE-Reg.-Nr. DE 95881150
> ___________________________________________________
>



-- 
*Rick Curtis*