Results 1 to 5 of 5
- 05-14-2009, 09:42 AM #1
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
PreparedStatement and Hibernate session
Hi, I want to retrieve a Preparedstatement object from a hibernate Session but now that the connection() method of hibernate Session has been Deprecated, how do I do it.
I want to achieve same result which could be achieved by the the below code:
The problem with the above code is that the "st" object is null when it executes since the connection method has been Deprecated...I may be wrong....any ideas, referrals would be appreciated.SessionFactory sessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
private Session session =sessionFactory.openSession();
PreparedStatement st = session.connection().prepareStatement("{update DMETERS dm set dm.shape = ? where dm.objectid = ?}");
st.setString(1,"value for shape" );
st.setString(2, objectid);
st.execute();Tshegofatso Manakana
a.k.a Untouchable ™
- 05-14-2009, 01:06 PM #2
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
Hi
I have found a work around after spending an hour studying the Hibernate documentation. This seems to work just fine:
ConnectionProvider cp = impl.getConnectionProvider();
Connection conn = cp.getConnection();
String sqlString = "update CE_DOMESTIC_METERS dm set dm.shape = ? where objectid = ?";
PreparedStatement st = conn.prepareStatement(sqlString);Tshegofatso Manakana
a.k.a Untouchable ™
- 10-21-2009, 04:16 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 1
- Rep Power
- 0
Could you post the impl code
What is impl. here?
Could you please code the complete code please?
Aslos how do you manage closing the commenction?
Thanks,
- 10-21-2009, 04:53 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 25
- Rep Power
- 0
If you are not tied down to using PreparedStatement, you could use Hibernate's SQLQuery.
Java Code:int userId = 5; String sql = "select * from your_table as yt where user_id = :user_id"; SQLQuery query = session.createSQLQuery(sql); query.setInteger("user_id", userId); List<?> yourStuff = query.list();Last edited by literallyjer; 10-21-2009 at 04:54 PM. Reason: runtime error
- 10-22-2009, 02:53 PM #5
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
Hi, here is the complete code.
Java Code:import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.persister.entity.SingleTableEntityPersister; try { // Create the SessionFactory from standard (hibernate.cfg.xml) config file. sessionFactory = new Configuration().configure().buildSessionFactory(); session = sessionFactory.openSession(); } catch (Throwable ex) { // Log the exception. System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } SessionFactoryImplementor impl = (SessionFactoryImplementor)sessionFactory; ConnectionProvider cp = impl.getConnectionProvider(); Connection conn = cp.getConnection(); String sqlString = "query in here"; PreparedStatement st = conn.prepareStatement(sqlString); st.setInt(1, domMeters.getObjectid()) int results = st.executeUpdate(); conn.commit(); st.close();Tshegofatso Manakana
a.k.a Untouchable ™
Similar Threads
-
hibernate session = null in the run() method of TimerTask
By fabs in forum Threads and SynchronizationReplies: 1Last Post: 09-17-2009, 03:12 PM -
How to Kill session at application context level by using session Id
By Kishore.Kumar in forum Java ServletReplies: 1Last Post: 04-21-2009, 11:20 PM -
NoClassDefFoundError: org/hibernate/Session
By simon in forum JDBCReplies: 3Last Post: 08-01-2007, 04:34 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks