Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write Less DAOs with Spring Hibernate using Annotations

My Spring+Hibernate configuration files are small and super tight. I use auto scanning to find my model entities/daos.

I don't want to have to write a DAO + DAOImpl for EVERY Entity in my hierarchy.

Some may qualify to have their own, like if they have complex relationships with other entities and require more than basic CRUD functionality. But for the rest...

Is there any way to circumvent the defacto standard?

Say, something like a generic DAO, ex:

http://www.ibm.com/developerworks/java/library/j-genericdao/index.html

Then I can do something like

  GenericDao dao = appContext.getBean("genericDao");
  dao.save(car);            
  dao.save(lease);

Is this possible with annotations? I don't want to have to configure anything in xml. If I cannot do above, is it still possible to have one GenericDaoImpl.java with something like:

 @Repository("carDao")
 @Repository("leaseDao")
 class GenericDaoImpl extends CustomHibernateDaoSupport implements GenericDao {
 ...
 }

and then

  GenericDao dao = appContext.getBean("carDao");
  dao.save(car);            
  dao = appContext.getBean("leaseDao"); //carDao is garbage coll.
  dao.save(lease);

Is this practical at all?

like image 797
niken Avatar asked Nov 22 '11 20:11

niken


People also ask

What annotation for DAO?

The best way to guarantee that your Data Access Objects (DAOs) or repositories provide exception translation is to use the @Repository annotation. This annotation also allows the component scanning support to find and configure your DAOs and repositories without having to provide XML configuration entries for them.

What is DAO in hibernate?

Data Access Objects (or DAOs for short) are used as a direct line of connection and communication with our database. DAOs are used when the actual CRUD (CRUD = Create, Read, Update, Delete) operations are needed and invoked in our Java code. These data access objects also represent the “data layer” of our application.

What is DAO layer in spring boot?

DAO stands for data access object. Usually, the DAO class is responsible for two concepts: encapsulating the details of the persistence layer and providing a CRUD interface for a single entity.

Why we use DAO class in spring?

The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate, JPA or JDO in a consistent way.


3 Answers

Using generics, you might try something like this:

@Repository
@Transactional
public class GenericDAOImpl<T> implements GenericDAO<T> {

    @Autowired
    private SessionFactory factory;

    public void persist(T entity) {
        Session session = factory.getCurrentSession();
        session.persist(entity);
    }

    @SuppressWarnings("unchecked")
    public T merge(T entity) {
        Session session = factory.getCurrentSession();
        return (T) session.merge(entity);
    }

    public void saveOrUpdate(T entity) {
        Session session = factory.getCurrentSession();
        session.saveOrUpdate(entity);
    }

    public void delete(T entity) {
        Session session = factory.getCurrentSession();
        session.delete(entity);
    }

}

The content may be different, but the general idea is applicable.

You should be able to then autowire the DAO in your controller and service classes by using

@Autowired
private GenericDAO<Car> carDao;
like image 173
Beau Grantham Avatar answered Sep 30 '22 01:09

Beau Grantham


You can combine Spring/Hibernate with JPA, which provides the EntityManager for a large amount of basic persistence tasks:

@Service
public class CarService {

    @PersistenceContext
    private EntityManager em;

    public void saveCarAndLease(Car car, Lease lease) {
        em.persist(car);
        em.persist(lease);
    }
}

It will also handle transactions and simple queries without needing to write a DAO. For the more complex operations, you can still write a DAO and fall back to Hibernate's SessionFactory (although JPA is an option here too).

Some tutorials suggest you should still write the DAO for abstraction of the JPA plumbing. However, I have personally found this unnecessary (JPA has a very small integration footprint), and in fact this is also the way Spring Roo deals with the data layer behind the scenes.

like image 25
seanhodges Avatar answered Sep 30 '22 02:09

seanhodges


Have you tried to use Spring Data. I mean to say Spring JPA where you can use repositories.
You can eliminate writing all stuffs for each entity.

like image 25
Atul Avatar answered Sep 30 '22 01:09

Atul