Develop the Data Access Tier

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

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

“Find all records” operation

Press + to interact
@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.

Press + to interact
@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 ...