本篇內(nèi)容主要講解“Hibernate一對(duì)多數(shù)據(jù)關(guān)聯(lián)是什么”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Hibernate一對(duì)多數(shù)據(jù)關(guān)聯(lián)是什么”吧!
創(chuàng)新互聯(lián)建站-云計(jì)算及IDC服務(wù)提供商,涵蓋公有云、IDC機(jī)房租用、四川電信科技城機(jī)房、等保安全、私有云建設(shè)等企業(yè)級(jí)互聯(lián)網(wǎng)基礎(chǔ)服務(wù),來(lái)電聯(lián)系:028-86922220
Hibernate一對(duì)多數(shù)據(jù)關(guān)聯(lián)。指的是單向一對(duì)多數(shù)據(jù)關(guān)聯(lián)一個(gè)用戶有多個(gè)地址,在用戶類TUser中包含地址類TAddress集合。
Hibernate如果上手了,那么所謂的一對(duì)多,多對(duì)一,多對(duì)多,一對(duì)一這些關(guān)系,應(yīng)該很快能理解.下面主要介紹Hibernate一對(duì)多的問(wèn)題。
1.由TUser對(duì)象將自身的id賦給addr.user_id,這樣導(dǎo)致addr屬性值變動(dòng),在事物提交的時(shí)候,會(huì)進(jìn)行update。
1)當(dāng)save該用戶的時(shí)候,
insert into t_address (user_id, address, zipcode, tel) value (null, "HongKong", "233123", "1123")
2)當(dāng)tx.commit()時(shí):
update t_address user_id="1", address="HongKong", zipcode="233123",tel="1123" where id=2;
這樣,在save user時(shí),就會(huì)出現(xiàn)約束違例。
調(diào)整方法:
可以在定義數(shù)據(jù)表字段時(shí)候,不加NOT NULL約束。或者在開(kāi)始為user_id隨意賦一個(gè)非空值(因?yàn)檫€要update,不正確也沒(méi)關(guān)系),或者將user_id字段從TAddress.hbm.xml中刪除(本例就是這樣實(shí)現(xiàn))。但是這些都是權(quán)宜之計(jì),用兩條SQL語(yǔ)句完成一次數(shù)據(jù)庫(kù)操作,性能低下。而雙向一對(duì)多解決了這個(gè)問(wèn)題。
下面來(lái)實(shí)現(xiàn)雙向關(guān)聯(lián):修改配置文件 TUser.hbm.xml
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">id> key> set> class> hibernate-mapping>
設(shè)定inverse="true",表明將TUser類作為被動(dòng)類,將數(shù)據(jù)關(guān)聯(lián)的維護(hù)工作交給關(guān)聯(lián)對(duì)象TAddress來(lái)管理。
在one-to-many模型中,將many一方設(shè)為主控方有助于性能的改善。(讓總理記住每個(gè)人困難,但是每個(gè)人記住總理方便)
TAddress.hbm.xml
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">id> class> hibernate-mapping>
2.對(duì)TAddress.java做如下改造:去掉user_id字段,增加user字段,和getter,setter方法。
package cn.blogjava.start; import java.io.Serializable; public class TAddress implements Serializable { private Integer id; private String address; private String zipcode; private String tel; private String type; private Integer idx; private TUser user; public TUser getUser() { return user; } public void setUser(TUser user) { this.user = user; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Integer getIdx() { return idx; } public void setIdx(Integer idx) { this.idx = idx; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getZipcode() { return zipcode; } public void setZipcode(String zipcode) { this.zipcode = zipcode; } }
3.測(cè)試代碼
既然TUser不維護(hù)關(guān)聯(lián)關(guān)系,需要TAddress需要自己來(lái)維護(hù)TUser,所以需要addr.setUser(user);
package cn.blogjava.start; import java.util.HashSet; import java.util.Iterator; import java.util.List; import junit.framework.Assert; import junit.framework.TestCase; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class HibernateTest extends TestCase { Session session = null; protected void setUp() { try { Configuration config = new Configuration().configure(); SessionFactory sessionFactory = config.buildSessionFactory(); session = sessionFactory.openSession(); } catch (HibernateException e) { e.printStackTrace(); } } protected void tearDown() { try { session.close(); } catch (HibernateException e) { e.printStackTrace(); } } /** *//** * 對(duì)象持久化測(cè)試(Insert方法) */ public void testInsert() { Transaction tran = null; try { TUser user = new TUser(); user.setName("byf"); user.setAge(new Integer(26)); TAddress addr = new TAddress(); addr.setTel("1123"); addr.setZipcode("233123"); addr.setAddress("HongKong"); addr.setUser(user); TAddress addr2 = new TAddress(); addr2.setTel("139"); addr2.setZipcode("116001"); addr2.setAddress("dalian"); addr2.setUser(user); TAddress addr3 = new TAddress(); addr3.setTel("136"); addr3.setZipcode("100080"); addr3.setAddress("beijing"); addr3.setUser(user); //設(shè)置關(guān)聯(lián) HashSet set = new HashSet(); set.add(addr); set.add(addr2); set.add(addr3); user.setAddress(set); tran = session.beginTransaction(); //插入user信息 session.save(user); session.flush(); tran.commit(); Assert.assertEquals(user.getId().intValue()>0 ,true); } catch (HibernateException e) { e.printStackTrace(); Assert.fail(e.getMessage()); if(tran != null) { try { tran.rollback(); } catch (Exception e1) { e1.printStackTrace(); } } } } /** *//** * 對(duì)象讀取測(cè)試(Select方法) */ public void testSelect(){ String hql = " from TUser where name='byf'"; try { List userList = session.createQuery(hql).list(); TUser user = (TUser)userList.get(0); System.out.println("user name is " + user.getName()); for (Iterator iter = user.getAddress().iterator(); iter.hasNext();) { TAddress addr = (TAddress) iter.next(); System.out.println("user address is " + addr.getAddress()); } Assert.assertEquals(user.getName(), "byf"); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } } }
到此,相信大家對(duì)“Hibernate一對(duì)多數(shù)據(jù)關(guān)聯(lián)是什么”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!