举例订单与客户
多对一关系模型
· 首先是导包这个省略不写了
· 其次写domain类
分为Client Order两个类因为是多对一的关系所以外键应该放在Order这个类里
配置文件hibernatecfgxml
[java]
<hibernateconfiguration>
<sessionfactory name=foo>
<property name=nnectiondriver_class>commysqljdbcDriver</property>
<property name=connectionurl>jdbc:mysql:///test</property>
<property name=connectionusername>root</property>
<property name=connectionpassword>root</property>
<property name=dialect>orghibernatedialectMySQLDialect</property>
<property name=show_sql>true</property>
<property name=hbmddlauto>update</property>
<mapping resource=com/hbsi/domain/Clienthbmxml/>
<mapping resource=com/hbsi/domain/Orderhbmxml/>
</sessionfactory>
</hibernateconfiguration>
定义一个Client的变量
·然后写配置文件Clienthbmxml
<hibernatemapping package=comhbsidomain>
<class name=Client table=client>//类名和表名
<id name=id column=id>//变量和字段
<generator class= native />//根据数据库里的设置自定义
</id>
<property name=name />
</class>
</hibernate mapping>
Orderhbmxml
<hibernatemapping>
<class name=Order table=ordershop>
<id name=id>
<generator class=native />
</id>
<property name=shopname column=shopname />
<property name=number column=number/>
<property name = price column=price />
<manytoone name=client column=client_id />
<class>
</hibernatemapping>
配置文件这样就写完了接下来就是添加查找
Session session=null;
@Test
public void add(Client clientOrder order){
try{
session = HibernateUtilgetSession()//获取工具类
sessionbeginTransaction()//开启事务
/*Client client = new Client()
clientsetName(刘总)
Order order = new Order()
ordersetShopname(航空母舰)
ordersetNumber()
ordersetPrice()
ordersetCt(client)
Order order = new Order()
ordersetShopname(航空母舰)
ordersetNumber()
ordersetPrice()
ordersetCt(client)*/
sessionsave(client)
sessionsave(order)
sessionbeginTransaction()commit()
}finally{
if(session!=null){
sessionclose()
}
}
}
public void find(int id){
try{
session = HibernateUtilgetSession()
sessionbeginTransaction()
Order order = (Order) sessionget(Orderclass id)
Systemoutprintln(ordergetCt()getName())
sessionbeginTransaction()commit()
}finally{
if(session!=null){
sessionclose()
}
}
}
测试
public static void main(String[] args ){
Client client = new Client()
clientsetName(xiao总)
Order order = new Order()
ordersetShopname(美国)
ordersetNumber()
ordersetPrice()
ordersetCt(client)
Order order = new Order()
ordersetShopname(日本)
ordersetNumber()
ordersetPrice()
ordersetCt(client)
Manyone mo = new Manyone()
moadd(client order)
mofind()
}
关系模型一对多
Clienthbmxml
<hibernatemapping package=comhbsidomain>
<class name=Client table=client>
<id name=id column=id>
<generator class = native />
</id>
<property name=name />
<set name=order table=ordershop cascade=saveupdate>
<key column=user_id />
<onetomany class=Order />
</set>
</class>
</hibernatemapping>
Orderhbmxml
<hibernatemapping package=comhbsidomain>
<class name=Order table=ordershop>
<id name=id>
<generator class = native />
</id>
<property name=shopname column=shopname />
<property name=number column=number />
<property name=price column=price />
</class>
</hibernatemapping>
添加方法
public void add(Client clientOrder order){
try{
session = HibernateUtilgetSession()
sessionbeginTransaction()
sessionsave(client)
// sessionsave(order)
sessionbeginTransaction()commit()
}finally{
if(session!=null){
sessionclose()
}
}
}
public void find(int id){
try{
session = HibernateUtilgetSession()
sessionbeginTransaction()
Client client = (Client) sessionget(Orderclass id)
Set<Order> orders =clientgetOrder()
orderssize()
sessionbeginTransaction()commit()
}finally{
if(session!=null){
sessionclose()
}
}
}