问题描述
让我们来一个基本的例子,假设我们有一个实体类
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项目,我们用于构建多个应用程序(战争,JAR文件等)。我们选择的框架是春天。
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.
那你在类似的情况呢?
推荐答案
我会创建单独的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).
这个模块将被添加作为一个依赖于任何其他应用程序模块(EJB或WAR模块),这需要DB层。在Spring配置,你应该正确定义为数据源,持久化上下文,事务管理等注射规则这样你会共同code具有不同的配置。
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的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!