使用 Spring Hibernate 使用注释编写更少的 DAO

发布于 2024-12-17 18:19:01 字数 1007 浏览 2 评论 0原文

我的 Spring+Hibernate 配置文件很小而且非常紧凑。我使用自动扫描来查找我的模型实体/DAOS。

我不想为层次结构中的每个实体编写 DAO + DAOImpl。

有些人可能有资格拥有自己的,例如,如果他们与其他实体有复杂的关系并且需要的不仅仅是基本的 CRUD 功能。但对于其余的......

有什么办法可以绕过事实上的标准吗?

比如说,类似于通用 DAO,例如:

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

然后我可以做类似的事情,

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

这可以使用注释吗?我不想在 xml 中配置任何内容。如果我不能执行上述操作,是否仍然可以拥有一个 GenericDaoImpl.java ,其中包含以下内容:

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

然后

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

这是否实用?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她如夕阳 2024-12-24 18:19:01

使用泛型,你可以尝试这样的事情:

@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);
    }

}

内容可能不同,但总体思想是适用的。

然后,您应该能够使用以下命令在控制器和服务类中自动装配 DAO

@Autowired
private GenericDAO<Car> carDao;

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;
無處可尋 2024-12-24 18:19:01

您可以结合 Spring /Hibernate with JPA,它为大量基本持久性任务提供了 EntityManager:

@Service
public class CarService {

    @PersistenceContext
    private EntityManager em;

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

它还将处理事务和简单查询,而无需编写 DAO。对于更复杂的操作,您仍然可以编写 DAO 并回退到 Hibernate 的 SessionFactory(尽管这里也可以选择 JPA)。

一些教程建议您仍然应该编写 DAO 来抽象 JPA 管道。然而,我个人认为这是不必要的(JPA 的集成足迹非常小),事实上这也是这样的方式 Spring Roo 处理幕后的数据层。

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.

月寒剑心 2024-12-24 18:19:01

您是否尝试过使用Spring Data。我的意思是说Spring JPA,您可以在其中使用存储库。
您可以消除为每个实体编写所有内容的情况。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文