一、结构

二、代码

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. 20180831_jar包冲突2_天安微信httpclient冲突

    一.异常现象 微信项目需要向腾讯服务器发送请求获取token. 但是在请求的时候抛了个异常: <2018-8-30 下午05时39分18秒 CST> <Notice> < ...

  2. weinre

    https://www.cnblogs.com/diva/p/3995674.html

  3. sql server 纵横表的转换

    在平常的工作中或者面试中,我们可能有遇到过数据库的纵横表的转换问题.今天我们就来讨论下. 1.创建表 首先我们来创建一张表. sql语句: --1. 创建数据表 if OBJECT_ID('Score ...

  4. Mybatis中对于标签的配置可能不会出现自动提示解决方案

    解决办法:引入mybatis-3-config.dtd 文件 Window-preferences-搜索xml-xml catalog <!DOCTYPE configuration PUBLI ...

  5. Yii 查询 搜索

    一.视图 <div class="form-horizontal"> <?php $form = $this->beginWidget('CActiveFo ...

  6. SPI驱动框架-1(DM8127 Linux2.6.37为例)

    一.驱动程序结构 1.platform_device 文件:/arch/arm/mach-omap2/device.c static struct omap2_mcspi_platform_confi ...

  7. php用zendstudio建立wsdl

    首先,新建的时候要选择soap,然后deocument和rpc都可以. 类和方法的页面: <?php //发货接口 class test{ function send_do_delivery($ ...

  8. java代码继承疑惑,请有心人解答

    总结:这段程序没有问题,编译运行都是可以的.关键是,子类的无参构造方法第一句少了super(a,b);运行后,显示了双重结果 .还有.如果子类中没有声明成员变量String  name.那么结果显示父 ...

  9. Jconsole 测试.

    Jconsole 测试. 1 下载并安装 d:\Program Files\Java\jdk1.8.0_111\ JDK自带,Windows下图形界面,监控分析Java程序 2 查看jmx进程号 [r ...

  10. maven 学习 十 关于打包

    clean package -Dmaven.test.skip=true -P product 这个命令干的活: 清class文件,打包构建,跳过测试,注意最后一个 -P product, 会激活项目 ...