使用 EntityManagerFactory 导致重复主键异常

发布于 2024-08-25 03:07:23 字数 568 浏览 8 评论 0 原文

大家好,我的目标是使用依赖于正在使用的数据库的属性创建一个 EntityManager。我在所有 Google 搜索中都看到过类似的操作(为了解决这个问题,我使代码变得更加基本):

@PersistenceUnit
private EntityManagerFactory emf; 
private EntityManager em;
private Properties props;

@PostConstruct
public void createEntityManager(){

//if oracle set oracle properties else set postgres properties

emf = Persistence.createEntityManagerFactory("app-x");
em = emf.createEntityManager(props);
}

这有效,我可以成功加载 Oracle 或 Postgres 属性,并且可以从任一数据库中进行选择。然而,我在执行 INSERT 语句时遇到了问题。每当插入完成时,我都会收到重复的主键异常..每次!谁能解释一下为什么会发生这种情况?谢谢 -布拉德

Hey guys, my goal is create an EntityManager using properties dependent on which database is in use. I've seen something like this done in all my Google searches(I made the code more basic for the purpose of this question):

@PersistenceUnit
private EntityManagerFactory emf; 
private EntityManager em;
private Properties props;

@PostConstruct
public void createEntityManager(){

//if oracle set oracle properties else set postgres properties

emf = Persistence.createEntityManagerFactory("app-x");
em = emf.createEntityManager(props);
}

This works and I can load Oracle or Postgres properties successfully and I can Select from either database. HOWEVER, I am running into issues when doing INSERT statements. Whenever an INSERT is done I get a duplicate primary key exception.. every time! Can anyone shed some light on why this may be happening? Thanks
-Brad

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

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

发布评论

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

评论(2

静谧幽蓝 2024-09-01 03:07:23

容器管理环境中,您可以直接注入EntityManager

要获取 EntityManager 实例,请将实体管理器注入到应用程序组件中:

<前><代码>@PersistenceContext
实体管理器em;

如果需要处理不同的 持久性单元(以及几个 EntityManager 实例),在 persistence.xml 并注入正确的 EntityManager按其名称:

@PersistenceContext(unitName = "MyFirstPU")
EntityManager em;

更新:根据指定数据库(还提到了博客文章),EclipseLink 可能能够自动检测数据库平台,并且 eclipselink.target-database 是可选的:

如果您使用默认的持久性提供程序,该提供程序会尝试根据连接元数据自动检测数据库类型。

如果这适用于 Oracle 和 PostgreSQL(我的理解是它应该),客户只需设置一个数据源,这在我看来是理想的场景。

In a container-managed environment, you can directly inject an EntityManager:

To obtain an EntityManager instance, inject the entity manager into the application component:

@PersistenceContext
EntityManager em;

If you need to deal with different persistence units (and thus several EntityManager instances), declare them in the persistence.xml and get the right EntityManager injected by its name:

@PersistenceContext(unitName = "MyFirstPU")
EntityManager em;

Update: According to Specifying the Database (and also mentioned this blog post), EclipseLink may be able to auto-detect the database platform and the eclipselink.target-database is optional:

If you are using the default persistence provider, the provider attempts to automatically detect the database type based on the connection metadata.

If this works with Oracle and PostgreSQL (and my understanding is that it should), the customer would only have to setup a datasource which is IMO the ideal scenario.

远昼 2024-09-01 03:07:23

使用 < 注释您的 EntityManager code>@PersistenceContext(unitName="app-x")

因此,您不需要创建新的实体管理器和工厂 - 一切都由容器自动处理。

Annotate your EntityManager with @PersistenceContext(unitName="app-x")

Thus you will not need to create new entity managers and factories - everything is automatically handled by your container.

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