Search⌘ K
AI Features

Develop the Data Access Tier

Explore how to develop the data access tier in Java Spring applications using Hibernate. Understand test-driven approaches to implement operations such as insert, delete, update, and fetching all records, enhancing your ability to manage database interactions effectively.

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

Find all records operation

Java
@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.

Java
@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

...