显示标签为“Hibernate”的博文。显示所有博文
显示标签为“Hibernate”的博文。显示所有博文

2009年3月11日星期三

test.java, customer.java

// test.java

import org.hibernate.*;
import org.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();
}
}
}

// customer.java
public class Customer {

private int id;
private String username;
private String password;


public int getId() {
return id;
}

public String getPassword() {
return password;
}

public String getUsername() {
return username;
}

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

public void setPassword(String password) {
this.password = password;
}

public void setUsername(String username) {
this.username = username;
}

}

Customer.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Customer" table="CUSTOMER">
<id name="id" column="CID" type="java.lang.Integer">
<generator class="increment" />
</id>
<property name="username" column="USERNAME" type="java.lang.String"/>
<property name="password" column="PASSWORD" type="java.lang.String"/>
</class>
</hibernate-mapping>

hibernate.cfg.xml

<?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">

<hibernate-configuration>

<session-factory name="java:/hibernate/HibernateFactory">

<property name="show_sql">true</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/javatest
</property>
<property name="connection.username">
javatestor
</property>
<property name="connection.password">
secret
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>

<mapping resource="Customer.hbm.xml" /> <!-- 指定Customer的映射文件 -->

</session-factory>

</hibernate-configuration>

apache build.xml for a hibernate demo

<?xml version="1.0" encoding="utf-8"?>

<project name="build.xml" default="build">

<!-- path -->
<property name="src.java.dir" location="src"/>
<property name="build.classes.dir" location="classes"/>
<property name="hibernate.lib.dir" location="C:/java/hibernate-distribution-3.3.1.GA"/>
<property name="mysql.lib.dir" location="C:/java/mysql-connector-java-3.1.14"/>

<!-- classpath -->
<path id="project.classpath">
<pathelement location="${build.classes.dir}"/>
</path>
<path id="library.path">
<fileset dir="${hibernate.lib.dir}/lib">
<include name="**/required/*.jar"/>
</fileset>
<fileset dir="${hibernate.lib.dir}">
<include name="hibernate3.jar"/>
</fileset>
<fileset dir="${mysql.lib.dir}">
<include name="mysql-connector-java-3.1.14-bin.jar"/>
</fileset>
</path>

<!-- action -->
<target name="init">
<mkdir dir="${build.classes.dir}"/>
</target>

<target name="build" depends="init">
<javac srcdir="${src.java.dir}" destdir="${build.classes.dir}">
<classpath refid="project.classpath"/>
<classpath refid="library.path" />
</javac>
<copy todir="${build.classes.dir}" >
<fileset dir="${src.java.dir}" >
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>

<target name="run" depends="build">
<java classname="Test" fork="true">
<classpath refid="project.classpath"/>
<classpath refid="library.path" />
</java>
<echo>If you see this,it works!!!</echo>
</target>

<target name="clean">
<delete dir="${build.classes.dir}"/>
</target>

</project>