表有时不会通过 Hibernate 本机查询进行更新

发布于 2024-11-16 02:20:04 字数 959 浏览 1 评论 0原文

我有一个实体,我们称之为 X。(该实体有一个 @Id id)。 该实体映射到一个表,我们称之为:X_TABLE。 它还包含

@ManyToOne
@JoinColumn(name = "JOIN_COLUMN")
YClass joinColumn;//another entity

我正在使用以下代码:

X x = new X(xDTO);//just a copy constructor, nothing fancy, without id
x.joinColumn = null;
entityManager.persit(x);
entityManager.flush();
join_column_id = somne_function();//irelevant
Query q = entityManager.createNativeQuery(
        "UPDATE  X_TABLE t SET t.JOIN_COLUMN=" + join_column_id + " WHERE " + " t.id= "    + x.getId());
q.executeUpdate();
q.flush();

万一有人想知道,这样做的要点是 join_column 是另一个表的外键,位于不同的数据库中,并且我通过本机设置它查询,因为对应的表不存在于包含 X_TABLE 的同一数据库中。但这不是问题。实体映射和关系都很好,我可以保证这一点。

上面的代码在循环中针对多个记录执行,有时对于所有记录都可以正常工作(保存并更新表)。但有时它对除循环中最后一条记录之外的所有记录都可以正常工作(从某种意义上说,它保存记录,但不在数据库中执行更新)。 没有抛出任何异常,并且 Hibernate 的 JBoss 服务器调试日志显示更新发生在正确的值上,没有错误。然而,该表在 JOIN_COLUMN 中仅包含一条记录的 null,我不知道为什么。有什么想法吗?

PS:该服务是容器管理事务。

I have an entity, let's call it X. (the entity has a @Id id).
The entity is mapped to a table, let's call it: X_TABLE.
It also contains a

@ManyToOne
@JoinColumn(name = "JOIN_COLUMN")
YClass joinColumn;//another entity

I'm working with the following code:

X x = new X(xDTO);//just a copy constructor, nothing fancy, without id
x.joinColumn = null;
entityManager.persit(x);
entityManager.flush();
join_column_id = somne_function();//irelevant
Query q = entityManager.createNativeQuery(
        "UPDATE  X_TABLE t SET t.JOIN_COLUMN=" + join_column_id + " WHERE " + " t.id= "    + x.getId());
q.executeUpdate();
q.flush();

The point in doing this, in case anyone is wondering, is that join_column is a foreign key to another table, in a different database, and I'm setting it through a native query because the coresponding table does not exist in the same database containing X_TABLE. But this isn't the problem. The enitity mappings and relations are all fine, I can garantee for that.

The code above is executed for several records in a loop, and it sometimes works fine (saves and updates the table fine) for all records. But sometimes it works fine for all records except for the last one in the loop (in the sense that it saves the record, but does not execute the update in the database). There are no Exceptions thrown and the JBoss server debug log for hibernate shows the UPDATE happening with the right values, with no error. Yet, the table contains null in JOIN_COLUMN for just one record and I have no ideea why. Any thoughts?

PS: The service is a Container Managed Transaction.

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

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

发布评论

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

评论(1

半城柳色半声笛 2024-11-23 02:20:04

几点建议:

  • 检查受executeUpdate()影响的行数
  • 您的事务策略是什么?定义了任何 TransactionManager 吗?

Few suggestions:

  • Check the number of rows affected by executeUpdate()
  • What is your Transaction policy? Any TransactionManager defined?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文