/*
Test.java 编译及运行 命令行:
C:\J2SDK\bin\javac -classpath "E:\Hibernate\hibernate-2.1.2\hibernate-2.1\hibernate2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\ant-1.5.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\ant-optional-1.5.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\c3p0-0.8.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\cglib-2.0-rc2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-collections-2.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-dbcp-1.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-lang-1.0.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-logging-1.0.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-pool-1.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\concurrent-1.3.2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\connector.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\dom4j-1.4.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\ehcache-0.6.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jaas.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-cache.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-common.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-jmx.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-system.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jcs-1.0-dev.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jdbc2_0-stdext.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jgroups-2.2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jta.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\junit-3.8.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\log4j-1.2.8.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\odmg-3.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\oscache-2.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\proxool-0.8.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\swarmcache-1.0rc2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\xalan-2.4.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\xerces-2.4.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\xml-apis.jar;F:\mssql\jdbc\lib\msbase.jar;F:\mssql\jdbc\lib\mssqlserver.jar;F:\mssql\jdbc\lib\msutil.jar;C:\J2SDK\demo\jfc\CodePointIM\CodePointIM.jar;C:\J2SDK\demo\jfc\Java2D\Java2Demo.jar;C:\J2SDK\demo\plugin\jfc\CodePointIM\CodePointIM.jar;C:\J2SDK\demo\plugin\jfc\Java2D\Java2Demo.jar;C:\J2SDK\jre\javaws\javaws.jar;C:\J2SDK\jre\lib\charsets.jar;C:\J2SDK\jre\lib\deploy.jar;C:\J2SDK\jre\lib\ext\dnsns.jar;C:\J2SDK\jre\lib\ext\ldapsec.jar;C:\J2SDK\jre\lib\ext\localedata.jar;C:\J2SDK\jre\lib\ext\sunjce_provider.jar;C:\J2SDK\jre\lib\ext\sunpkcs11.jar;C:\J2SDK\jre\lib\im\indicim.jar;C:\J2SDK\jre\lib\im\thaiim.jar;C:\J2SDK\jre\lib\javaws.jar;C:\J2SDK\jre\lib\jce.jar;C:\J2SDK\jre\lib\jsse.jar;C:\J2SDK\jre\lib\plugin.jar;C:\J2SDK\jre\lib\rt.jar;C:\J2SDK\lib\dt.jar;C:\J2SDK\lib\htmlconverter.jar;C:\J2SDK\lib\jconsole.jar;C:\J2SDK\lib\tools.jar" Test.java
C:\J2SDK\bin\java -classpath ".;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\hibernate2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\ant-1.5.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\ant-optional-1.5.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\c3p0-0.8.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\cglib-2.0-rc2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-collections-2.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-dbcp-1.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-lang-1.0.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-logging-1.0.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-pool-1.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\concurrent-1.3.2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\connector.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\dom4j-1.4.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\ehcache-0.6.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jaas.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-cache.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-common.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-jmx.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-system.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jcs-1.0-dev.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jdbc2_0-stdext.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jgroups-2.2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jta.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\junit-3.8.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\log4j-1.2.8.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\odmg-3.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\oscache-2.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\proxool-0.8.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\swarmcache-1.0rc2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\xalan-2.4.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\xerces-2.4.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\xml-apis.jar;F:\mssql\jdbc\lib\msbase.jar;F:\mssql\jdbc\lib\mssqlserver.jar;F:\mssql\jdbc\lib\msutil.jar;C:\J2SDK\demo\jfc\CodePointIM\CodePointIM.jar;C:\J2SDK\demo\jfc\Java2D\Java2Demo.jar;C:\J2SDK\demo\plugin\jfc\CodePointIM\CodePointIM.jar;C:\J2SDK\demo\plugin\jfc\Java2D\Java2Demo.jar;C:\J2SDK\jre\javaws\javaws.jar;C:\J2SDK\jre\lib\charsets.jar;C:\J2SDK\jre\lib\deploy.jar;C:\J2SDK\jre\lib\ext\dnsns.jar;C:\J2SDK\jre\lib\ext\ldapsec.jar;C:\J2SDK\jre\lib\ext\localedata.jar;C:\J2SDK\jre\lib\ext\sunjce_provider.jar;C:\J2SDK\jre\lib\ext\sunpkcs11.jar;C:\J2SDK\jre\lib\im\indicim.jar;C:\J2SDK\jre\lib\im\thaiim.jar;C:\J2SDK\jre\lib\javaws.jar;C:\J2SDK\jre\lib\jce.jar;C:\J2SDK\jre\lib\jsse.jar;C:\J2SDK\jre\lib\plugin.jar;C:\J2SDK\jre\lib\rt.jar;C:\J2SDK\lib\dt.jar;C:\J2SDK\lib\htmlconverter.jar;C:\J2SDK\lib\jconsole.jar;C:\J2SDK\lib\tools.jar" Test
*/
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class Test
{
public static void main(String[] args) throws Exception
{
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();
// Query query = session.createQuery("select username from customers as c");
// java.util.Iterator i = query.iterate();
// while (i.hasNext())
// {
// Customer c = (Customer) i.next();
// System.out.println(c.getUsername());
// }
}
}
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;
}
}
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 name="java:/hibernate/HibernateFactory">
<property name="show_sql">
true
</property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://server\\psqlke;DataBaseName=hibernate_test
</property>
<property name="connection.username">
sa
</property>
<property name="connection.password">
123abc
</property>
<property name="dialect">
net.sf.hibernate.dialect.SQLServerDialect
</property>
<mapping resource="Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
Customer.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="Customer" table="customers">
<id name="id" column="CID">
<generator class="increment" />
</id>
<property name="username" column="USERNAME" />
<property name="password" column="PASSWORD" />
</class>
</hibernate-mapping>
SQL:
CREATE TABLE CUSTOMER
(
CID INTEGER NOT NULL PRIMARY KEY,
USERNAME VARCHAR(12) NOT NULL,
PASSWORD VARCHAR(12)
);
本文地址:http://com.8s8s.com/it/it12561.htm