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 ...
随机推荐
- Java 接口与继承 道至简第六章发表阅读笔记
一.继承条件下的构造方法调用 class Grandparent { public Grandparent() { System.out.println("GrandParent Creat ...
- Spring_总结_04_高级配置(六)_Bean的初始化和销毁
一.前言 本文承接上一节:Spring_总结_04_高级配置(五)_运行时注入值
- 完成一个servlet 就要在web.xml里面配一个映射,这样就有一个路径供我们 使用????? servlet从页面接收值?
最后,最容易忘记的是:在dao层中 调用xml里的删除sql语句 后面需要人为加上事务提交.一定要! sqlSession.commit();//jdbc是自动提交,但是mybatis中不是自动提交的 ...
- Debian For ARM mysql-server install information
/**************************************************************************** * Debian For ARM mysql ...
- WC2018 即时战略
交互题 一棵树,一开始只有 1 号点是已知的,其他的都是未知的,你可以调用函数 explore(x,y) ,其中 x 必须是已知的,函数会找到 x 到 y 路径上第二个点,并把它标成已知,求最小步数使 ...
- tableau学习笔记—1
第一部分 第一章 数据可视化 1.1 用数据讲故事 1.2 数据不只是数字 1.3 在数据中寻找什么(关系.模式.异常) 第二章 Tableau概述 2.1 Tableau概述 2.2 产品简介 第三 ...
- CODE FESTIVAL 2017 qual A--B-fLIP(换种想法,暴力枚举)
个人心得:开始拿着题目还是有点懵逼的,以前做过相同的,不过那是按一个位置行列全都反之,当时也是没有深究.现在在打比赛不得不 重新构思,后面一想把所有的状态都找出来,因为每次确定了已经按下的行和列后,按 ...
- LeetCode Longest Uncommon Subsequence II
原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-ii/#/description 题目: Given a list ...
- C# 多线程参数传递
之前使用多线程的时候,基本没有遇到过参数传递的情况,最近,接连遇到需要进行参数传递的多线程的使用.每次都要重新上网查一下,太麻烦了.为了方便以后的使用,就把经常参阅的网上资料记录下来. 原文地址如下: ...
- Angular5学习笔记 - 创建、运行、发布项目(一)
一.安装脚手架 npm install -g cnpm --registry=https://registry.npm.taobao.org #安装阿里镜像 npm install -g @angul ...