JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-009Polymorphic collections(@OneToMany(mappedBy = "user")、@ManyToOne、)
一、代码
1.
package org.jpwh.model.inheritance.associations.onetomany; 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.TABLE_PER_CLASS)
public abstract class BillingDetails { @Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id; @ManyToOne(fetch = FetchType.LAZY)
protected User user; @NotNull
protected String owner; protected BillingDetails() {
} protected BillingDetails(String owner) {
this.owner = owner;
} public Long getId() {
return id;
} public String getOwner() {
return owner;
} public void setOwner(String owner) {
this.owner = owner;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public void pay(int amount) {
// NOOP
} // ...
}
2.
package org.jpwh.model.inheritance.associations.onetomany; 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;
}
}
3.
package org.jpwh.model.inheritance.associations.onetomany; 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;
} }
4.
package org.jpwh.model.inheritance.associations.onetomany; import org.jpwh.model.Constants; import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set; @Entity
@Table(name = "USERS")
public class User { @Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id; @NotNull
protected String username; @OneToMany(mappedBy = "user")
protected Set<BillingDetails> billingDetails = new HashSet<>(); 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 Set<BillingDetails> getBillingDetails() {
return billingDetails;
} public void setBillingDetails(Set<BillingDetails> billingDetails) {
this.billingDetails = billingDetails;
} // ...
}
5.测试
package org.jpwh.test.inheritance; import org.jpwh.env.JPATest;
import org.jpwh.model.inheritance.associations.onetomany.BankAccount;
import org.jpwh.model.inheritance.associations.onetomany.BillingDetails;
import org.jpwh.model.inheritance.associations.onetomany.CreditCard;
import org.jpwh.model.inheritance.associations.onetomany.User;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import javax.persistence.EntityManager;
import javax.transaction.UserTransaction; import static org.testng.Assert.assertEquals; public class PolymorphicOneToMany extends JPATest { @Override
public void configurePersistenceUnit() throws Exception {
configurePersistenceUnit("PolymorphicOneToManyPU");
} @Test
public void storeAndLoadItemBids() throws Exception {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager(); BankAccount ba = new BankAccount(
"Jane Roe", "445566", "One Percent Bank Inc.", "999"
);
CreditCard cc = new CreditCard(
"John Doe", "1234123412341234", "06", "2015"
);
User johndoe = new User("johndoe"); johndoe.getBillingDetails().add(ba);
ba.setUser(johndoe); johndoe.getBillingDetails().add(cc);
cc.setUser(johndoe); em.persist(ba);
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); for (BillingDetails billingDetails : user.getBillingDetails()) {
billingDetails.pay(123);
}
assertEquals(user.getBillingDetails().size(), 2);
} tx.commit();
em.close(); } finally {
TM.rollback();
}
} }
JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-009Polymorphic collections(@OneToMany(mappedBy = "user")、@ManyToOne、)的更多相关文章
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-003Table per concrete class with unions(@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)、<union-subclass>)
一.代码 1. package org.jpwh.model.inheritance.tableperclass; import org.jpwh.model.Constants; import ja ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-002Table per concrete class with implicit polymorphism(@MappedSuperclass、@AttributeOverride)
一.结构 二.代码 1. package org.jpwh.model.inheritance.mappedsuperclass; import javax.persistence.MappedSup ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-006Mixing inheritance strategies(@SecondaryTable、@PrimaryKeyJoinColumn、<join fetch="select">)
一.结构 For example, you can map a class hierarchy to a single table, but, for a particular subclass, s ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-005Table per subclass with joins(@Inheritance(strategy = InheritanceType.JOINED)、@PrimaryKeyJoinColumn、)
一.结构 The fourth option is to represent inheritance relationships as SQL foreign key associations. Ev ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-004Table per class hierarchy(@Inheritance..SINGLE_TABLE)、@DiscriminatorColumn、@DiscriminatorValue、@DiscriminatorFormula)
一.结构 You can map an entire class hierarchy to a single table. This table includes columns for all pr ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-001Hibernate映射继承的方法
There are four different strategies for representing an inheritance hierarchy: Use one table per co ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-008Polymorphic many-to-one associations(@ManyToOne、@Inheritance、)
一.结构 二.代码 1. package org.jpwh.model.inheritance.associations.manytoone; import org.jpwh.model.Consta ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-007Inheritance of embeddable classes(@MappedSuperclass、@Embeddable、@AttributeOverrides、、)
一.结构 二.代码 1. package org.jpwh.model.inheritance.embeddable; import javax.persistence.MappedSuperclas ...
- JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-007UserTypes的用法(@org.hibernate.annotations.Type、@org.hibernate.annotations.TypeDefs、CompositeUserType、DynamicParameterizedType、、、)
一.结构 二.Hibernate支持的UserTypes接口 UserType —You can transform values by interacting with the plain JD ...
随机推荐
- JVM_总结_01_JDK的安装
一.前言 本文主要简要介绍下JDK的安装 二.下载 1.JDK下载地址 前往官方网站下载JDK jdk8官网下载 2.JDK下载 如下图 下载完之后得到安装软件,如下图 三.安装 双击运行安装软件,即 ...
- centos 6 简单安装mysql
yum list installed | grep mysql yum -y remove mysql-libs.i686 yum list installed | grep mysql wget d ...
- hadoop碰到的 一个问题
在里面添加/usr/local/hadoop/etc/hadoop/log4j.properties log4j.logger.org.apache.hadoop.util.NativeCodeLoa ...
- [独孤九剑]Oracle知识点梳理(三)导入、导出
本系列链接导航: [独孤九剑]Oracle知识点梳理(一)表空间.用户 [独孤九剑]Oracle知识点梳理(二)数据库的连接 [独孤九剑]Oracle知识点梳理(三)导入.导出 [独孤九剑]Oracl ...
- 数字排列(n,m)(搜索与回溯)
题目描述: 设有n个整数的集合{1,2,…,n},从中取出任意r个数进行排列(r<n),试列出所有的排列. 代码如下: #include<iostream>#include<c ...
- 理解SQL查询的底层原理
阅读目录 一.SQL Server组成部分 二.查询的底层原理 本系列[T-SQL]主要是针对T-SQL的总结. T-SQL基础 [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...
- Python的交互模式和命令行模式
Pyhton的交互模式 在终端输入Python3命令就会进入家Python的交互模式,在交互模式下,输入一行代码,回车,就会执行这行代码. Python的命令行模式 在终端输入Python3 1.py ...
- C#获取堆栈信息,输出文件名、行号、函数名、列号等
命名空间:System.Diagnostics 得到相关信息: StackTrace st = new StackTrace(new StackFrame(true));StackFrame sf = ...
- BZOJ2120:数颜色(分块版)
浅谈分块:https://www.cnblogs.com/AKMer/p/10369816.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...
- Spring Boot基本配置
本文参考javaEE开发的颠覆者SpringBoot实战第一版 基本配置 入口类和@SpringBootApplication Spring Boot通常有一个名为*Application的入口类,且 ...