Hello,
I'm currently working on a project using Hibernate, Spring, and some direct JDBC. In this project, we have a portion of the code that uses JDBC to persist data. Another part of the code uses Hibernate. We have a service which needs to call both within the same transaction.
First, a JPA object is created for a new User object. Then the our UserDAO class uses the Spring method getJpaTemplate().persist(user), which uses Hibernate to save the new record in table "user" with primary key "userId". Next, our JDBC service is called, which tries to create a record in another table (let's call it "jdbc_table"), putting the userId value in it's own column ("jdbc_table.userId"). The "userId" column in "jdbc_table" has a foreign key constraint with the user table, so it enforces that the user record exists. Since I'm calling the UserDAO method first, I expect that this should work, since I'm first creating the user record, then I'm creating a record in jdbc_table.
However, it appears the Hibernate DAO call has not persisted within the transaction yet, since when I try to make the JDBC call, I get a foreign key constraint error saying that my new user record does not exist.
Without committing the transaction, is there any way to have Hibernate let JDBC know that this database user value exists (or will exist when the transaction is committed)? I can't easily switch the JDBC code over to Hibernate, so that is not an option at this point. The transaction is managed by Spring, which commits the transaction at the end of the service call.
Has anybody run into this situation, and what's a workable solution?
Thanks,
Matthew
|