// 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>
<p class="indent">
|