最近在学hibernate,由于是菜鸟,只能看一些入门文章,按它的知道做。
首先在Tomcat中的conf/server.xml中加入数据源配置:(hibernate是我的工程名)
<Context path="/hibernate" docBase="D:\eclipseWorkPlace\hibernate\WebRoot" debug="0" reloadable="true" crossContext="true">
<Resource
name="jdbc/hibernate"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="test_hibernate"
password="test_hibernate"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://192.168.7.6:1433/test_hibernate"
/>
</Context>在hibernate/WebRoot/WEB-INF/classes/下放置hibernate.cfg.xml文件,如下:
<!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/hibernate</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Mapping files -->
<mapping resource="Course.hbm.xml"/>
</session-factory>
</hibernate-configuration>在hiberante.cfg.xml中没有配置数据源url、username、password等,因为已在数据源中设好了,没错吧?!不知道以上配置是否有错误?(jdk1.5+tomcat5.5.20+hibernate3.1,IDE用myeclise5.5.1+eclipse3.2)因为运行查找或删除时,报错说jdbc驱动及url为空,得不到conection连接。(用的是hibernate事务操作)大吓请指点!!
首先在Tomcat中的conf/server.xml中加入数据源配置:(hibernate是我的工程名)
<Context path="/hibernate" docBase="D:\eclipseWorkPlace\hibernate\WebRoot" debug="0" reloadable="true" crossContext="true">
<Resource
name="jdbc/hibernate"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="test_hibernate"
password="test_hibernate"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://192.168.7.6:1433/test_hibernate"
/>
</Context>在hibernate/WebRoot/WEB-INF/classes/下放置hibernate.cfg.xml文件,如下:
<!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/hibernate</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Mapping files -->
<mapping resource="Course.hbm.xml"/>
</session-factory>
</hibernate-configuration>在hiberante.cfg.xml中没有配置数据源url、username、password等,因为已在数据源中设好了,没错吧?!不知道以上配置是否有错误?(jdk1.5+tomcat5.5.20+hibernate3.1,IDE用myeclise5.5.1+eclipse3.2)因为运行查找或删除时,报错说jdbc驱动及url为空,得不到conection连接。(用的是hibernate事务操作)大吓请指点!!
auth="Container"
type="javax.sql.DataSource"/> <ResourceParams name="jdbc/TestDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter> <!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter> <!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter> <!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter> <!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>javauser</value>
</parameter>
<parameter>
<name>password</name>
<value>javadude</value>
</parameter> <!-- Class name for mm.mysql JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter> <!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>
</parameter>
</ResourceParams>
[/code]
还有,你的那种写法我也试过了,我看网上说5.0的用你那种,5.5的就用我那种,不知道对否?
jdbc的驱动和url都没有..给你发个我用的看看.
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="connection.username">MyTest</property>
<property name="connection.url">
jdbc:oracle:thin:@10.1.2.117:1521:ORCL
</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="myeclipse.connection.profile">orcl</property>
<property name="connection.password">1212</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<mapping resource="myhibernate/Mytesttable.hbm.xml" /> </session-factory></hibernate-configuration> <property name="connection.url">
jdbc:oracle:thin:@10.1.2.117:1521:ORCL
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver少这两个..
我参考的是http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=833
你可以试试看我那种可以不可以..
我不知道如果你在tomcat那设置了的话,hibernate能不能找到,或者tomcat是不是会用什么办法告诉hibernate它的配置..
我是刚开始学hibernate的,想问下lz那种方法比较常用还是直接在hibernate.cfg.xml里配置url和dbdriver的常用啊?如果在tomcat里配置有什么好处么?
添加如下代码即可:
<resource-ref>
<!-- 对所引用的资源的说明 -->
<description>DB Connection</description>
<!-- 指定所引用资源的JNDI名字,与<Resource>元素中的name属性相对应 -->
<res-ref-name>jdbc/hibernate</res-ref-name>
<!-- 指定所引用资源的类名字,与<Resource>元素中的type属性相对应 -->
<res-type>javax.sql.DataSource</res-type>
<!-- 指定管理所引用的资源的Manager,与<Resource>元素中的auth属性对应 -->
<res-auth>Container</res-auth>
</resource-ref>多谢各位!