You are 100% correct. This is a huge performance problem, because as
you said, you have to actually em.find both entities (or, if you are
creating one, you have to find secound to the pair). For a small
amount of data to be "married" this is not a problem, but for
something bigger - my only choice was to use native query and then to
invalidate TopLink's cache for the related entities. For example, I am
doing this when I am producing transactions in my database. Many
transactions can be assigned to one loan (loan holds list of
transactions and transaction holds loan). When I am producing few
hundreds/thousands transactions, I am inserting them into database
using JDBC, providing loanId in the transaction.loan_id field. During
jdbc inserts, I am collecting loan identifiers in List<Long>, and at
the end I call my UtilityServiceBean passing that list and Loan.class
to it and it is invalidating TopLink cache. After commit, I can query
Loan using JPA and the loan.getTransactions() :: List<Transaction>
returns everything.
Ugly hack, but JPA is still very young, it has its ugly parts :/
At the beginning I was doing this using JPA, but entire operation was
eating huge amount of memory, CPU resources and was lasting like for
ever. Now it works incredibly fast, CPU is sleeping and there is no
need for extra memory.
-Witold Szczerba
p.s.
Do not look for a hope in em.getReference. It works just like em.find.
At the beginning I was hoping - maybe they will eventually do
something with em.getReference, so there would be no need to "walk to
the database", but then I figured out that since em.getReference is
supposed to throw an exception when there is no entity in DB - there
is no way for TopLink developers to make it useful.
I hope, they will deprecate em.getReference in future and create the
replacement so it could be used to operate on some kind on entity
stub, but this is my wishful thinking...
Who knows, maybe in JPA 2.0?
2007/11/19, glassfish_at_javadesktop.org <glassfish_at_javadesktop.org>:
> Does your comment on setting both sides of relationship pertain to all relationship types (e.g. one to one, many to one, one to many) ?
>
> This just seems like a performance problem. In order to add a category to an item I need to actually find the category object to add the item to the category as well ??
> [Message sent by forum member 'fsa3' (fsa3)]
>
> http://forums.java.net/jive/thread.jspa?messageID=246102
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>