如何使用JPA/hibernate EntityManager和EJB3.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定我是否完全理解这个问题,但是:
由于您有两个不同的实体管理器和两个不同的数据库(假设您没有同时将相同的数据重复保存到两个数据库,看来您没有),我认为有两种不同的方法是合理的在你的界面中。 (我认为我会对它们进行不同的命名以避免混淆。)
要合并前两个,如何使用公共接口或继承的抽象基类并将参数类型更改为该公共类型?
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?
如果您需要在同一方法中合并来自 2 个不同数据库的 2 个实体,则应该配置 JTA - 因为事务将跨越 2 个数据库。
不太确定您要对通用事物做什么...您是否试图提供一种方法来进行增删改查,例如
T extends AbstractEntity
,然后在增删改查方法中,???
或者您是否尝试进行水平分区?:
多用户数据源 - Spring + Hibernate ,
http://www.jroller.com/kenwdelong/entry/horizontal_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,???
or are you trying to do horizontal partitioning ?:
Multi-user Datasources - Spring + Hibernate,
http://www.jroller.com/kenwdelong/entry/horizontal_database_partitioning_with_spring