eclipselink.join-fetch not work at all [message #689679] |
Tue, 28 June 2011 07:12 |
Boyd Pang 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.
(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.
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 |
Boyd Pang 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 |
|
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
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03455 seconds