Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] How do I batchload many to many relations

Batch reading uses a distinct for OneToOne to avoid duplicate data, but this
is not possible for ManyToMany.

The issue is that the resulting objects must be matched to their owners, so
the owner's ID (t0.Employee_ID) must also be selected, so the rows are not
distinct, a row is required for each relationship.  So there is no way to
avoid the select, at least from the join table (you could re-map it as a
OneToMany to a join table object with a OneTOne, but unless your B object is
very big, this probably will not be beneficial).

You will still benefit from not having to build the B objects.



von Carlsen wrote:
> 
> In our project we have entity A that has a many to many reference to
> entity B. Thousands of A entities normally reference the same B entity.
> When I search 1000 A entities and on these use the shared B entity I have
> not found a way to avoid fetching the same B entity 1000 times from the
> database!
> I thought it could be done with batchread but it dosn't seem to work. I
> have made a small project to illustrate the problem with an Employee that
> can have several Addresses like this:
> 
> Employee:
> @ManyToMany
>     public List<Address> getAddresses() {
>         return addresses;
>     }
> 
> And search for the employees like this:
> 
> Query query = em.createQuery("select e from Employee e");
> query.setHint("eclipselink.batch", "e.addresses"); 
> 
> But the sql send to the database is:
> 
> SELECT t1.ID, t1.STREET, t0.Employee_ID FROM EMPLOYEE_ADDRESS t0, EMPLOYEE
> t2, ADDRESS t1 WHERE ((t0.Employee_ID = t2.ID) AND (t1.ID =
> t0.addresses_ID)
> 
> which will fetch the address info one time for each employee and then
> discard it in the entity manager when it finds out that it already has the
> Address loaded.
> 
> Is there another way of doing this.
> 
> 


-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://old.nabble.com/How-do-I-batchload-many-to-many-relations-tp26228795p26284505.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top