public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}
将此类存为c:\workspace\My1stHibernate\src\Customer.java文件。
编写Test类
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class Test {
public static void main(String[] args) {
try {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
for (int i = 0; i < 200; i++) {
Customer customer = new Customer();
customer.setUsername("customer" + i);
customer.setPassword("customer");
session.save(customer);
}
tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
将此类存为c:\workspace\My1stHibernate\src\Test.java文件。
创建Hibernate映射文件
因为这里只有一个Class --- Customer 和一个Table --- CUSTOMER,你只需要建立一个映射文件--- Customer.hbm.xml,来对应Customer类和CUSTOMER表之间的关系。
<?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="Customer" table="CUSTOMER">
<id name="id" column="CID">
<generator class="increment" />
</id>
<property name="username" column="USERNAME" />
<property name="password" column="PASSWORD" />
</class>
</hibernate-mapping>
把此文件存为c:\workspace\My1stHibernate\src\Customer.hbm.xml,和Customer.java放在同一目录下。
编写Ant build.xml文件
你不一定要知道这个build.xml的细节,其实Ant也不是Hibernate所必须的。这里用Ant是为了简化一些任务,比如: 编译、copy、运行,等。
<?xml version="1.0" ?>
<project name="My1stHibernate" default="build" basedir=".">
<property name="base.dir" value="." />
<property name="src.dir" value="src" />
<property name="lib.dir" value="lib" />
<property name="build.dir" value="classes" />
<path id="myclasspath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
<pathelement location="${build.dir}" />
</path>
<target name="init">
<mkdir dir="${build.dir}" />
</target>
<target name="build" depends="init" description="compile the source files">
<javac classpathref="myclasspath" srcdir="${src.dir}" destdir="${build.dir}" />
<copy todir="${build.dir}" >
<fileset dir="$
|