简体   繁体   中英

Simplify DAO Layer with Resteasy / Hibernate / Spring

I'm looking for a cleaner code for my dao layer :

I have a generic dao interface :

public interface GenericDAO<T> {
    T save(T entity);
    T merge(T entity);
    void delete(T entity);
    T findFromId(int id);
    List<T> findAll();
}

An abstract implementation :

public abstract class AbstractGenericDAOImpl<T> implements GenericDAO<T> {
    [...]
}

For each database entity, I have two files :

An interface :

public interface UserDAO extends GenericDAO<UserPE> {  }

A concrete class :

@Repository
public class UserDAOImpl extends AbstractGenericDAOImpl<UserPE> implements UserDAO {

    [ no code for most entities ]

}

I'm using spring injection like this :

@Autowired
private UserDAO userDao;

I'd like to use the generic dao for common entities, like :

@Autowired
private GenericDAO<MyEntity> myEntityDao;

But spring doesn't want to inject it (NoSuchBeanDefinitionException) and I don't know how to configure hibernate queries (which needs entity classes).

I'm using : Spring 3.1.0 Hibernate 3.6.3 Resteasy 2.3.7

Do you have any idea ?

Thks for reading.

If you use Spring 4, you will be able to use

@Autowired
private GenericDAO<MyEntity> myEntityDao;

if there is an implementation of GenericDAO without doing anything else.

Such capability was missing in Spring 3. Check out this blog post for more details.

However, if you introduce Spring 4 to your project you might break the integration with RestEasy. That's something you'll need to check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM