用Hibernate访问DB27.2的问题



// Cat类
public class Cat {

private String id;
private String name;
private char sex;
private float weight;

public Cat() {
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public char getSex() {
return sex;
}

public void setSex(char sex) {
this.sex = sex;
}

public float getWeight() {
return weight;
}

public void setWeight(float weight) {
this.weight = weight;
}

}
=====================================================
//cat.hbm.xml文件
<?xml version=
"1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name=
"hibernate.examples.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" sql-type="varchar(16)" not-null="true"/>
</property>

<property name=
"sex"/>

<property name=
"weight"/>

</class>

</hibernate-mapping>
===================================================
//测试类
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import java.util.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;


public class Test {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
private Connection con;

public static void main(String[] args) {
Test a=new Test();
a.Test();
}

public void Test() {
try {

// Load Configuration and build SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();

// Prepare out
PrintWriter out = new PrintWriter(System.out);

// Create some Cats
beginTransaction();
createCats(out);
endTransaction(true);

// Select all Cats
beginTransaction();
selectAllCats(out);
endTransaction(false);

// Select female Cats
beginTransaction();
selectFemaleCats(out);
endTransaction(false);

} catch (HibernateException e) {
e.printStackTrace();
;
}
}

public void createCats(PrintWriter out)
throws HibernateException {
out.print(
"<h3>Creating Cats:</h3>");
out.println(
"CREATING 'Princess'...<br/>");
Cat princess = new Cat();
princess.setName(
"Princess");
princess.setSex('F');
princess.setWeight(7.4f);
session.save(princess);

out.println(
"CREATING 'Max'...<br/>");
Cat max = new Cat();
max.setName(
"Max");
max.setSex('M');
max.setWeight(8.1f);
session.save(max);

out.println(
"CREATING 'Sophie'...<br/>");
Cat sophie = new Cat();
sophie.setName(
"Sophie");
sophie.setSex('F');
sophie.setWeight(4.1f);
session.save(sophie);
}

public void selectAllCats(PrintWriter out)
throws HibernateException {

out.print(
"<h3>Retrieving all Cats:</h3>");
String queryString =
"select cat from Cat as cat";
Query query = session.createQuery(queryString);
for (Iterator it = query.iterate(); it.hasNext();) {
Cat cat = (Cat) it.next();
out.println(
"CAT: " + cat.getName() + " (" + cat.getSex() + ", " + cat.getWeight() + ")<br/>");
}
}

public void selectFemaleCats(PrintWriter out)
throws HibernateException {

out.print(
"<h3>Retrieving female Cats:</h3>");
String queryString =
"select cat from Cat as cat where cat.sex = :sex";
Query query = session.createQuery(queryString);
query.setCharacter(
"sex", 'F');
for (Iterator it = query.iterate(); it.hasNext();) {
Cat cat = (Cat) it.next();
out.println(
"CAT: " + cat.getName() + " (" + cat.getSex() + ", " + cat.getWeight() + ")<br/>");
}
}

private void beginTransaction()
throws HibernateException {

session = sessionFactory.openSession();
transaction = session.beginTransaction();
}

private void endTransaction(boolean commit)
throws HibernateException {

if (commit) {
transaction.commit();
} else {
// Don't commit the transaction, can be faster for read-only operations
transaction.rollback();
}
session.close();
}
}
======================
//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=
"hibernate.connection.driver_class">COM.ibm.db2.jdbc.app.DB2Driver</property>
<property name=
"hibernate.connection.url">jdbc:db2:SKMRP</property>
<!-- <property name=
"hibernate.connection.url">jdbc:db2://s7:50000/SKMRP</property>-->
<property name=
"hibernate.connection.username">db2admin</property>
<property name=
"hibernate.connection.password">db2admin</property>
<!-- <property name=
"hibernate.connection.pool_size">20</property>-->
<!-- <property name=
"hibernate.statement_cache.size">3</property>-->
<!-- <property name=
"hibernate.connection.isolation"></property>-->

<property name=
"hibernate.show_sql">false</property>
<property name=
"hibernate.use_outer_join">true</property>
<property name=
"hibernate.dialect">net.sf.hibernate.dialect.DB2Dialect</property>

<!-- Mapping files -->
<mapping resource=
"Cat.hbm.xml"/>

</session-factory>

</hibernate-configuration>



// 结果
13:03:57,000 INFO Environment:377 - Hibernate 2.0 beta 5
13:03:57,010 INFO Environment:406 - hibernate.properties not found
13:03:57,020 INFO Environment:426 - using CGLIB reflection optimizer
13:03:57,020 INFO Environment:436 - JVM proxy support: true
13:03:57,030 INFO Configuration:669 - Configuration resource: /hibernate.cfg.xml
13:03:57,240 INFO Configuration:251 - Mapping resource: Cat.hbm.xml
13:03:57,431 INFO Collection:166 - Mapping class: hibernate.examples.Cat -> CAT
13:03:57,611 INFO Configuration:851 - Configured SessionFactory: null
13:03:58,332 INFO SessionFactoryImpl:122 - building session factory
13:03:58,352 INFO Dialect:36 - Using dialect: net.sf.hibernate.dialect.DB2Dialect
13:03:58,362 INFO DriverManagerConnectionProvider:40 - Hibernate connection pool size: 20
13:03:58,502 INFO DriverManagerConnectionProvider:69 - using driver: COM.ibm.db2.jdbc.app.DB2Driver at URL: jdbc:db2:SKMRP
13:03:58,502 INFO DriverManagerConnectionProvider:70 - connection properties: {user=db2admin, password=db2admin}
13:03:58,502 INFO SessionFactoryImpl:155 - Use outer join fetching: true
java.lang.AbstractMethodError: COM.ibm.db2.jdbc.app.DB2DatabaseMetaData.supportsResultSetType(I)Z
at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:163)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:594)
at hibernate.examples.Test.Test(Test.java:34)
at hibernate.examples.Test.main(Test.java:27)
Exception in thread
"main" Process terminated with exit code 1

