Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. package com.chungykang.springdemo.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import javax.transaction.Transactional;
  6.  
  7. import org.hibernate.Session;
  8. import org.hibernate.SessionFactory;
  9. import org.hibernate.query.Query;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Repository;
  12.  
  13. import com.chungykang.springdemo.entity.Customer;
  14.  
  15.  
  16. @Repository // Always applied to DAO implementations
  17. public class CustomerDAOImpl implements CustomerDAO {
  18.  
  19.  
  20. // Need to inject the session factory
  21. @Autowired
  22. private SessionFactory sessionFactory;
  23.  
  24.  
  25. @Override
  26. @Transactional
  27. public List<Customer> getCustomers() {
  28.  
  29. // Get current Hibernate session
  30. Session currentSession = sessionFactory.getCurrentSession();
  31.  
  32. // Create a query
  33. Query<Customer> theQuery =
  34. currentSession.createQuery("from Customer", Customer.class);
  35.  
  36. // Get list of customers from Query
  37. List<Customer> customers = theQuery.getResultList();
  38.  
  39. // Return the results
  40. return customers;
  41. }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement