Results 1 to 5 of 5
  1. #1
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default 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:

    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();
    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.
    Tshegofatso Manakana
    a.k.a Untouchable

  2. #2
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    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

  3. #3
    adas is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Question 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,

  4. #4
    literallyjer is offline Member
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default

    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 05:54 PM. Reason: runtime error

  5. #5
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    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

  1. hibernate session = null in the run() method of TimerTask
    By fabs in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-17-2009, 04:12 PM
  2. Replies: 1
    Last Post: 04-22-2009, 12:20 AM
  3. Replies: 3
    Last Post: 08-01-2007, 05:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •