如何在多个 PU 之间最好地共享实体类/DAO? [英] How to best share entity classes/DAOs across multiple PUs?

查看:20
本文介绍了如何在多个 PU 之间最好地共享实体类/DAO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

举一个基本的例子,假设我们有一个实体类

Let's take a basic example, say we have an entity class

@Entity
public class User {
}

和用于处理数据的 DAO:

and a DAO for dealing with the data:

public interface UserDao {
    @Transactional
    public void changeUser(User user);
}

@Repository
public class UserDaoImpl implements UserDao {
    @PersistenceContext
    private EntityManager em;

    @Override
    public void changeUser(User user) {
        // ...
    }
}

我们有一个主要的 Maven 项目,其中包含许多其他子项目,用于构建多个应用程序(WAR、JAR 等).我们选择的框架是 Spring.

We have a main Maven Project that contains plenty other sub-projects, that we use for building multiple application (WARs, JARs etc.). Our framework of choice is Spring.

每个应用程序都需要上述实体/DAO,并且不需要针对每个应用程序进行自定义,一切都保持不变.每个应用程序都有自己的数据源/持久化单元/事务管理器.

Each and every application needs the above entity / DAO and there are no per application customizations needed, everything remains the same. Every application has its own data source / persistence unit / transaction manager.

如何才能在多个应用程序中最好地重用上述实体/DAO?实体需要保留在应用程序的数据库中,我们还需要重用应用程序的事务管理器.

How can one best reuse the above entity / DAO across multiple applications? The entities need to be persisted in the application's database and we'd also need to reuse the app's transaction manager.

你在类似情况下做了什么?

What did you do in similar situations?

推荐答案

我将创建单独的 Maven 模块,其中包含实体和 DAO(您甚至可以创建两个模块 - 一个用于实体,一个用于 DAO).

I would create separate Maven module that would contain entities and DAOs (you can even create two modules - one for entities and one for DAOs).

此模块将作为依赖项添加到任何其他需要 DB 层的应用程序模块(EJB 或 WAR 模块).在 Spring 配置中,你应该正确定义数据源、持久化上下文、事务管理器等的注入规则.这样你就会有不同配置的通用代码.

This module would be added as a dependency to any other application module (EJB or WAR modules), which needs DB layer. In Spring configuration you should properly define injection rules for datasource, persistence context, transaction manager, etc. In this way you would have common code with different configuration.

这篇关于如何在多个 PU 之间最好地共享实体类/DAO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