-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Bug in Hibernate lazy loading or bad JPA design?
PostPosted: Thu Aug 28, 2014 5:31 pm 
Newbie

Joined: Thu Aug 28, 2014 5:25 pm
Posts: 1
I have a one2one relation between Student and Address. I want the firstName and lastName fields of Student to be lazy loaded. Also I want lazy for the address field.

These are my entity clasess:

Code:
@Entity
    @Table(name = "students")
    public class Student {
   
       @Id
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       private Long id;
   
       @OneToOne(mappedBy = "student", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
       @LazyToOne(LazyToOneOption.NO_PROXY)
       private Address address;
   
       @Basic(fetch = FetchType.LAZY)
       @Column(name = "first_name")
       private String firstName;
   
       @Basic(fetch = FetchType.LAZY)
       @Column(name = "last_name")
       private String lastName;
   
       public Long getId() {
          return id;
       }
   
       public void setId(Long id) {
          this.id = id;
       }
   
       public Address getAddress() {
          return address;
       }
   
       public void setAddress(Address address) {
          this.address = address;
       }
   
       public String getFirstName() {
          return firstName;
       }
   
       public void setFirstName(String firstName) {
          this.firstName = firstName;
       }
   
       public String getLastName() {
          return lastName;
       }
   
       public void setLastName(String lastName) {
          this.lastName = lastName;
       }
   
       @Override
       public String toString() {
          return "Student [id=" + id + ", address=" + address + ", firstName="
                + firstName + ", lastName=" + lastName + "]";
       }
   
    }


The Address class:
Code:
 
    @Entity
    @Table(name = "addresses")
    public class Address {
   
     @Id
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       private Long id;
   
       @OneToOne
       @JoinColumn(name = "s_id")
       private Student student;
   
       @Column
       private String street;
   
       public Long getId() {
          return id;
       }
   
       public void setId(Long id) {
          this.id = id;
       }
   
       public Student getStudent() {
          return student;
       }
   
       public void setStudent(Student student) {
          this.student = student;
       }
   
       public String getStreet() {
          return street;
       }
   
       public void setStreet(String street) {
          this.street = street;
       }
   
       @Override
       public String toString() {
          return "Address [id=" + id + ", street=" + street + "]";
       }
   
    }


My test method looks like this ( the Java 8 lambda is just creating an entitymanager in back and executes all in transaction ):
Code:

   @Test
   public void dummyTest() {
      JPA_UTILS.runInTransaction(e -> {
         Student s = e.find(Student.class, 150L);
         System.out.println("----------++++++++++++++-----------");
         s.getFirstName();
         System.out.println("----------++++++++++++++-----------");
      });
   }

So here I am loading an existing student from the database, then fetch the lazy property firstName ( mapped to first_name column ). The problem is that Hibernate doesn't load only firstName but also lastName and address fields:
Code:

    just.hibernate.one2one.TestApp > dummyTest STANDARD_OUT
        Hibernate:
            select
                student0_.id as id1_1_0_
            from
                students student0_
            where
                student0_.id=?
        ----------++++++++++++++-----------
        Hibernate:
            /* sequential select
                just.hibernate.one2one.Student */ select
                    student_.first_name as first_na2_1_,
                    student_.last_name as last_nam3_1_
                from
                    students student_
                where
                    student_.id=?
        Hibernate:
            /* load just.hibernate.one2one.Address */ select
                address0_.id as id1_0_1_,
                address0_.street as street2_0_1_,
                address0_.s_id as s_id3_0_1_,
                student1_.id as id1_1_0_
            from
                addresses address0_
            left outer join
                students student1_
                    on address0_.s_id=student1_.id
            where
                address0_.s_id=?
        ----------++++++++++++++-----------

I don't want this behavior. I would have expected to query just the firstName field. Not lastName and by any means the address relation.

Is it a bug or just the intended behavior of Hibernate/JPA?

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.