如何使用JPA/hibernate EntityManager和EJB3.0实现泛型?

发布于 2024-09-27 10:58:24 字数 657 浏览 3 评论 0原文

我有一个 slsb 保存我的业务逻辑,如何使用泛型将以下三种方法更改为一种泛型方法?前两个是相同的数据库,第三个是不同的数据库。这些方法还需要与事务相关的进一步注释吗?

@PersistenceContext(unitName = "db")
private EntityManager myEntityManager;

@PersistenceContext(unitName = "db2")
private EntityManager myDB2EntityManager;

@TransactionAttribute(TransactionAttribute.Required)
public void crud(MyEntity myEntity) throws MyException {
 myEntityManager.merge(myEntity);
}

public void crud(ADifferentEntity aDifferentEntity) throws MyException {
 myEntityManager.merge(aDifferentEntity);
}

public void crud(DB2Entity db2Entity) throws MyException {
 myDB2EntityManager.merge(db2Entity);
}

非常感谢。 干杯!

I have an slsb holding my business logic, how do I use generics to change the following three methods into one generic method ? The first two are the same db, the third is a different database. Also do the methods require further annotation in relation to transaction ?

@PersistenceContext(unitName = "db")
private EntityManager myEntityManager;

@PersistenceContext(unitName = "db2")
private EntityManager myDB2EntityManager;

@TransactionAttribute(TransactionAttribute.Required)
public void crud(MyEntity myEntity) throws MyException {
 myEntityManager.merge(myEntity);
}

public void crud(ADifferentEntity aDifferentEntity) throws MyException {
 myEntityManager.merge(aDifferentEntity);
}

public void crud(DB2Entity db2Entity) throws MyException {
 myDB2EntityManager.merge(db2Entity);
}

Many thanks in advance.
Cheers!

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

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

发布评论

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

评论(2

魄砕の薆 2024-10-04 10:58:24

不确定我是否完全理解这个问题,但是:
由于您有两个不同的实体管理器和两个不同的数据库(假设您没有同时将相同的数据重复保存到两个数据库,看来您没有),我认为有两种不同的方法是合理的在你的界面中。 (我认为我会对它们进行不同的命名以避免混淆。)

要合并前两个,如何使用公共接口或继承的抽象基类并将参数类型更改为该公共类型?

Not sure if I fully understand the question, but:
Since you have two different entity managers there and two different DBs (assuming you're not saving the same data in duplicate to both DBs at the same time, which it appears that you are not), I think it's reasonable to have two different methods in your interface. (I would name them differently to avoid confusion I think.)

To merge the first two how about using a common interface or inherited abstract base class and changing the parameter type to that common type?

静待花开 2024-10-04 10:58:24

如果您需要在同一方法中合并来自 2 个不同数据库的 2 个实体,则应该配置 JTA - 因为事务将跨越 2 个数据库。

不太确定您要对通用事物做什么...您是否试图提供一种方法来进行增删改查,例如 T extends AbstractEntity,然后在增删改查方法中,

crud(T entity) {
    if (entity instanceof DB1Entity) then em1.merge(entity) 
      else em2.merge(entity)
}

???

或者您是否尝试进行水平分区?:

多用户数据源 - Spring + Hibernate ,

http://www.jroller.com/kenwdelong/entry/horizo​​ntal_database_partitioning_with_spring

If you require merging of 2 entities from 2 different databases within the same method, you should have JTA configured - as the transaction will span the 2 databases.

Not too sure what you're trying to do with the generic thing... Are you trying to provide a method to crud e.g. a T extends AbstractEntity, and then in the crud method,

crud(T entity) {
    if (entity instanceof DB1Entity) then em1.merge(entity) 
      else em2.merge(entity)
}

???

or are you trying to do horizontal partitioning ?:

Multi-user Datasources - Spring + Hibernate,

http://www.jroller.com/kenwdelong/entry/horizontal_database_partitioning_with_spring

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