1

I am new to JPA and multi-tiered architecture way of coding. I am currently working on a new project were we are making an API in which DAO layer is used to communicate with database using JPQL.

Currently, I have written JPQL statements to get data from database. In addition to that,I am also performing lazy initializations to retrieve the complex objects. I want to know if it is right way to perform lazy initializations in DAO layer.

I have 2 more layers before DAO layer called Engine layer and EJB layer where I have the database connection active and can perform lazy initialization there as well. I want to know if it is a good architectural way to do like that because I want the database related to stuff to go only in DAO layer.

But again, I get stuck at a point where I argue myself saying why isn't doing lazy initialization in EJB/Engine layer a good way as I have the database connection active to perform DB operations. I am thinking of this way because I can only retrieve the necessary data in DAO layer which can be reused and lazy initialization can be performed as required by different classes.

I am not sure why I have the DB connection active in Engine and EJB layer( I hope this is for transaction management). It found it is done as per the architect suggestions where I have the EJBs defined as stateless and I have the Engine level classes having databases connections in their scope.

Sorry for lengthy question. Hope I have given necessary details to answer the question.

P.S. Please suggest me any good book or articles which will help me deciding which layer has responsibility of performing what task ideally.

poorna chandra
  • 27
  • 1
  • 1
  • 8
  • 1
    Since the point of lazy-initialization is to defer the loading of entities until they are needed, my opinion will be to do it in the highest of layers where you still have a `Session` or `Transaction` in scope (EJB in your case). Loading them in your DAO will always do it, whether you actually require them or not defeating the purpose of being lazy. My $0.02. – kjsebastian Oct 20 '16 at 02:40
  • thanks for the reply @conscells it ok to do database retrieval in EJB layer when we have DAO layer specifically to do that? – poorna chandra Oct 20 '16 at 04:24
  • If you are using Hibernate it is as simple as `Hibernate.initialize(parent.getChildRelation())`. So something like `if (userRequestedChild) { Hibernate.initialize(...); } return parent;` is ok to do in the EJB layer in my opinion. – kjsebastian Oct 20 '16 at 06:24

1 Answers1

1

I would say doing it in DAO is the right thing to do. But create a separate method for common use. For example if you have a method to get departments, like

public List<Department> getDepartments() {
     //get departments
     //Also fetch employees in each department here  // DON'T DO   
} 

It will be nice to have another method specific to fetch all entities and java doc it correctly.

public List<Department> getDepartmentsWithEmployees() {
     //get departments
     //Also fetch employees in each department here     
}

So people will call right method for the need and will reduce the chance of any performance issue.

Note: Use Join Fetch if possible.

Lyju I Edwinson
  • 1,764
  • 20
  • 20