I am trying to get some JPA stuff to work. I have a table that has foreign keys to another tables. When I generate the JPA entities I get one to many anotations over those variables... what else would I need to do get all the tables back in the result set? if I indeed call it. Here is an example of what I am trying to do.
@OneToMany(mappedBy="resourceId")
private List<DistributionLink> distributionLinkCollection;
@OneToMany annotation is sitting pretty...
I am thinking to get all values back I would need to just call the named query that calls for this table right?
qLaunchArr = (QuickLaunch[])emf.createNamedQuery("QuickLaunch.getQuickLaunch").getResultList().toArray();
Am I way off base to think that I will also get an array of DistributionLinks with all result sets? Now I know this has something to do with Eager Loading vs Lazy loading. Exactly what, I am not sure. I know lazy loading only loads @oneToMany attributes explicitly called (I think)? How can I get back all those collections I will need. Will I need to loop through the result set and also call a named query to get all result sets using a named query?
Thanks.
When you choose to lazy load something depending on whether you're still in the transaction context, when you fetch your child collection with a getter, JPA should automatically fetch it for you. So if the data leaves the session bean that has the entity manager that fetched the parent object, you will not be able to automatically instantiate the lazy loaded collection.
If you choose to use an EAGER fetch strategy, each instance of that collection will be pre-instantiated and will be available anywhere.
A good compromise that lets you avoid constraining your design decision (but still allows you to take advantage of lazy loading to avoid loading stuff you don't need on every fetch of those records) to only dealing with these collections in the bean that fetches the parent entities would be to when you know in advance that you will need that collection, stick in your query:
JOIN FETCH distributionLinks