不知道是我配置文件有问题,还是Hibernate对DB2支持有问题。特向
有相关经验的朋友请教。

别乱想了,也不看看仔细,明明是找不到配置文件,也不知道你把hibernate.properties放到哪里去了。

hibernate.properties确实是没找到,不过
好像hibernate从其他地方(hibernate.cfg.xml?)取得
了所需的参数(url,username,password...)。

java.lang.AbstractMethodError: COM.ibm.db2.jdbc.app.DB2DatabaseMetaData.supportsResultSetType(I)Z at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:163)
估计确实是db2驱动的问题(不敢确定)。

问题可能是DB2的驱动包使用的版本不正确所导致,我也遇到过此类问题,但已经解决。

我在hibernate.cfg.xml中把数据源改为MySql的,程序就运行成功了。
但是hibernate是支持DB2的,不知道哪里做的不对?

我也遇到过这样的问题,我现在是重新找了个db2的驱动,然后就好了。你还是换个db2驱动试试看。

> 我也遇到过这样的问题,我现在是重新找了个db2的驱动,然?> 就好了。你还是换个db2驱动试试看。

请问你用的是什么版本的db2驱动?是6.1的7.2的还是8.1的?
是英文版的还是中文版的?

我现在使用的就是DB27.2的英文版本,当时也出现过和你一样的问题,后来将DB27.2下的DB2驱动替换掉以前的包,一切就OK!!

问题解决了,并不是驱动程序有问题,一开始的问题解决方向错了。
随正版DB2发布的驱动怎么会有问题呢?不兼容就更谈不上了。
我想IBM也不至于如此差劲吧。

所以调整思路后发现DB2安装的时候会自动设置系统级别ClassPath=
.;F:\SQLLIB\java\db2java.zip;F:\SQLLIB\java\runtime.zip;F:\SQLLIB\java\sqlj.zip;F:\SQLLIB\bin

所以照葫芦画瓢把F:\SQLLIB\bin加入程序的ClassPath后就搞定了。;-)
郁闷,这样的问题也过了这么久才解决!

考虑一下,把经验发表在 Hibernate中文论坛上吧。