久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

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

 KILLKISS 2013-07-11
在一對一關(guān)聯(lián)中,JPA沒有規(guī)定誰為關(guān)系的維護(hù)方,OneToOne的mapped屬性值指定了由另外一方實體的某個屬性來維護(hù)一對一關(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. 測試保存
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時,,上面的idcard.setPerson(person)可以省略,因為Person既設(shè)置了級聯(lián)屬性cascade={CascadeType.ALL},,在保存Person實體的時候也會保存IDcard,,又是關(guān)系的維護(hù)方,在它們之間建立關(guān)聯(lián),。如果為false,,在先保存idcard時候,由于not null約束,,程序報錯,。


2. 測試更新:
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)可以省略,因為idcard已經(jīng)是托管態(tài)了,,對它的修改在commit的時候都會提交到數(shù)據(jù)庫,。如果idcard是游離態(tài)則需要該語句。


3. 測試查詢:
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. }  

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


4. 測試刪除:
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時,能刪除成功,;為false時刪除會報錯:org.hibernate.PropertyValueException: not-null property references a null or transient value: com.john.bean.Person.idcard

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多