JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-009Polymorphic collections(@OneToMany(mappedBy = "user")、@ManyToOne、)

一、代码

1.

 1 package org.jpwh.model.inheritance.associations.onetomany;
 2 
 3 import org.jpwh.model.Constants;
 4 
 5 import javax.persistence.*;
 6 import javax.validation.constraints.NotNull;
 7 
 8 // Can not be @MappedSuperclass when it's a target class in associations!
 9 @Entity
10 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
11 public abstract class BillingDetails {
12 
13     @Id
14     @GeneratedValue(generator = Constants.ID_GENERATOR)
15     protected Long id;
16 
17     @ManyToOne(fetch = FetchType.LAZY)
18     protected User user;
19 
20     @NotNull
21     protected String owner;
22 
23     protected BillingDetails() {
24     }
25 
26     protected BillingDetails(String owner) {
27         this.owner = owner;
28     }
29 
30     public Long getId() {
31         return id;
32     }
33 
34     public String getOwner() {
35         return owner;
36     }
37 
38     public void setOwner(String owner) {
39         this.owner = owner;
40     }
41 
42     public User getUser() {
43         return user;
44     }
45 
46     public void setUser(User user) {
47         this.user = user;
48     }
49 
50     public void pay(int amount) {
51         // NOOP
52     }
53 
54     // ...
55 }

 

2.

 1 package org.jpwh.model.inheritance.associations.onetomany;
 2 
 3 import javax.persistence.Entity;
 4 import javax.validation.constraints.NotNull;
 5 
 6 @Entity
 7 public class BankAccount extends BillingDetails {
 8 
 9     @NotNull
10     protected String account;
11 
12     @NotNull
13     protected String bankname;
14 
15     @NotNull
16     protected String swift;
17 
18     public BankAccount() {
19         super();
20     }
21 
22     public BankAccount(String owner, String account, String bankname, String swift) {
23         super(owner);
24         this.account = account;
25         this.bankname = bankname;
26         this.swift = swift;
27     }
28 
29     public String getAccount() {
30         return account;
31     }
32 
33     public void setAccount(String account) {
34         this.account = account;
35     }
36 
37     public String getBankname() {
38         return bankname;
39     }
40 
41     public void setBankname(String bankname) {
42         this.bankname = bankname;
43     }
44 
45     public String getSwift() {
46         return swift;
47     }
48 
49     public void setSwift(String swift) {
50         this.swift = swift;
51     }
52 }

 

3.

 1 package org.jpwh.model.inheritance.associations.onetomany;
 2 
 3 import javax.persistence.Entity;
 4 import javax.validation.constraints.NotNull;
 5 
 6 @Entity
 7 public class CreditCard extends BillingDetails {
 8 
 9     @NotNull
10     protected String cardNumber;
11 
12     @NotNull
13     protected String expMonth;
14 
15     @NotNull
16     protected String expYear;
17 
18     public CreditCard() {
19         super();
20     }
21 
22     public CreditCard(String owner, String cardNumber, String expMonth, String expYear) {
23         super(owner);
24         this.cardNumber = cardNumber;
25         this.expMonth = expMonth;
26         this.expYear = expYear;
27     }
28 
29     public String getCardNumber() {
30         return cardNumber;
31     }
32 
33     public void setCardNumber(String cardNumber) {
34         this.cardNumber = cardNumber;
35     }
36 
37     public String getExpMonth() {
38         return expMonth;
39     }
40 
41     public void setExpMonth(String expMonth) {
42         this.expMonth = expMonth;
43     }
44 
45     public String getExpYear() {
46         return expYear;
47     }
48 
49     public void setExpYear(String expYear) {
50         this.expYear = expYear;
51     }
52 
53 }

 

4.

 1 package org.jpwh.model.inheritance.associations.onetomany;
 2 
 3 import org.jpwh.model.Constants;
 4 
 5 import javax.persistence.*;
 6 import javax.validation.constraints.NotNull;
 7 import java.util.HashSet;
 8 import java.util.Set;
 9 
10 @Entity
11 @Table(name = "USERS")
12 public class User {
13 
14     @Id
15     @GeneratedValue(generator = Constants.ID_GENERATOR)
16     protected Long id;
17 
18     @NotNull
19     protected String username;
20 
21     @OneToMany(mappedBy = "user")
22     protected Set<BillingDetails> billingDetails = new HashSet<>();
23 
24     public User() {
25     }
26 
27     public User(String username) {
28         this.username = username;
29     }
30 
31     public Long getId() {
32         return id;
33     }
34 
35     public String getUsername() {
36         return username;
37     }
38 
39     public void setUsername(String username) {
40         this.username = username;
41     }
42 
43     public Set<BillingDetails> getBillingDetails() {
44         return billingDetails;
45     }
46 
47     public void setBillingDetails(Set<BillingDetails> billingDetails) {
48         this.billingDetails = billingDetails;
49     }
50 
51     // ...
52 }

 

5.测试

 1 package org.jpwh.test.inheritance;
 2 
 3 
 4 import org.jpwh.env.JPATest;
 5 import org.jpwh.model.inheritance.associations.onetomany.BankAccount;
 6 import org.jpwh.model.inheritance.associations.onetomany.BillingDetails;
 7 import org.jpwh.model.inheritance.associations.onetomany.CreditCard;
 8 import org.jpwh.model.inheritance.associations.onetomany.User;
 9 import org.testng.annotations.BeforeClass;
10 import org.testng.annotations.Test;
11 
12 import javax.persistence.EntityManager;
13 import javax.transaction.UserTransaction;
14 
15 import static org.testng.Assert.assertEquals;
16 
17 public class PolymorphicOneToMany extends JPATest {
18 
19     @Override
20     public void configurePersistenceUnit() throws Exception {
21         configurePersistenceUnit("PolymorphicOneToManyPU");
22     }
23 
24     @Test
25     public void storeAndLoadItemBids() throws Exception {
26         UserTransaction tx = TM.getUserTransaction();
27         try {
28             tx.begin();
29             EntityManager em = JPA.createEntityManager();
30 
31             BankAccount ba = new BankAccount(
32                 "Jane Roe", "445566", "One Percent Bank Inc.", "999"
33             );
34             CreditCard cc = new CreditCard(
35                 "John Doe", "1234123412341234", "06", "2015"
36             );
37             User johndoe = new User("johndoe");
38 
39             johndoe.getBillingDetails().add(ba);
40             ba.setUser(johndoe);
41 
42             johndoe.getBillingDetails().add(cc);
43             cc.setUser(johndoe);
44 
45             em.persist(ba);
46             em.persist(cc);
47             em.persist(johndoe);
48 
49             tx.commit();
50             em.close();
51 
52             Long USER_ID = johndoe.getId();
53 
54             tx.begin();
55             em = JPA.createEntityManager();
56             {
57                 User user = em.find(User.class, USER_ID);
58 
59                 for (BillingDetails billingDetails : user.getBillingDetails()) {
60                     billingDetails.pay(123);
61                 }
62                 assertEquals(user.getBillingDetails().size(), 2);
63             }
64 
65             tx.commit();
66             em.close();
67 
68         } finally {
69             TM.rollback();
70         }
71     }
72 
73 }

 

posted @ 2016-04-08 15:21  shamgod  阅读(243)  评论(0编辑  收藏  举报
haha