Search⌘ K
AI Features

Develop the Data Access Tier

Explore how to develop the data access tier in Java applications by writing test-driven operations such as finding, inserting, updating, and deleting records using Hibernate's Session methods. Understand practical implementation with single table inheritance and REST integration.

We will start with the test case for the “find all records” operation.

“Find all records” operation

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

We will learn how to write the corresponding data access operation of the above test.

Java
@Repository
@Transactional
public class ProtocolDaoImpl implements ProtocolDao {
@Autowired
private SessionFactory sessionFactory ;
@SuppressWarnings("unchecked")
@Override
public List<Protocol> getAll() {
return sessionFactory.getCurrentSession().createQuery("select protocol from Protocol protocol order by protocol.id desc").list();
}
}

Insert operation

Let’s move on ...