-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Combining Hibernate and JDBC
PostPosted: Wed Jan 06, 2010 6:17 pm 
Newbie

Joined: Wed Jan 06, 2010 5:49 pm
Posts: 1
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


Top
 Profile  
 
 Post subject: Re: Combining Hibernate and JDBC
PostPosted: Fri Jan 08, 2010 10:43 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
Hi,

considering your requirements the only way I can think of is to tell hibernate to persist its internal state by calling flush() on the session/entityManager/jpaTemplate.

This will not commit the transaction, but you have to take care not to use autocommit on your jdbc-connections (but autocommit is evil anyway).

This way will kill off some of hibernate's performance-gain since it kills transactional write behind, but that's the price you have to pay....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.