1

I have a pojo which contains a few named queries to get data.

@NamedQueries({
   @NamedQuery(name="abc", query="test")
})
@Entity
@Table(name = "MY_TABLE")
public class MyTable implements java.io.Serializable{
    private long id;
    private String name;
     ...........

I have to access the result of this named query from inside a service layer method. So I tried to autowire the hibernate session factory into the service layer class.

@Service
public class MyServiceClass{ 
    @Autowired
    SessionFactory sessionFactory;
    ..........
    public void myMethod() {
       Session session = acceSessionFactory.getCurrentSession();
       Query query = session.getNamedQuery("abc").setInteger("id", 1).setString("name", "testname");
       MyTable mytablerow = (MyTable) query.uniqueResult();
          .......
    }

However in the above approach - I think we are having the dao layer logic in service layer. Is this the correct way to access the named queries?

Note: I do not have a DAO interface or class for the MyTable class above.

user811433
  • 3,999
  • 13
  • 53
  • 76

2 Answers2

0

Yes you are having DAO layer logic in your service class. Better design would be to have a MyTableDao interface which exposes various methods which can be used to retrieve data from MyTable.

Pratik Shelar
  • 3,154
  • 7
  • 31
  • 51
  • If I add a MyTableDao interface, would that not be defeating the purpose of having the namedqueries? – user811433 Oct 10 '13 at 06:23
  • According to design you should expose a method in your interface for your named query. This method will use the namedQuery and retrieve results and send accordingly. For a single named query your approach will work fine. But thinking of future if you have multiple named queries having all the methods which call the named query in one place will help. Also there could be scenarios where you wish to filter out some data which you recieved in the named query and dao class would be to ideal place to do that for readablity purpose – Pratik Shelar Oct 10 '13 at 06:44
0

In you approach you actually have no DAO layer.
The common approach for Service Layer with DAO will be

@NamedQueries({
   @NamedQuery(name="abc", query="test")
})
@Entity
@Table(name = "MY_TABLE")
public class MyTable


 @Repository
 public class MyTableDAOImpl implements MyTableDAO

    @Autowire
    protected SessionFactory sessionFactory;
    public MyTable myMethod1() {
        Query query = session.getNamedQuery("abc")
        .setInteger("id",1).setString("name", "testname");
        return (MyTable) query.uniqueResult();}

    public MyTable myMethod2() { ...}


@Service
public class MyTableServiceImpl implements MyTableService 
   @Autowire
   protected MyTableDAO myTableDAO;


   public MyTable myMethodService() {
      //Some logic
       ...
       return  myTableDAO.myMethod1()

  }

The purpose of having the named queries is that they are compiled and validated at app start-up time See Advantages of Named queries in hibernate?

I suggest that you will consider the GenericDAO pattern

Community
  • 1
  • 1
Haim Raman
  • 11,508
  • 6
  • 44
  • 70
  • How about this combined approach - Have a GenericDAO - Have an interface IMyTableDAO which implements GenericDAO - but do not have any MyTableDAOImpl. – user811433 Oct 10 '13 at 06:43
  • @user811433 look at `spring-data-jpa`, it's exactly why you're describing. – Kartoch Oct 10 '13 at 06:48
  • You will need MyTableDAOImpl NamedQuery("abc") is usually specific for your entity or some base entity. It will not be refereed from GenericDAOImpl – Haim Raman Oct 10 '13 at 06:49
  • Yes spring-data-jpa had formalized this approach. I did not worked with spring-data-jpa, but it looks promising. – Haim Raman Oct 10 '13 at 06:55
  • @Kartoch - I do not know about spring-data-jpa. Thanks for pointing me to that. Will I need to integrate that with hibernate to use in my current scenario? Apologies if that is a naive question. – user811433 Oct 10 '13 at 07:00
  • 2
    @user811433 sprtng-data-jpa generate all DAO(s) (repositories in spring vocabulary) based on a single interface you have wrote. So it is build on the top of spring and JPA? thus completly independant of the ORM provider (hibernate in your case). – Kartoch Oct 10 '13 at 07:18
  • Accepting this answer as it pointed me towards the GenericDAO pattern and spring-data-jpa which helped me out. – user811433 Oct 10 '13 at 07:29