舉例訂單與客戶
多對一關系模型
· 首先是導包
· 其次寫domain類
分為Client Order兩個類
配置文件
[java]
<hibernate
<session
<property name=
<property name=
<property name=
<property name=
<property name=
<property name=
<property name=
<mapping resource=
<mapping resource=
</session
</hibernate
定義一個Client的變量
·然後寫配置文件
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
</class>
</hibernate
Order
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<property name=
<property name =
<many
<class>
</hibernate
配置文件這樣就寫完了
Session session=null;
@Test
public void add(Client client
try{
session = HibernateUtil
session
/*Client client = new Client()
client
Order order
order
order
order
order
Order order
order
order
order
order
session
session
session
}finally{
if(session!=null){
session
}
}
}
public void find(int id){
try{
session = HibernateUtil
session
Order order = (Order) session
System
session
}finally{
if(session!=null){
session
}
}
}
測試
public static void main(String[] args ){
Client client = new Client()
client
Order order
order
order
order
order
Order order
order
order
order
order
Many
mo
mo
}
關系模型一對多
Client
<hibernate
<class name=
<id name=
<generator class =
</id>
<property name=
<set name=
<key column=
<one
</set>
</class>
</hibernate
Order
<hibernate
<class name=
<id name=
<generator class =
</id>
<property name=
<property name=
<property name=
</class>
</hibernate
添加方法
public void add(Client client
try{
session = HibernateUtil
session
session
// session
session
}finally{
if(session!=null){
session
}
}
}
public void find(int id){
try{
session = HibernateUtil
session
Client client = (Client) session
Set<Order> orders =client
orders
session
}finally{
if(session!=null){
session
}
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28399.html