JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-007Inheritance of embeddable classes(@MappedSuperclass、@Embeddable、@AttributeOverrides、、)
一、结构

二、代码
1.
package org.jpwh.model.inheritance.embeddable; import javax.persistence.MappedSuperclass;
import javax.validation.constraints.NotNull; @MappedSuperclass
public abstract class Measurement { @NotNull
protected String name; @NotNull
protected String symbol; // ...
protected Measurement() {
} public Measurement(String name, String symbol) {
this.name = name;
this.symbol = symbol;
} public String getName() {
return name;
} public String getSymbol() {
return symbol;
} public void setName(String name) {
this.name = name;
} public void setSymbol(String symbol) {
this.symbol = symbol;
}
}
2.
package org.jpwh.model.inheritance.embeddable; import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal; @Embeddable
@AttributeOverrides({
@AttributeOverride(name = "name",
column = @Column(name = "DIMENSIONS_NAME")),
@AttributeOverride(name = "symbol",
column = @Column(name = "DIMENSIONS_SYMBOL"))
})
public class Dimensions extends Measurement { public static Dimensions centimeters(BigDecimal width, BigDecimal height, BigDecimal depth) {
return new Dimensions("centimeters", "cm", width, height, depth);
} public static Dimensions inches(BigDecimal width, BigDecimal height, BigDecimal depth) {
return new Dimensions("inches", "\"", width, height, depth);
} @NotNull
protected BigDecimal depth; @NotNull
protected BigDecimal height; @NotNull
protected BigDecimal width; // ...
public Dimensions() {
} public Dimensions(String name, String symbol, BigDecimal width, BigDecimal height, BigDecimal depth) {
super(name, symbol);
this.height = height;
this.width = width;
this.depth = depth;
} public BigDecimal getDepth() {
return depth;
} public BigDecimal getHeight() {
return height;
} public BigDecimal getWidth() {
return width;
} public void setDepth(BigDecimal depth) {
this.depth = depth;
} public void setHeight(BigDecimal height) {
this.height = height;
} public void setWidth(BigDecimal width) {
this.width = width;
} public String toString() {
return String.format("W:%s%s x H:%s%s x D:%s%s", this.height, this.symbol, this.width, this.symbol, this.depth, this.symbol);
}
}
3.
package org.jpwh.model.inheritance.embeddable; import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal; @Embeddable
@AttributeOverrides({
@AttributeOverride(name = "name",
column = @Column(name = "WEIGHT_NAME")),
@AttributeOverride(name = "symbol",
column = @Column(name = "WEIGHT_SYMBOL"))
})
public class Weight extends Measurement { public static Weight kilograms(BigDecimal weight) {
return new Weight("kilograms", "kg", weight);
} public static Weight pounds(BigDecimal weight) {
return new Weight("pounds", "lbs", weight);
} @NotNull
@Column(name = "WEIGHT")
protected BigDecimal value; // ...
public Weight() {
} public Weight(String name, String symbol, BigDecimal weight) {
super(name, symbol);
this.value = weight;
} public BigDecimal getValue() {
return value;
} public void setValue(BigDecimal value) {
this.value = value;
} public String toString() {
return String.format("%s%s", this.value, this.symbol);
}
}
4.
package org.jpwh.model.inheritance.embeddable; import org.jpwh.model.Constants; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size; @Entity
public class Item { @Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id; @NotNull
@Size(
min = 2,
max = 255,
message = "Name is required, maximum 255 characters."
) protected String name; protected Dimensions dimensions; protected Weight weight; // ... public Item() {
} public Item(String name, Dimensions dimensions, Weight weight) {
this.name = name;
this.dimensions = dimensions;
this.weight = weight;
} public Dimensions getDimensions() {
return dimensions;
} public Long getId() { // Optional but useful
return id;
} public String getName() {
return name;
} public Weight getWeight() {
return weight;
} public void setDimensions(Dimensions dimensions) {
this.dimensions = dimensions;
} public void setName(String name) {
this.name = name;
} public void setWeight(Weight weight) {
this.weight = weight;
} }
5.
JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-007Inheritance of embeddable classes(@MappedSuperclass、@Embeddable、@AttributeOverrides、、)的更多相关文章
- 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-009Polymorphic collections(@OneToMany(mappedBy = "user")、@ManyToOne、)
一.代码 1. package org.jpwh.model.inheritance.associations.onetomany; import org.jpwh.model.Constants; ...
- 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 value types-006类型转换器( @Converter(autoApply = true) 、type="converter:qualified.ConverterName" )
一.结构 二.代码 1. package org.jpwh.model.advanced; import java.io.Serializable; import java.math.BigDecim ...
随机推荐
- OpenCV - Android Studio 2.2 中利用CAMKE进行OpenCV的NDK开发
我在http://www.cnblogs.com/fx-blog/p/8206737.html一文中提到了如何在Android Studio中Java层导入OpenCV(包含opencv_contri ...
- hl7 v2.X 版本中RSP_K23消息的构造
RSP_K23消息有MSH, MSA, ERR, QAK, QPD, PID几个segment,其中ERR,PID为可选. 1. 当MSA有err时,ERR段填充出错的详细信息. 2. 当MSA为AA ...
- maven 历史版本下载
1.登录http://maven.apache.org/download.cgi 2.拉倒最下面,点击 archives 3.可以看到maven个版本,找自己需要的下载
- Spark Streaming之一:整体介绍
提到Spark Streaming,我们不得不说一下BDAS(Berkeley Data Analytics Stack),这个伯克利大学提出的关于数据分析的软件栈.从它的视角来看,目前的大数据处理可 ...
- Python操作txt文本文件
题目: 1.TXT文本文件中的内容为: url:http://119.23.241.154:8080/futureloan/mvc/api/member/login,mobilephone:13760 ...
- Sass和Less、Stylus的转译和语法(1)
四.Sass.LESS和Stylus转译成CSSSass.LESS和Stylus源文件(除了LESS源文件在客户端下运行之外)都不能直接被浏览器直接识别,这样一来,要正常的使用这些源文 件,就需要将其 ...
- npm、nvm、nrm
随着前端技术的不断更新和发展,nodejs也越来越流行,作为一个web developer,要安装的依赖包.工具库也越来越多,所以npm几乎是所有前端开发者所必须要用到的,我在工作中曾经遇到过这样的问 ...
- 编译适用于TP-Link WR703N的OpenWRT固件
编译适用于TP-Link WR703N TP-Link MR11U 以及使用AR9331芯片组的单WAN/LAN复用口的路由. 注:刷机有风险,刷机需谨慎.一般情况下是不会失败的,若无法通过捅Rese ...
- asp.netcore di 实现批量接口注入
废话少说,先上代码 public static Dictionary<Type, Type[]> GetImpleAndInterfaces(string assemblyName,str ...
- “百度杯”CTF比赛 2017 二月场(Misc Web)
爆破-1: 打开链接,是502 我直接在后面加个变量传参数:?a=1 出了一段代码 var_dump()函数中,用了$$a,可能用了超全局变量GLOBALS 给hello参数传个GLOBALS 得到f ...