JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-008Polymorphic many-to-one associations(@ManyToOne、@Inheritance、)

时间:2023-03-09 10:04:25
JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-008Polymorphic many-to-one associations(@ManyToOne、@Inheritance、)

一、结构

JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-008Polymorphic many-to-one associations(@ManyToOne、@Inheritance、)

二、代码

1.

 package org.jpwh.model.inheritance.associations.manytoone;

 import org.jpwh.model.Constants;

 import javax.persistence.*;
import javax.validation.constraints.NotNull; // Can not be @MappedSuperclass when it's a target class in associations!
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class BillingDetails { protected Long id; @NotNull
protected String owner; protected BillingDetails() {
} protected BillingDetails(String owner) {
this.owner = owner;
} // Use property instead of field access, so calling getId() doesn't initialize a proxy!
@Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
public Long getId() {
return id;
} private void setId(Long id) {
this.id = id;
} public String getOwner() {
return owner;
} public void setOwner(String owner) {
this.owner = owner;
} public void pay(int amount) {
// NOOP
}
}

2.

 package org.jpwh.model.inheritance.associations.manytoone;

 import javax.persistence.Entity;
import javax.validation.constraints.NotNull; @Entity
public class CreditCard extends BillingDetails { @NotNull
protected String cardNumber; @NotNull
protected String expMonth; @NotNull
protected String expYear; public CreditCard() {
super();
} public CreditCard(String owner, String cardNumber, String expMonth, String expYear) {
super(owner);
this.cardNumber = cardNumber;
this.expMonth = expMonth;
this.expYear = expYear;
} public String getCardNumber() {
return cardNumber;
} public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
} public String getExpMonth() {
return expMonth;
} public void setExpMonth(String expMonth) {
this.expMonth = expMonth;
} public String getExpYear() {
return expYear;
} public void setExpYear(String expYear) {
this.expYear = expYear;
} }

3.

 package org.jpwh.model.inheritance.associations.manytoone;

 import javax.persistence.Entity;
import javax.validation.constraints.NotNull; @Entity
public class BankAccount extends BillingDetails { @NotNull
protected String account; @NotNull
protected String bankname; @NotNull
protected String swift; public BankAccount() {
super();
} public BankAccount(String owner, String account, String bankname, String swift) {
super(owner);
this.account = account;
this.bankname = bankname;
this.swift = swift;
} public String getAccount() {
return account;
} public void setAccount(String account) {
this.account = account;
} public String getBankname() {
return bankname;
} public void setBankname(String bankname) {
this.bankname = bankname;
} public String getSwift() {
return swift;
} public void setSwift(String swift) {
this.swift = swift;
}
}

4.

 package org.jpwh.model.inheritance.associations.manytoone;

 import org.jpwh.model.Constants;

 import javax.persistence.*;
import javax.validation.constraints.NotNull; @Entity
@Table(name = "USERS")
public class User { @Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id; @NotNull
protected String username; @ManyToOne(fetch = FetchType.LAZY)
protected BillingDetails defaultBilling; public User() {
} public User(String username) {
this.username = username;
} public Long getId() {
return id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public BillingDetails getDefaultBilling() {
return defaultBilling;
} public void setDefaultBilling(BillingDetails defaultBilling) {
this.defaultBilling = defaultBilling;
} // ...
}

5.测试

 package org.jpwh.test.inheritance;

 import org.jpwh.env.JPATest;
import org.jpwh.model.inheritance.associations.manytoone.BillingDetails;
import org.jpwh.model.inheritance.associations.manytoone.CreditCard;
import org.jpwh.model.inheritance.associations.manytoone.User;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import javax.persistence.EntityManager;
import javax.transaction.UserTransaction; import static org.testng.Assert.*; public class PolymorphicManyToOne extends JPATest { @Override
public void configurePersistenceUnit() throws Exception {
configurePersistenceUnit("PolymorphicManyToOnePU");
} @Test
public void storeAndLoadItemBids() throws Exception {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager(); CreditCard cc = new CreditCard(
"John Doe", "1234123412341234", "06", "2015"
);
User johndoe = new User("johndoe");
johndoe.setDefaultBilling(cc); em.persist(cc);
em.persist(johndoe); tx.commit();
em.close(); Long USER_ID = johndoe.getId(); tx.begin();
em = JPA.createEntityManager();
{
User user = em.find(User.class, USER_ID); // Invoke the pay() method on a concrete subclass of BillingDetails
user.getDefaultBilling().pay(123);
assertEquals(user.getDefaultBilling().getOwner(), "John Doe");
} tx.commit();
em.close(); tx.begin();
em = JPA.createEntityManager();
{
User user = em.find(User.class, USER_ID); BillingDetails bd = user.getDefaultBilling(); assertFalse(bd instanceof CreditCard); // Don't do this, ClassCastException!
// CreditCard creditCard = (CreditCard) bd;
}
{
User user = em.find(User.class, USER_ID); //because the defaultBilling property is
// mapped with FetchType.LAZY , Hibernate will proxy the association target.
BillingDetails bd = user.getDefaultBilling(); CreditCard creditCard =
em.getReference(CreditCard.class, bd.getId()); // No SELECT! assertTrue(bd != creditCard); // Careful!
}
tx.commit();
em.close(); tx.begin();
em = JPA.createEntityManager();
{
User user = (User) em.createQuery(
"select u from User u " +
"left join fetch u.defaultBilling " +
"where u.id = :id")
.setParameter("id", USER_ID)
.getSingleResult(); // No proxy has been used, the BillingDetails instance has been fetched eagerly
CreditCard creditCard = (CreditCard) user.getDefaultBilling();
}
tx.commit();
em.close(); } finally {
TM.rollback();
}
} }