I use MS SQL Server as my datastore.
I have deployed my JNDI,JDBC in my webcontainer:Tomcat5.0.
This is the deploy details:
JNDI Name: jdbc/testDB
Data Source URL: jdbc:microsoft:sqlserver://127.0.0.1:1433;
databaseName=Hibernate
JDBC Driver Class: com.microsoft.jdbc.sqlserver.SQLServerDriver
And this is my Servlet:
Code:
package net.hibernate.quickstart;
import javax.servlet.*;
import javax.servlet.http.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class SaveCats extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,java.io.IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,java.io.IOException {
try {
// Session session = HibernateUtil.currentSession();
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
Transaction tx= session.beginTransaction();
Cat princess = new Cat();
princess.setName("Princess");
princess.setSex('F');
princess.setWeight(7.4f);
session.save(princess);
tx.commit();
HibernateUtil.closeSession();
}
catch(HibernateException he) {
// response.getWriter().print("Exception: " + he);
he.printStackTrace(response.getWriter());
}
}
When i run the servlet,it thrown exceptions:
net.sf.hibernate.JDBCException: could not insert: [net.hibernate.quickstart.Cat#297e3afc05b86a340105b86a3a840001]
...
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Invalid object name 'Cat'.
...
This tell me, the table
Cat is not vaild,I checked my Cat.hbm.xml,it's correct.So, i copy my Cat.db in a database named
Hibernate to the default example database
Northwind in MS SQL Server.
Then i run my servlet again,this time,a record insert to Northwind.Cat correctly?
I feel quite strange,I have set my JNDI JDBC URL to:
jdbc:microsoft:sqlserver://127.0.0.1:1433;
databaseName=Hibernate
With a specific database
Hibernate.
But why the hibernate find the Cat in Northwind?
Can u help me?