• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA + stored procedures + EntityManager

 
Anthony Sykes
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to get a database Connection object from the EntityManager using JPA. I have seen examples off getting a Connection object using the session object with hibernate. Is this possible using JPA without hibernate? Could someone provide examples of how to do this using JPA?
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes there is. However it's not something I like to do if I can avoid it. For one thing, you're doing work outside the band of the JPA framework. ORM technologies often gain a significant performance advantage over raw JDBC, but it's because they have a "big picture" view of what's going on and optimize accordingly. When you grab a connection directly, that's outside the picture. For another, you'd better make sure you always return that connection, or the connection pool will slowly bleed dry. The ORM framework automatically ensures that for its own internal work.

I also avoid stored procedures in general, since they often take program logic out of the program and store it in the database. Which means that to make sense of the program, you need access to the database as well.

However, there are times when it's the best thing to do. As far as a generic "get a connection method" goes, I'd have to go back and check the JPA javaDocs, but I'm pretty sure it's implementation-dependent (typically you cast the EntityManager to a vendor-specific extended EntityManager subclass). I don't worry too much about that, since I've already bought into other non-portable solutions if I got to that point.

You CAN invoke stored procedures directly in ORM systems, but there I'll have to refer you to the docs. Like I said, it's not something I like to do everyday.
 
Anthony Sykes
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could I possibly do it this way:

I have read examples that show you can get the database connection from the session...


Now can I manipulate the following:



from an Action on the ActionClass.
Also, where does this session come from? I am assuming this is something hibernate.

In this scenario, the utility class would be used to perform database interactions using stored procedures.


Tim Holloway wrote:Yes there is. However it's not something I like to do if I can avoid it. For one thing, you're doing work outside the band of the JPA framework. ORM technologies often gain a significant performance advantage over raw JDBC, but it's because they have a "big picture" view of what's going on and optimize accordingly. When you grab a connection directly, that's outside the picture. For another, you'd better make sure you always return that connection, or the connection pool will slowly bleed dry. The ORM framework automatically ensures that for its own internal work.

I also avoid stored procedures in general, since they often take program logic out of the program and store it in the database. Which means that to make sense of the program, you need access to the database as well.

However, there are times when it's the best thing to do. As far as a generic "get a connection method" goes, I'd have to go back and check the JPA javaDocs, but I'm pretty sure it's implementation-dependent (typically you cast the EntityManager to a vendor-specific extended EntityManager subclass). I don't worry too much about that, since I've already bought into other non-portable solutions if I got to that point.

You CAN invoke stored procedures directly in ORM systems, but there I'll have to refer you to the docs. Like I said, it's not something I like to do everyday.
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the session is Hibernate's equivalent to the JPA entityManager. It's an unfortunate name, since it confuses with Servlet HTTP Sessions, but so it goes.

These are all questions with little direct connection to JSF. We have an ORM forum that's more suitable. And guess what? I notice that someone else has a similar question! http://www.coderanch.com/t/435041/Object-Relational-Mapping/java/Stored-procedure-JPA

I see no answer so far, and he's asking about TopLink, but I bet you'll find other similar topics if you use the forum Search feature.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!