Search⌘ K
AI Features

Develop the Data Access Tier

Explore how to develop the data access tier by implementing key database operations such as find all, insert, update, and delete using Hibernate 4 methods in Java. This lesson guides you through writing tests and corresponding data access functions to manage entities effectively within the Spring framework.

“Find all records” operation

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

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

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