浏览 5097 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2004-12-04
和不成功。 hibernate.properties配置文件和hibernate.cfg.xml配置文件能同时使用吗? 当我单独用hibernate.cfg.xml配置文件(代码在下边)的时候程序是正确的。 当我单独用hibernate.properties配置文件(代码在下边)的时候出错(错误提示在下边) 当我一起使用hibernate.properties配置文件和hibernate.cfg.xml配置文件的时候出错(错误提示在下边) ----------------------------------------------------- -------------------------------------- hibernate.properties配置文件中MySQL的配置信息 ## MySQL hibernate.dialect net.sf.hibernate.dialect.MySQLDialect hibernate.connection.driver_class com.mysql.jdbc.Driver hibernate.connection.url jdbc:mysql://localhost:3306/quickstart hibernate.connection.username root hibernate.connection.password ----------------------- hibernate.cfg.xml配置文件 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.datasource">java:comp/env/jdbc/quickstart</property> <property name="show_sql">false</property> <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property> <!-- Mapping files --> <mapping resource="Cat.hbm.xml"/> </session-factory> </hibernate-configuration> 现在的问题是我单独用hibernate.cfg.xml就可以运行。 ------------------------------------------------- 而我单独用hibernate.properties出现错误如下: exception javax.servlet.ServletException org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758) org.apache.jsp.testdb.test_jsp._jspService(test_jsp.java:108) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94) javax.servlet.http.HttpServlet.service(HttpServlet.java:856) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:298) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236) javax.servlet.http.HttpServlet.service(HttpServlet.java:856) root cause java.lang.NoClassDefFoundError org.apache.jsp.testdb.test_jsp._jspService(test_jsp.java:67) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94) javax.servlet.http.HttpServlet.service(HttpServlet.java:856) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:298) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236) javax.servlet.http.HttpServlet.service(HttpServlet.java:856) ------------------------------- 我将hibernate.properties、hibernate.cfg.xml一起用,出错误如下: exception org.apache.jasper.JasperException org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:346) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236) javax.servlet.http.HttpServlet.service(HttpServlet.java:856) root cause java.lang.UnsupportedOperationException org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:116) org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:554) net.sf.hibernate.connection.DatasourceConnectionProvider.getConnection(DatasourceConnectionProvider.java:56) net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:286) net.sf.hibernate.impl.SessionImpl.connect(SessionImpl.java:3326) net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3286) net.sf.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:40) net.sf.hibernate.transaction.JDBCTransactionFactory.beginTransaction(JDBCTransactionFactory.java:19) net.sf.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:2231) org.apache.jsp.testdb.test_jsp._jspService(test_jsp.java:69) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94) javax.servlet.http.HttpServlet.service(HttpServlet.java:856) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:298) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236) javax.servlet.http.HttpServlet.service(HttpServlet.java:856) ------------------------------------------------------------------------------ 环境变量设置如下: C:\j2sdk1.4.2_04\lib\dt.jar; C:\j2sdk1.4.2_04\lib\tools.jar; E:\quickstart\WEB-INF\lib\cglib-full-2.0.2.jar; E:\quickstart\WEB-INF\lib\commons-collections-2.1.1.jar; E:\quickstart\WEB-INF\lib\commons-dbcp-1.2.1.jar; E:\quickstart\WEB-INF\lib\commons-lang-1.0.1.jar; E:\quickstart\WEB-INF\lib\commons-logging-1.0.4.jar; E:\quickstart\WEB-INF\lib\commons-pool-1.2.jar; E:\quickstart\WEB-INF\lib\dom4j-1.4.jar; E:\quickstart\WEB-INF\lib\ehcache-0.9.jar; E:\quickstart\WEB-INF\lib\hibernate2.jar; E:\quickstart\WEB-INF\lib\jta.jar; E:\quickstart\WEB-INF\lib\log4j-1.2.8.jar; E:\quickstart\WEB-INF\lib\odmg-3.0.jar; C:\tomcat\common\lib\servlet.jar; ---------------------------------------------- server.xml数据库连接池配置如下: <Context path="/quickstart" docBase="e:/quickstart" reloadable="true" debug="0"> <Resource auth="Container" name="jdbc/quickstart" global="jdbc/quickstart" scope="Shareable" type="javax.sql.DataSource"/> <ResourceParams name="jdbc/quickstart"> <parameter> <name>factory</name> <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> </parameter> <!-- DBCP database connection settings --> <parameter> <name>url</name> <!--<value>jdbc:mysql://localhost:3306/quickstart?useUnicode=true&characterEncoding=GB2312</value>--> <value>jdbc:mysql://localhost:3306/quickstart</value> </parameter> <parameter> <name>driverClassName</name> <value>com.mysql.jdbc.Driver</value> <!--<value>org.gjt.mm.mysql.Driver</value>--> </parameter> <parameter> <name>username</name> <value>root</value> </parameter> <parameter> <name>password</name> <value></value> </parameter> <!-- DBCP connection pooling options --> <parameter> <name>maxWait</name> <value>3000</value> </parameter> <parameter> <name>maxIdle</name> <value>100</value> </parameter> <parameter> <name>maxActive</name> <value>10</value> </parameter> </ResourceParams> </Context> --------------------------------------- test.jsp内容如下: <%@ page language="java" pageEncoding="GB2312" %> <%@ page import="net.sf.hibernate.Transaction"%> <%@ page import="net.sf.hibernate.Session"%> <%@ page import="net.sf.hibernate.cfg.*"%> <%@ page import="net.sf.hibernate.Query"%> <%@ page import="net.sf.hibernate.examples.quickstart.HibernateUtil"%> <%@ page import="net.sf.hibernate.examples.quickstart.Cat"%> <%@ page import="java.util.*"%> <!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en"> <html> <head><title>Lomboz JSP</title></head> <body bgcolor="#FFFFFF">Hi <% try { //添加一只Cat Session ses = HibernateUtil.currentSession(); out.println(ses.toString()+"<br>"); Transaction tx= ses.beginTransaction(); Cat princess = new Cat(); princess.setName("HHH"); princess.setSex('F'); princess.setWeight(10.5f); ses.save(princess); tx.commit(); HibernateUtil.closeSession(); //读取库里所有Cat ses = HibernateUtil.currentSession(); tx= ses.beginTransaction(); Query query = ses.createQuery("select c from Cat as c where c.sex = :sex"); query.setCharacter("sex", 'F'); for (Iterator it = query.iterate(); it.hasNext();) { Cat cat = (Cat) it.next(); out.println("Female Cat: " + cat.getName() +"<br>"); } tx.commit(); HibernateUtil.closeSession(); } catch(Exception excep) { out.println(excep+"<br>"); excep.printStackTrace(); throw excep; } %> </body> </html> --------------------------------------------- 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2004-12-04
对于那些已经成功运行此例此例的朋友,希望能帮忙调试一下。
下载此例后,调试成功后的朋友,希望给些指点。谢谢。 |
|
返回顶楼 | |
发表时间:2004-12-06
从错误上看应该是你的数据库连接池的配置有问题,和事务没有关系
|
|
返回顶楼 | |
发表时间:2004-12-06
net.sf.hibernate.JDBCException: Could not execute JDBC batch update
这个问题已经解决了,正如tonybear朋友所说的。往楼下看:) |
|
返回顶楼 | |
发表时间:2004-12-07
mysql你用的什么版本,password不能为空
|
|
返回顶楼 | |
发表时间:2004-12-07
myepoch 写道 net.sf.hibernate.JDBCException: Could not execute JDBC batch update
现在出现了新的错误。 出現這個錯誤,是你運行第二遍了。\ 運行第一遍時記錄已經insert, 運行第二遍時cat_id重複了。 因爲cat_id的長度爲20,“uuid.hex”生成的長度爲32,被截取的前20位沒有變化,在cat表裏把cat_id的長度改爲32,就OK了。 |
|
返回顶楼 | |