小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

JPA 一對(duì)一關(guān)聯(lián)

 KILLKISS 2013-07-11
在一對(duì)一關(guān)聯(lián)中,JPA沒有規(guī)定誰為關(guān)系的維護(hù)方,OneToOne的mapped屬性值指定了由另外一方實(shí)體的某個(gè)屬性來維護(hù)一對(duì)一關(guān)聯(lián)。以Person和IDCard為例。

Person類:
Java代碼  收藏代碼
  1. @Entity  
  2. @Table(name="pillow")  
  3. public class Person {  
  4.     private Integer id;  
  5.     private String name;  
  6.     private IDCard idcard;  
  7.     public Person() {  
  8.     }  
  9.     public Person(String name) {  
  10.         this.name = name;  
  11.     }  
  12.     @Id @GeneratedValue  
  13.     public Integer getId() {  
  14.         return id;  
  15.     }  
  16.     @Column(length=20, nullable=false)  
  17.     public String getName() {  
  18.         return name;  
  19.     }  
  20.     @OneToOne(cascade={CascadeType.ALL},optional=false// optional specified to false means the corresponding column couldn't be nullable  
  21.     @JoinColumn(name="idcard_id"// Indicate the column name of the foreign key  
  22.     public IDCard getIdcard() {  
  23.         return idcard;  
  24.     }  
  25.     // Setters are omitted  
  26. }  


Java代碼  收藏代碼
  1. @Entity  
  2. public class IDCard {  
  3.     private Integer id;  
  4.     private String cardno;  
  5.     private Person person;  
  6.     public IDCard() {  
  7.     }  
  8.     public IDCard(String cardno) {  
  9.         this.cardno = cardno;  
  10.     }  
  11.     @Id @GeneratedValue  
  12.     public Integer getId() {  
  13.         return id;  
  14.     }  
  15.     @Column(length=18, nullable=false)  
  16.     public String getCardno() {  
  17.         return cardno;  
  18.     }  
  19.     // mappedBy specifies the property of the corresponding class that maintains the relationship. Also indicates this entity is the maintained edge.  
  20.     @OneToOne(mappedBy="idcard", cascade={CascadeType.REMOVE, CascadeType.MERGE, CascadeType.REFRESH},  
  21.             optional=false, fetch=FetchType.EAGER) // Maintained edge  
  22.     public Person getPerson() {  
  23.         return person;  
  24.     }  
  25.     // Setters are omitted  
  26. }  


1. 測(cè)試保存
Java代碼  收藏代碼
  1. public void save() {  
  2.     EntityManagerFactory factory = Persistence.createEntityManagerFactory("refrigerator");  
  3.     EntityManager em = factory.createEntityManager();  
  4.     Person person = new Person("Johnson");  
  5.     IDCard idcard = new IDCard("330818198602260444");  
  6.     // idcard.setPerson(person);  
  7.     person.setIdcard(idcard);  
  8.     em.getTransaction().begin(); // It's mandatory to open transaction to insert, update or delete data.  
  9.     em.persist(person);  
  10.     em.getTransaction().commit();  
  11.     em.close();  
  12.     factory.close();  
  13. }  


當(dāng)IDCard的person屬性的optional為true時(shí),上面的idcard.setPerson(person)可以省略,因?yàn)镻erson既設(shè)置了級(jí)聯(lián)屬性cascade={CascadeType.ALL},在保存Person實(shí)體的時(shí)候也會(huì)保存IDcard,又是關(guān)系的維護(hù)方,在它們之間建立關(guān)聯(lián)。如果為false,在先保存idcard時(shí)候,由于not null約束,程序報(bào)錯(cuò)。


2. 測(cè)試更新:
Java代碼  收藏代碼
  1. EntityManagerFactory factory = Persistence.createEntityManagerFactory("refrigerator");  
  2. EntityManager em = factory.createEntityManager();  
  3. em.getTransaction().begin();  
  4. IDCard idcard = em.find(IDCard.class1);  
  5. idcard.setCardno("330624198802060168");  
  6.         em.merge(idcard);  
  7. em.getTransaction().commit();  
  8. em.close();  
  9. factory.close();  

em.merge(idcard)可以省略,因?yàn)閕dcard已經(jīng)是托管態(tài)了,對(duì)它的修改在commit的時(shí)候都會(huì)提交到數(shù)據(jù)庫(kù)。如果idcard是游離態(tài)則需要該語句。


3. 測(cè)試查詢:
Java代碼  收藏代碼
  1. public void queryFromMaintained() {  
  2.     EntityManagerFactory factory = Persistence.createEntityManagerFactory("refrigerator");  
  3.     EntityManager em = factory.createEntityManager();  
  4.     IDCard idcard = em.find(IDCard.class1);  
  5.     em.close();  
  6.     factory.close();  
  7.     System.out.println(idcard.getCardno());  
  8.     System.out.println(idcard.getPerson().getName());  
  9. }  

因?yàn)镮DCard實(shí)體的person屬性設(shè)置了fetch=FetchType.EAGER,jpa會(huì)使用一個(gè)SELECT連表查詢出Person和IDCard。
如果fetch設(shè)置為FetchType.LAZY,jpa會(huì)使用兩個(gè)SELECT語句(第一個(gè)查詢出IDCard,第二個(gè)連表查詢),不會(huì)產(chǎn)生EntityManager關(guān)閉導(dǎo)致的問題。


4. 測(cè)試刪除:
Java代碼  收藏代碼
  1. public void deleteFromMaintained() {  
  2.     EntityManagerFactory factory = Persistence.createEntityManagerFactory("refrigerator");  
  3.     EntityManager em = factory.createEntityManager();  
  4.     em.getTransaction().begin();  
  5.     IDCard idcard = em.find(IDCard.class1);  
  6.     em.remove(idcard);  
  7.     em.getTransaction().commit();  
  8.     em.close();  
  9.     factory.close();  
  10. }  

當(dāng)IDCard的person屬性的optional為true時(shí),能刪除成功;為false時(shí)刪除會(huì)報(bào)錯(cuò):org.hibernate.PropertyValueException: not-null property references a null or transient value: com.john.bean.Person.idcard

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多