Naveen
Naveen

Reputation: 77

How to achive lazy loading using spring data jpa?

In my project, I am using Spring Data JPA and extend the JpaRepository interface for my data fetching class.

OrganizationMaster class :

  @Entity
  @Table(name="organization_master")
  public class OrganizationMaster  {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="organization_id")
    private int organizationId;

    @OneToMany(mappedBy="organizationMaster")
        private List<CompanyMaster> companyMasters;
    }

CompanyMaster Class:

Entity
@Table(name="company_master")
public class CompanyMaster {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="company_id")
    private int companyId;


    @ManyToOne
    @JoinColumn(name="organization_id")
    private OrganizationMaster organizationMaster;
}

My Controller

@RequestMapping(value = "/GetOrganization", method = RequestMethod.GET)
    public
    @ResponseBody
    List<OrganizationMaster> getOrganization(){
       return organizationService.getOrganization();
    }

OrganizationService:

public interface OrganizationService {
   List<OrganizationMaster> getOrganization();
}

OrganizationServiceImpl:

@Service
public class OrganizationServiceImpl implements OrganizationService{
    @Autowired
    private OrganizationDao organizationDao;
    @Override
    public List<OrganizationMaster> getOrganization() {
        return organizationDao.findAll();
    }
}

OrganizationDao Interface:

public interface OrganizationDao extends JpaRepository<OrganizationMaster,Long> {
}

My Output Response is: [{"organizationId":5,"companyMasters":[{"companyId":29},{"companyId":30}]}]

But my need is [{"organizationId":5}]

When I am trying to get data from the organization master using findall() method it also fetches data from the company master based on the relationship. How can I achieve lazy fetching (get data only from organization master) using spring data JpaRepository

Upvotes: 0

Views: 2148

Answers (2)

Emre Savcı
Emre Savcı

Reputation: 3070

All XToOne associations are default EAGER. You have a bidirectional relationship, so you should use FetchType.LAZY on your @ManyToOne side.

@ManyToOne(fetch = FetchType.LAZY)

Also if you use any serializer (like json serializer) when it serialize it calls getter methods and it may causes to load lazy items unneccessarly.

Another consideration is while using Lombok, @Data annotation causes to load lazy items without need. So be careful when using Lombok.

So in your case, beacuse of you return entity itself, while serialization it serializes the child too, it causes to load lazly child entity.

You need to return a dto which represents only your parent entity to prevent serialization of child entity. If you call child with getter method, it laods lazly child entity from database.

Take a look for further information associations:

https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/

Upvotes: 3

mohammad
mohammad

Reputation: 57

I believe this question is asked before!you can use this annotation:

@OneToMany( fetch = FetchType.LAZY )

read this article for better view in this point:

https://howtodoinjava.com/hibernate/lazy-loading-in-hibernate/

Upvotes: -1

Related Questions