users@glassfish.java.net

Noob JPA question OneToMany and what actually gets pulled from DB and when?

From: <glassfish_at_javadesktop.org>
Date: Thu, 20 Dec 2007 12:07:22 PST

Hi all,

Evaluating the possibility of using JPA in upcoming JSF project and have what is probably a very basic question.

In the prototype of the app we just used SQL/JDBC and some of the queries were really complex with a lot of joins across multiple tables and it did work OK. However, I like the idea of using @OneToMany and similar annotations in JPA to define relationships between entities as it would be great for example, given an instance of an purchaseOrder be able to easily get the lineItems associated with it as Java objects via a sinle method call. However, does using this approach mean that when I find/retrieve a purchaseOrder it will also automatically go and and fetch *all* the corresponding lineItems from the database at that time, or only if I invoke the appropriate get method on the purchaseOrder instance (e.g. somepo.getLineItems())?

I'm asking because we will be dealing with a large database with a lot of related tables and if retrieving an instance of some object results in every other record related to it in the system being returned not sure how that would work.

I suspect I can do individual queries against each individual entity I'm interested in using value(s) from prior result to get what I want in next query, but that seems to be little improvement over using raw SQL.

Apologize for totally noob question but just learning about JPA. Very grateful to any insights on how to best achieve the equivalent of complex joins in standard SQL using JPA/TopLink instead.

thx in advance.
[Message sent by forum member 'kyancy' (kyancy)]

http://forums.java.net/jive/thread.jspa?messageID=251042