表有时不会通过 Hibernate 本机查询进行更新
我有一个实体,我们称之为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几点建议:
Few suggestions: