Develop the Data Access Tier
The data access tier is the second step in the process of developing a service.
We'll cover the following...
We'll cover the following...
Now let’s turn to the data access tier. We will start with the test case for the get all records procedure.
Find all records operation
Press + to interact
@RunWith( SpringJUnit4ClassRunner.class )@ContextConfiguration( locations = { "classpath:context.xml" } )@TransactionConfiguration( defaultRollback = true )@Transactionalpublic class StudentDaoImplTest {@Autowiredprivate StudentDao dao ;@Testpublic void testGetAll() {Assert.assertEquals(0L, dao.getAll().size());}}
For the test above, let’s look at the corresponding data access operation. The Hibernate 4 Session.createQuery
method is used with SQL to retrieve all the records from the Student
entity.
Press + to interact
@Repository@Transactionalpublic class StudentDaoImpl implements StudentDao{@Autowiredprivate SessionFactory sessionFactory ;@Overridepublic List<Student> getAll() {return (List<Student>)sessionFactory.getCurrentSession().createQuery("select student from Student student").list();}}