Results 1 to 3 of 3

Thread: How to Force JOIN on JPA EAGER FETCH

Hybrid View

  1. #1
    Join Date
    Dec 2007
    Posts
    2

    Default How to Force JOIN on JPA EAGER FETCH

    Hi All,

    I am using struts2.11-spring(2.5)-jpa(hibernate 3.2.5) combination.

    I have a @ManyToOne annotation which eagerly fetched. I am unable to force join on this no matter what i do. it issues a seperate select statement once I make fetch type is set to Eager.

    here 's the code

    @Entity
    @Table(name = "person")
    public class Person {

    @Id
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "FirstName", nullable = false)
    private String firstName;

    @Column(name = "LastName", nullable = false)
    private String lastName;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "addressid", referencedColumnName = "addressid")
    private Address address;

    setters .. getters..

    @Entity
    @Table(name = "address")
    public class Address {

    @Id
    @Column(name = "addressid", nullable = false)
    private Integer addressid;

    @Column(name = "StreetName", nullable = false)
    private String streetName;

    @Column(name = "HouseNumber", nullable = false)
    private String houseNumber;
    ....

    I tried hibernate specific annotation @Fetch(FetchMode.JOIN) , but still a separate query is excuted instead of joining person table and address table.
    i can make this LAZY connection and do a JOIN FETCH but i want to Eagerly fetch but using a join. the reason for this is i am using

    <filter>
    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    <filter-class>
    org.springframework.orm.jpa.support.OpenEntityMana gerInViewFilter
    </filter-class>
    </filter>

    in web.xml for getting all lazy collections on demand from Action/JSP even after transcation has been completed.

    if i make my addess fetch LAZY this filter would generate a separate SQL and JOIN FETCH would go wasted.


    "select p FROM Person p" generates the following

    hibernate SQL:

    Hibernate: select person0_.id as id1_, person0_.addressid as addressid1_, person0_.FirstName as FirstName1_, person0_.LastName as LastName1_ from person person0_

    Hibernate: select address0_.addressid as addressid0_0_, address0_.City as City0_0_, address0_.HouseNumber as HouseNum3_0_0_, address0_.State as State0_0_, address0_.StreetName as StreetName0_0_, address0_.ZipCode as ZipCode0_0_ from address address0_ where address0_.addressid=?


    Thanks in advance

  2. #2
    Join Date
    Jul 2008
    Posts
    1

    Default We have the same issue, anyone has any soloution?

    This is really a JPA and implementation issue, so I'm going to post the question to those forums, but I was curious if anyone had any luck looking at what jpa365 had posted back in December?

  3. #3
    Join Date
    Oct 2010
    Posts
    1

    Default

    Hello

    Hibernate seems dont understand automatically the eager fetch

    try with this select

    select p FROM Person p left join fetch p.address

    good luck

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •