Avoid unnecessary fetches in @onetomany join

I have two classes mapped to two different tables in DB. For example ,

    @Entity
    @Table(schema = "schema", name = "tableA")
    public class ClassA{

    @Id
    @Column(name = "id", columnDefinition = "serial")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger id;

        @Column(name="title")
    private String title;

    @Column(name="first_name")
    private String firstName;

    @Column(name="middle_name")
    private String middleName;

    @Column(name="last_name")
    private String lastName;

        @Column(name ="updater")
        private BigInteger updaterId;

        @OneToOne(targetEntity=UserBean.class, fetch=FetchType.EAGER)
    @JoinColumn(name="updated_by",referencedColumnName="employee_number",insertable=false ,updatable=false)
    private User updatedDetails;  

 //getter setter
}

I have the User class as follows

@Entity
    @Table(schema = "schema", name = "User")
    public class User{

    @Id
    @Column(name = "id", columnDefinition = "serial")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger id;

        @Column(name="title")
    private String title;

    @Column(name="first_name")
    private String firstName;

    @Column(name="middle_name")
    private String middleName;

    @Column(name="last_name")
    private String lastName;

    //getter setters }

Now when I fetch the ClassA objects through ("From ClassA"), I am getting the corresponding User class objects also. Now my question is how do I ignore the unnecessary properties of the User class.

For example, I want to ignore middleName and lastName properties of User class. I have to restrict Hibernate from reading these two columns while joining. How do I do that?

I am using Spring + Hibernate.

Answers


Just change following line to Load Entity lazily

@OneToOne(targetEntity=UserBean.class, fetch=FetchType.EAGER)

to

@OneToOne(targetEntity=UserBean.class, fetch=FetchType.LAZY)

and read about FetchType.EAGER VS FetchType.LAZY

If you want to Load Field of an entity Lazily you can use @Basic(fetch=FetchType.LAZY) annotation on fields which you want load lazily .

 @Basic(fetch=FetchType.LAZY)
    @Column(name="first_name")
    private String firstName;

Need Your Help

Accessing database outside of localhost with PHP and Wampserver

php mysql database localhost wampserver

I am trying to access a database that is not in my localhost or in my network. I am WampServer so I can open the PHP file. I am trying this code bellow, just to see the connection working. but never

Document Click triggered on 'enter' form submission

javascript javascript-events jquery

I'm having trouble understanding why a click event binded to the document would be triggered through an 'enter' form submission. Here's the test page I'm looking at: