i am following the quickstart with tomcat and the example of the Cat in the hibernate reference docs, and i still have problems at runtime when my servlet recalls a simple method of HibernateUtil.java, that follows:
Code:
HibernateUtil.java
public static void connessione(){
Session session = HibernateUtil.currentSession();
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();
}
Hibernate version: 3.0
Full stack trace of any exception that occurs:Code:
org.hibernate.exception.GenericJDBCException: Cannot open connection
org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:427)
org.hibernate.jdbc.JDBCContext.connect(JDBCContext.java:168)
org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:103)
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:49)
org.hibernate.transaction.JDBCTransactionFactory.beginTransaction(JDBCTransactionFactory.java:24)
org.hibernate.jdbc.JDBCContext.beginTransaction(JDBCContext.java:231)
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1073)
org.hibernate.examples.quickstart.HibernateUtil.connessione(HibernateUtil.java:63)
prova.processRequest(prova.java:56)
prova.doGet(prova.java:72)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
Mapping documents:hibernate.cfg.xml:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.datasource">java:comp/env/jdbc/quickstart2</property>
<property name="show_sql">false</property>
<!-- Mapping files -->
<mapping resource="org/hibernate/examples/quickstart/Cat.hbm.xml"/>
</session-factory>
</hibernate-configuration>
i found useful editing the context.xml of my webapp, setting the resourceLink with the name of the connection provided by tomcat through JNDI
when i was setting the name with the path of the JDBC provided connection, it was saying "could not find datasource"
context.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/quickstart2">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="quickstart2." suffix=".log" timestamp="true"/>
<ResourceLink name="java:comp/env/jdbc/quickstart2" global="java:comp/env/jdbc/quickstart2"
type="javax.sql.DataSource"/>
</Context>
Cat.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.hibernate.examples.quickstart.Cat" table="CAT">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="CAT_ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="name">
<column name="NAME" length="16" not-null="true"/>
</property>
<property name="sex"/>
<property name="weight"/>
</class>
</hibernate-mapping>
Name and version of the database you are using:
mysql 4.10
(maybe there's an error in something like JTAFactory?)