建議是有一點點是一點點基礎的人又沒有hibernate基礎和經驗的人比較適合(所謂一點點基礎是最起碼不要我介紹一些配置文件的什麼的
大家一起努力學習吧
建立PO對象
建PO對象Customer
/*
* Created on
*
* TODO To change the template for this generated file go to
* Window
*/
package testdemo;
import java
import java
/**
* @author liuzj
*
* TODO To change the template for this generated type comment go to Window
* Preferences
*/
public class Customer {
private int id;
private String username;
private String password;
private Set orders = new HashSet();
public Customer() {
}
public Customer(String username
this
this
this
}
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
}
public void setPassword(String password) {
this
}
public void setUsername(String username) {
this
}
public void setOrders(Set orders) {
this
}
}
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<property name=
<set
name=
inverse=
cascade=
>
<key column=
<one
</set>
</class>
</hibernate
/*
* Created on
*
* TODO To change the template for this generated file go to
* Window
*/
package testdemo;
/**
* @author liuzj
*
* TODO To change the template for this generated type comment go to
* Window
*/
import java
public class Order implements Serializable {
private Long id;
private String orderNumber;
private double price;
private Customer customer;
public Order() {
}
public Order(String orderNumber
this
this
this
}
public Long getId() {
return this
}
public void setId(Long id) {
this
}
public String getOrderNumber() {
return this
}
public void setOrderNumber(String orderNumber) {
this
}
public Customer getCustomer() {
return this
}
public void setCustomer(Customer customer) {
this
}
public double getPrice(){
return this
}
private void setPrice( double price ){
this
}
}
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<property name=
<many
name=
column=
class=
not
/>
</class>
</hibernate
上面的PO已經建立完成
/*
* Created on
*
* TODO To change the template for this generated file go to
* Window
*/
package testdemo;
import java
import org
import org
import org
import org
import testdemo
/**
* @author liuzj
*
* TODO To change the template for this generated type comment go to Window
* Preferences
*/
public class Test {
SessionFactory sessionFactory = new Configuration(nfigure()
testdemo
Session session = sessionFactory
public void saveCustomer(Customer customer) throws Exception {
Transaction tx = null;
try {
tx = session
session
mit();
} catch (Exception e) {
if (tx != null) {
tx
}
throw e;
} finally {
session
}
}
public void testmethod()throws Exception
{
Customer customer=new Customer(
Order order
Order order
customer
customer
this
}
public static void main(String[] args) {
try{
new Test()
}catch(Exception e)
{
System
e
}
}
}
ok
<!DOCTYPE hibernate
<hibernate
<session
<property name=
jdbc:inetdae:localhost:
</property>
<property name=
com
</property>
<property name=
sa
</property>
<property name=
aa
</property>
<property name=
org
</property>
<property name=
true
&
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28232.html