简体   繁体   中英

Table will not UPDATE sometimes through a Hibernate native query

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, in the database). 数据库中 )。 There are and the JBoss server debug log for hibernate shows the UPDATE happening with the right values, with no error. 并且休眠的JBoss服务器调试日志显示了使用正确的值进行的UPDATE,并且没有错误。 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.

Few suggestions:

  • Check the number of rows affected by executeUpdate()
  • What is your Transaction policy? Any TransactionManager defined?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM