Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA 1.2 retrieving entities withtout associations  RSS feed

 
Markus Schmider
Ranch Hand
Posts: 147
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
using JPA 1.2 (JBoss 4.2, Hibernate)
I need to retrieve the contents of a table without fetching the associated objects.
The entity which maps to the table has many associatiations which are by default eagerly fetched.
The SQL
select * from tbe_bwz
executes in about a second with over 300 000 entries.

In java I have tried


this code seems to run for ever. I have read that Hibernate tries to fetch all associations even with native queries.
Is there a way to fetch the all the entities of a table without associations without resorting to raw JDBC code?

Thanks,

Hans
 
Carles Gasques
Ranch Hand
Posts: 199
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Have you annotated the associated fields of the entity with FetchType.LAZY?

You could use the jpa query language to express the same native query
Query q = em.createQuery("select bwz from tbe_bwz bwz");


Best reggards,
 
Markus Schmider
Ranch Hand
Posts: 147
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

I cannot annotate the fields LAZY, because for most use cases they must be fetched eagerly.
However I am trying to implement a mass update for this table, so I must override the eager fetching for exactly one query.
At the moment almost the entire database is loaded into memory when I try to load all entries of this table.

I have tried JPQL before, same result. I resorted to native query because I naively thought that this would disable fetching of all associations.

 
Carles Gasques
Ranch Hand
Posts: 199
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm not sure that jpa 1.2 or 2 have this feature but seems that you could enable or disable lazy fetch for a single query using hibernate criteria api.
But using directly hibernate provider would break your jpa abstraction layer.

Cheers,
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!