I am new to hibernate. I am trying out some examples. Here is what I did.
a) Created a User.hbm.xml mapping which maps 5 column names out of 10 columns from t_user table. b) Created User.java with those mapped columns (setter and getter) c) Created a stand along class with main method to test.
Following is the code snippet which selects all users from t_user table.
List users = null; Session s = factory.openSession(); System.out.println("Connection:"+s.isConnected()); //this is printing Transaction tx=null; try { tx = s.beginTransaction(); System.out.println ("Transaction"+tx.isActive()); //this is not printing. users = s.createQuery("from User").list(); tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); throw e; } finally { s.close(); }
Exception is : Mar 5, 2008 4:08:08 PM org.hibernate.impl.SessionFactoryObjectFactory addInstance INFO: Not binding factory to JNDI, no JNDI name configured Connection:true java.lang.UnsupportedOperationException: The user must supply a JDBCconnection at org.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:30) at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:423) at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144) at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119) at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57) at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
Yep, you have no JDBC connections for Hibernate to use, Hibernate comes with C3PO Connection Pool. I highly recommend either using Annotations or a hibernate.cfg.xml instead of building your SessionFactory from code, but that is my preference and for samples and tests you are ok doing it in code, I just tend to not do it that way.
Thanks, I am using XML now. I downloaded the MSSQL2005 sqljdbc.jar file and is in my class path along with other hibernate jars. I am using RAD6.0. The error is :
SEVERE: No suitable driver org.hibernate.exception.JDBCConnectionException: Cannot open connection
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name --> <session-factory>