Develop the Data Access Tier

The data access tier is the second step in the process of developing a service.

Let’s move to the data access tier, beginning with the test case for the getAll records operation.

Find all records operation

Press + to interact
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath:context.xml" } )
@TransactionConfiguration( defaultRollback = true )
@Transactional
public class CustomerDaoImplTest {
@Autowired
private CustomerDao dao ;
@Test
public void testGetAll() {
Assert.assertEquals(0L, dao.getAll().size());
}
}

We will see how to write the imitating data access operation for the test. The Hibernate 4 Session.createQuery operation is used with SQL to retrieve all the records from the Customer entity.

Press + to interact
@Repository
@Transactional
public class CustomerDaoImpl implements CustomerDao{
@Autowired
private SessionFactory sessionFactory ;
@Override
public List<Customer> getAll() {
return sessionFactory.getCurrentSession().
createQuery("select customer from Customer customer order by customer.id desc").list();
}
}

Insert operation

...