一、结构

二、代码

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、、)的更多相关文章

  1. 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 ...

  2. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-002Table per concrete class with implicit polymorphism(@MappedSuperclass、@AttributeOverride)

    一.结构 二.代码 1. package org.jpwh.model.inheritance.mappedsuperclass; import javax.persistence.MappedSup ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-001Hibernate映射继承的方法

    There are four different strategies for representing an inheritance hierarchy: Use one table per co ...

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

    一.代码 1. package org.jpwh.model.inheritance.associations.onetomany; import org.jpwh.model.Constants; ...

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

    一.结构 二.代码 1. package org.jpwh.model.inheritance.associations.manytoone; import org.jpwh.model.Consta ...

  9. 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 ...

随机推荐

  1. 19 Python 正则模块和正则表达式

    什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的代码(.p ...

  2. CodeForces-831A-Unimodal Array (水题)

    题目链接 /* Name: Copyright: Author: Date: 2018/5/6 19:34:23 Description: */ #include <iostream> # ...

  3. SVN管理多个项目版本库 (windows,linux 通用)

    SVN管理多个项目版本库: . 安装SVN服务器软件,路径: C:\Program Files\Subversion . 在D盘创建svn根目录D:\SVN-CM . 在D:\SVN-CM下创建SVN ...

  4. ACM学习历程—HDU5668 Circle(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...

  5. MAC的VMWare CentOS 6.8命令笔记

    1.图形界面切换到命令行界面 真正的Linux切换界面使用命令, Ctrl + Alt + Fn (1,2,3,4,5,6)           Wondows平台 Ctrl + option + F ...

  6. 学习动态性能表(19)--v$undostat

    学习动态性能表 第19篇--V$UNDOSTAT  2007.6.14 本视图监控当前实例中undo空间以及事务如何运行.并统计undo空间开销,事务开销以及实例可用的查询长度. V$UNDOSTAT ...

  7. nginx之 [error] 6702#0:XXX is forbidden (13: Permission denied)

    问题描述: 配置完 nginx 两个虚拟机后,客户端能够访问原始的server ,新增加的 server 虚拟机 不能够访问,报错如下页面 解决过程: 1. 查看报错日志[root@mysql03 n ...

  8. DIY ESXI虚拟化服务器再度升级ESXI6.0 (U盘安装Esxi)

    前期我写了一个篇关于<IT屌丝DIY ESXI虚拟化服务器记实 >链接地址:http://lidongni.blog.51cto.com/2554605/1643996,这次主要是在原有的 ...

  9. CentOS7安装wget 及配置

    yum -y install wget yum -y install setup yum -y install perl

  10. 蓝桥杯 基础训练 BASIC-27 2n皇后问题

    基础练习 2n皇后问题   时间限制:1.0s   内存限制:512.0MB 问题描述 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都 ...