You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by "Azamat S. Kalimoulline" <tu...@bazon.ru> on 2012/04/02 09:55:19 UTC

eager fetch do not do join fetch

I have 3 entities:

@Entity
class A
{
	@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
	Long id
	
	@Basic
	String name
	
	@OneToMany(mappedBy = "a", fetch = FetchType.EAGER, cascade = 
[CascadeType.ALL])
	List<B> bs = []
}


@Entity
class B
{
	@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
	Long id

	@Basic
	String name
	
	@ManyToOne(cascade = [CascadeType.PERSIST, CascadeType.MERGE, 
CascadeType.DETACH])
	A a
	
	@OneToMany(mappedBy = "b", fetch = FetchType.EAGER, cascade = 
[CascadeType.ALL])
	List<C> cs = []

	void setA(A a)
	{
        if(this.a != null)
        {
            this.a.getBs().remove(this)
        }

        if(a != null)
        {
            a.getBs().add(this)
        }

        this.a = a
	}
}


@Entity
class C
{
	@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
	Long id

	@Basic
	String name
	
	@ManyToOne(cascade = [CascadeType.PERSIST, CascadeType.MERGE, 
CascadeType.DETACH])
	B b
	
	void setB(B b)
	{
        if(this.b != null)
        {
            this.b.getCs().remove(this)
        }

        if(b != null)
        {
            b.getCs().add(this)
        }

        this.b = b
	}
}


A and B has OneToMany relations. Both relations defined by fetch = 
FetchType.EAGER.

in persistence.xml

			<property name="openjpa.jdbc.EagerFetchMode" value="join"/>

Therefore, whe I do em.find(A.class, id), it produces several sqls instead of 
one:

SELECT t0.NAME, t1.A_ID, t1.ID, t1.NAME 
    FROM APP.A t0 LEFT OUTER JOIN APP.B t1 ON t0.ID = t1.A_ID 
    WHERE t0.ID = ? 
    ORDER BY t1.A_ID ASC 
[params=(long) 1]
5921  openjpa-examples  TRACE  [main] openjpa.jdbc.SQL - <t 1132441875, conn 
1170789081> [6 ms] spent
5952  openjpa-examples  TRACE  [main] openjpa.jdbc.SQL - <t 1132441875, conn 
1170789081> executing prepstmnt 636562801 
SELECT t0.ID, t0.NAME 
    FROM APP.C t0 
    WHERE t0.B_ID = ? 
[params=(long) 52]
5953  openjpa-examples  TRACE  [main] openjpa.jdbc.SQL - <t 1132441875, conn 
1170789081> [1 ms] spent
5957  openjpa-examples  TRACE  [main] openjpa.jdbc.SQL - <t 1132441875, conn 
1170789081> executing prepstmnt 745129193 
SELECT t0.ID, t0.NAME 
    FROM APP.C t0 
    WHERE t0.B_ID = ? 
[params=(long) 51]

How to select A and eager fetch B and Bs Cs field?

-- 
WBR Turtle//BAZON Group