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 ...
随机推荐
- 20180831_jar包冲突2_天安微信httpclient冲突
一.异常现象 微信项目需要向腾讯服务器发送请求获取token. 但是在请求的时候抛了个异常: <2018-8-30 下午05时39分18秒 CST> <Notice> < ...
- vmware centos 连网方式
1.自动获取IP地址 虚拟机使用桥接模式,相当于连接到物理机的网络里,物理机网络有DHCP服务器自动分配IP地址. #dhclient 自动获取ip地址命令 #ifconfig 查询系统里网卡信息,i ...
- 剑指offer--11.数组中出现次数超过一半的数字
unique(), count()函数好用 ---------------------------------------------------------------------- 时间限制:1秒 ...
- 未定义的标示符“RECT”,引入了windows.h头文件也没有用?
我用的是win8的vs2012,RECT应该引入什么头文件?windows.h我第一个就引入了,去windows.h里面搜也搜不到RECT这个关键字,应该引入哪个头文件呢? 真是奇怪啊,是不是还需要什 ...
- java-05String课堂练习
1.阅读以下代码查看输出结果 public class StringPool { public static void main(String args[]) { String s0="He ...
- LeetCode Count Binary Substrings
原题链接在这里:https://leetcode.com/problems/count-binary-substrings/description/ 题目: Give a string s, coun ...
- BZOJ4605:崂山白花蛇草水
浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline ...
- CF 986A Fair——多源bfs
题目:http://codeforces.com/contest/986/problem/A 如果从每个村庄开始bfs找货物,会超时. 发现k较小.那就从货物开始bfs,给村庄赋上dis[ 该货物 ] ...
- Admin.Admin/Login --- 后台项目中的管理员及登录模块
管理员模块: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...
- HDU1576(扩展欧几里得)
A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...