用Hibernate访问DB27.2的问题

grape88 03-08-28



// 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>

grape88
2003-08-28 12:56



// 结果
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

grape88
2003-08-28 12:58

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

robbin
2003-08-28 15:30

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

yehs220
2003-08-28 16:38

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驱动的问题(不敢确定)。

3Go 1 2 3 下一页