Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The user must supply a JDBC connection - Hibernate Error

 
Vinod Kumar
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Friends,

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.

Configuration cfg = new Configuration()
.addResource("com/hibernate/test/User.hbm.xml")
.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect")
setProperty("connection.driver_class","com.microsoft.jdbc.sqlserver.SQLServerDriver")
.setProperty("connection.url","jdbc:microsoft:sqlserver://sqlser:1930/dbname")
.setProperty("connection.username","user1")
.setProperty("connection.password","pwd1");
test.factory = cfg.buildSessionFactory();
List users = test.viewAllUsers();
=======
viewAllUsers() method

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 JDBC connection
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)
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Mark
 
Vinod Kumar
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>

<!-- properties -->
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://server:1930/dbname
</property>
<property name="connection.username">usr1</property>
<property name="connection.password">pwd!</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="show_sql">false</property>

<!-- mapping files -->
<mapping resource="com/hibernate/test/User.hbm.xml" />

</session-factory>

</hibernate-configuration>
[ March 06, 2008: Message edited by: Vinod Kumar ]
 
Vinod Kumar
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please ignore my request...I found my mistake, for SQL Server 2005, there is a differece in connection URL string.

<property name="connection.url">

jdbc:sqlserver://server1:1930

</property>

Thanks,
Vinod
[ March 06, 2008: Message edited by: Vinod Kumar ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic