Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » eclipselink.join-fetch not work at all
eclipselink.join-fetch not work at all [message #689679] Tue, 28 June 2011 07:12 Go to next message
Boyd Pang is currently offline Boyd PangFriend
Messages: 12
Registered: June 2011
Junior Member
Hi all,

I have two class ManagedElement and EquipmentHolder,their relationship is as follows:

@Entity
@Table(name="managedelement")
public class ManagedElement
{
@OneToMany(mappedBy="managedElement", fetch=FetchType.LAZY,cascade={CascadeType.ALL})

private Set<EquipmentHolder> equipmentHolders = new HashSet<EquipmentHolder>();

......
}

@Entity
@Table(name="equipmentholder")
public class EquipmentHolder
{
@ManyToOne(cascade={CascadeType.REFRESH,CascadeType.MERGE})
@JoinColumn(name="ME_ID", referencedColumnName="TOPORES_ID")
private ManagedElement managedElement;

......
}

(1)If I set the fetchType of ManagedElment and Equipmentholder to FetchType.EAGER,

I do the follow query:

Query query = em.createQuery("select distinct me from ManagedElement me where me.id="+id);

query.getSingleResult();

With Eclipse IDE debug mode ,I can get the "equipmentHolders" of ManagedElement ,marked in red in the follow Figure.


index.php/fa/3149/0/

(2)If I set the fetchType of ManagedElment and Equipmentholder to FetchType.LAZY,

I use the following three queries:


(a)
Query query = em.createQuery("select distinct me from ManagedElement me LEFT JOIN FETCH me.equipmentHolders where me.id="+id);
(b)
Query query = em.createQuery("select distinct me from ManagedElement me where me.id="+id);

query.setHint("eclipselink.join-fetch", "me.equipmentHolders");

or

query.setHint("eclipselink.batch", "me.equipmentHolders");

(c)
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<ManagedElement> cq = qb.createQuery (ManagedElement.class);
cq.distinct (true);
Root<ManagedElement> meRoot = cq.from (ManagedElement.class);
meRoot.fetch ("equipmentHolders", JoinType.LEFT);
cq.where (qb.equal(meRoot.get("id"), id));
Query query = em.createQuery(cq);



query.getSingleResult();

With Eclipse IDE debug mode ,I can get the "equipmentHolders" of ManagedElement ,marked in red in the follow Figure.

index.php/fa/3150/0/

Why I got a IndirectSet? Is this normal ? I want get HashSet not IndirectSet,Can someone help me ?

Thank you,Boyd.
Re: eclipselink.join-fetch not work at all [message #689761 is a reply to message #689679] Tue, 28 June 2011 09:47 Go to previous messageGo to next message
Boyd Pang is currently offline Boyd PangFriend
Messages: 12
Registered: June 2011
Junior Member
I used spring3.1.0M1 and Eclipselink2.3.0.v20110604-r9504,and in the documents of spring I found the changes infomation as follows:

Changes in version 3.0.4 (2010-08-19)
-------------------------------------
* support for Hibernate Core 3.6, Hibernate Validator 4.1, EclipseLink 2.1, EHCache 2.2


Changes in version 3.0.3 (2010-06-15)
-------------------------------------

* Spring autodetects JodaTime 1.3 or higher (as required), ignoring older JodaTime versions
* clarified that Spring's Jackson support requires Jackson 1.3 or higher
* JPA 2.0 support tested and supported with Hibernate 3.5.2 and OpenJPA 2.0.0 GA as well

I was confused by this problem for a long time,hope someone help me.

Thank you very much,Boyd.

[Updated on: Tue, 28 June 2011 09:51]

Report message to a moderator

Re: eclipselink.join-fetch not work at all [message #689985 is a reply to message #689679] Tue, 28 June 2011 17:31 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

JPA requires that you use the collection interfaces Collection, List, Set or Map to allow the JPA provider to provide optimizations just as LAZY loading. It should not matter what type of Set it is, just use the Set interface. If you really want a HashSet for some specific purpose, you can define a getFooAsHashSet method that returns new HashSet(this.foo).

If you use EAGER, then by default EclipseLink will allow you to use an implementation class, but this is not possible with LAZY. EclipseLink will use an instance of IndirectSet (which contains a HashSet) for any LAZY field, this does not mean the set has not been read, just that it has the potential to be LAZY.

In general in Java it is best to use the collection interfaces in your code.

Why do you want a HashSet?


James : Wiki : Book : Blog : Twitter
Re: eclipselink.join-fetch not work at all [message #690093 is a reply to message #689985] Wed, 29 June 2011 01:21 Go to previous messageGo to next message
Boyd Pang is currently offline Boyd PangFriend
Messages: 12
Registered: June 2011
Junior Member
Thank you very much James.

I am a new one to Eclipselink,and I didn't understand lazy loading very well before.I thought that if the data fetched sucessfully,the eager loading and lazy loading will have a same data structure(that is a HashSet) ,that's why I said I need a HashSet.

Now I understand the lazy loading,if I set the hint "eclipselink.join-fetch",the data were fetched successfully indeed,although contained in a IndirectSet.

Once again, thank you very much.

(no subject) [message #690759 is a reply to message #690093] Thu, 30 June 2011 07:17 Go to previous messageGo to next message
Boyd Pang is currently offline Boyd PangFriend
Messages: 12
Registered: June 2011
Junior Member
James,

Is there any way to know that the IndirectSet is instantiated(tha) yet ? That is data have been fetcted from db.
Re: (no subject) [message #690992 is a reply to message #690759] Thu, 30 June 2011 14:12 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You can call the isInstantiated() method.

James : Wiki : Book : Blog : Twitter
Previous Topic:Executing arbitrary SQL in the current session
Next Topic:TopLink Essentials Caching Options
Goto Forum:
  


Current Time: Wed Apr 24 14:46:27 GMT 2024

Powered by FUDForum. Page generated in 0.03450 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top