0

Hibernate has deprecated the connection() because they think the framework is so awesome that no-one will ever need the connection. And no, this is NOT the same thing as providing the connection, unless you are coming from Haskell. Anyways, I have a better idea to get rid of hibernate: a Connection wrapper arounds the hibernate session so I can pass the session around as a connection. I heard there is a way to get the connection with reflection. Does anyone know where I can get a wrapper like that?

public class SessionConnection implements Connection {

   private final Session session;
   private final Connection conn;

   public SessionConnection(Session session) {
       this.session = session;
       this.conn = getConnectionFromStupidHibernate(session);
   }

   // delegate methods go here
}
Community
  • 1
  • 1
chrisapotek
  • 6,007
  • 14
  • 51
  • 85
  • 1
    I don't know, but please tell me if you find out as I have been always wanted to do this. – JohnPristine May 01 '12 at 23:18
  • 1
    Hmm...I agree that this is a bit of a pain, but why wouldn't the doWork() solution work for you (no pun intended) ? – GreyBeardedGeek May 01 '12 at 23:31
  • Java is not a functional language. I want to pass the object around so clients can do whatever they want with it. To pass a function or closure around is another thing very different. – chrisapotek May 01 '12 at 23:53
  • Hibernate team didn't have to think their framework is so awesome to make this change. They might also think there's a finite area that they want to cover. Exposing the raw connection means for them dealing with innumerable angry posts complaining about yet another "bug" in Hibernate. – Marko Topolnik May 02 '12 at 07:52
  • 1
    Hibernate is like learning Spanish on top of English so you can speak English better. It is a solution to a problem that creates a big problem than the original one. Other than that it is great! – chrisapotek May 02 '12 at 16:23

2 Answers2

4

If you're using spring, just use the same DataSource you use for your SessionFactory - don't design your app around hibernate, fit hibernate into your design

ianpojman
  • 1,743
  • 1
  • 15
  • 20
  • of course, this has nothing to do with Spring - but usually Spring encourages you to build together a system out of independent objects , rather than just stringing together everything in code in the first way that jumps to mind. anyway setting up hib/spring already has a bean of type DataSource defined somewhere. So all you gotta do is autowire it where you need it – ianpojman May 02 '12 at 20:01
0

You need a SessionFactory and a ConnectionProvider -- see Hibernate: Providing Connections.

ZeroOne
  • 3,041
  • 3
  • 31
  • 52