Develop the Data Access Tier

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

“Find all records” operation

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

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

We will then see how to write the corresponding data access operation for the above test.

Press + to interact
@Repository
@Transactional
public class EstateDaoImpl implements EstateDao {
@Autowired
private SessionFactory sessionFactory ;
@SuppressWarnings("unchecked")
@Override
public List<Estate> getAll() {
return sessionFactory.getCurrentSession().createQuery("select estate from Estate estate order by estate.id desc").list();
}
}

Insert operation

Let’s ...