建议是有一点点是一点点基础的人又没有hibernate基础和经验的人比较适合(所谓一点点基础是最起码不要我介绍一些配置文件的什么的.).注意我用的jdbc驱动com.inet.tds.tdsdriver
大家一起努力学习吧.
建立po对象
建po对象customer、order以及它们的hibernate配置文件
/*
* created on 2005-10-12
*
* todo to change the template for this generated file go to
* window - preferences - java - code style - code templates
*/
package testdemo;
import java.util.hashset;
import java.util.set;
/**
* @author liuzj
*
* todo to change the template for this generated type comment go to window -
* preferences - java - code style - code templates
*/
public class customer {
private int id;
private string username;
private string password;
private set orders = new hashset();
public customer() {
}
public customer(string username, string password, set orders) {
this.username = username;
this.password = password;
this.orders = orders;
}
public int getid() {
return id;
}
public string getpassword() {
return password;
}
public string getusername() {
return username;
}
public set getorders() {
return orders;
}
public void setid(int id) {
this.id = id;
}
public void setpassword(string password) {
this.password = password;
}
public void setusername(string username) {
this.username = username;
}
public void setorders(set orders) {
this.orders = orders;
}
}
<?xml version="1.0"?>
<!doctype hibernate-mapping public
"-//hibernate/hibernate mapping dtd 3.0//en"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="testdemo.customer" table="customer" dynamic-insert="true" dynamic-update="true">
<id name="id" column="id">
<generator class="increment" />
</id>
<property name="username" column="username" />
<property name="password" column="password" />
<set
name="orders"
inverse="true"
cascade="save-update"
>
<key column="customer_id" />
<one-to-many class="testdemo.order" />
</set>
</class>
</hibernate-mapping>
/*
* created on 2005-10-13
*
* todo to change the template for this generated file go to
* window - preferences - java - code style - code templates
*/
package testdemo;
/**
* @author liuzj
*
* todo to change the template for this generated type comment go to
* window - preferences - java - code style - code templates
*/
import java.io.serializable;
public class order implements serializable {
private long id;
private string ordernumber;
private double price;
private customer customer;
public order() {
}
public order(string ordernumber,double price,customer customer) {
this.ordernumber=ordernumber;
this.price=price;
this.customer=customer;
}
public long getid() {
return this.id;
}
public void setid(long id) {
this.id = id;
}
public string getordernumber() {
return this.ordernumber;
}
public void setordernumber(string ordernumber) {
this.ordernumber = ordernumber;
}
public customer getcustomer() {
return this.customer;
}
public void setcustomer(customer customer) {
this.customer = customer;
}
public double getprice(){
return this.price;
}
private void setprice( double price ){
this.price = price;
}
}
<?xml version="1.0"?>
<!doctype hibernate-mapping public
"-//hibernate/hibernate mapping dtd 3.0//en"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="testdemo">
<class name="order" table="orders">
<id name="id">
<generator class="increment"/>
</id>
<property name="ordernumber" column="order_number"/>
<property name="price" />
<many-to-one
name="customer"
column="customer_id"
class="customer"
not-null="true"
/>
</class>
</hibernate-mapping>
上面的po已经建立完成,下面是一个测试类
/*
* created on 2005-10-12
*
* todo to change the template for this generated file go to
* window - preferences - java - code style - code templates
*/
package testdemo;
import java.util.hashset;
import org.hibernate.session;
import org.hibernate.sessionfactory;
import org.hibernate.transaction;
import org.hibernate.cfg.configuration;
import testdemo.customer;
/**
* @author liuzj
*
* todo to change the template for this generated type comment go to window -
* preferences - java - code style - code templates
*/
public class test {
sessionfactory sessionfactory = new configuration().configure().addclass(
testdemo.customer.class).addclass(order.class).buildsessionfactory();
session session = sessionfactory.opensession();
public void savecustomer(customer customer) throws exception {
transaction tx = null;
try {
tx = session.begintransaction();
session.save(customer);
tx.commit();
} catch (exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
public void testmethod()throws exception
{
customer customer=new customer("lzhengj","001",new hashset());
order order1=new order("order",1000,customer);
order order2=new order("order",2000,customer);
customer.getorders().add(order1);
customer.getorders().add(order2);
this.savecustomer(customer);
}
public static void main(string[] args) {
try{
new test().testmethod();
}catch(exception e)
{
system.out.println("this is the testmethod throw exception.....");
e.printstacktrace();
}
}
}
ok,下面是一个hibernate的配置hibernate.cfg.xml(位于应用目录下面)
<!doctype hibernate-configuration public
"-//hibernate/hibernate configuration dtd 3.0//en"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">
jdbc:inetdae:localhost:1433?charset=gbk&database=hibernate_test
</property>
<property name="hibernate.connection.driver_class">
com.inet.tds.tdsdriver
</property>
<property name="hibernate.connection.username">
sa
</property>
<property name="hibernate.connection.password">
aa
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.sqlserverdialect
</property>
<property name="show_sql">
true
&
闽公网安备 35060202000074号