Skip to content

Instantly share code, notes, and snippets.

@darbyluv2code
Created January 15, 2018 15:50
Show Gist options
  • Select an option

  • Save darbyluv2code/dec1893c321dd138ab70d5507439767e to your computer and use it in GitHub Desktop.

Select an option

Save darbyluv2code/dec1893c321dd138ab70d5507439767e to your computer and use it in GitHub Desktop.
CustomerDAOImpl.java (check that you have the same imports)
package com.luv2code.springdemo.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.luv2code.springdemo.entity.Customer;
@Repository
public class CustomerDAOImpl implements CustomerDAO {
// need to inject the session factory
@Autowired
private SessionFactory sessionFactory;
@Override
@Transactional
public List<Customer> getCustomers() {
// get the current hibernate session
Session currentSession = sessionFactory.getCurrentSession();
// create a query
Query<Customer> theQuery =
currentSession.createQuery("from Customer", Customer.class);
// execute query and get result list
List<Customer> customers = theQuery.getResultList();
// return the results
return customers;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment