0

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.

Maheshwaran K
  • 1,982
  • 5
  • 19
  • 22

1 Answers1

1

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;
Community
  • 1
  • 1
Ashish Jagtap
  • 2,799
  • 3
  • 30
  • 45
  • Thanks for the answer. But FetchType.LAZY will fetch the entire child objects(User in this case) on demand. I want the properties of the child objects to be avoided, and only read certain properties of the child object, irrespective of FetchType.EAGER or FetchType.LAZY – Maheshwaran K Dec 10 '13 at 11:35
  • How it make seance to load some field of child objects Lazily. You can not load field of an entity Lazily. only Entities can be load either Lazily or Eagerly. – Ashish Jagtap Dec 10 '13 at 11:42
  • That's what my exact question is. Not even lazily. I dont wanna load them at all. These properties will be needed in CRUD of the second entity. But not for me all times. Is there any way of doing that? – Maheshwaran K Dec 10 '13 at 11:50
  • Some JPA providers support lazy fetching of individual elements in entities. I think it's provider specific. In general, fetch control in JPA is absolutely awful, and one of the reasons I stopped using it. See http://blog.ringerc.id.au/2012/06/mail-to-jpa-21-expert-group-re-fetch.html – Craig Ringer Dec 10 '13 at 12:25