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


    一、结构

    二、代码

    1.

     1 package org.jpwh.model.inheritance.embeddable;
     2 
     3 import javax.persistence.MappedSuperclass;
     4 import javax.validation.constraints.NotNull;
     5 
     6 @MappedSuperclass
     7 public abstract class Measurement {
     8 
     9     @NotNull
    10     protected String name;
    11 
    12     @NotNull
    13     protected String symbol;
    14 
    15     // ...
    16     protected Measurement() {
    17     }
    18 
    19     public Measurement(String name, String symbol) {
    20         this.name = name;
    21         this.symbol = symbol;
    22     }
    23 
    24     public String getName() {
    25         return name;
    26     }
    27 
    28     public String getSymbol() {
    29         return symbol;
    30     }
    31 
    32     public void setName(String name) {
    33         this.name = name;
    34     }
    35 
    36     public void setSymbol(String symbol) {
    37         this.symbol = symbol;
    38     }
    39 }

    2.

     1 package org.jpwh.model.inheritance.embeddable;
     2 
     3 import javax.persistence.AttributeOverride;
     4 import javax.persistence.AttributeOverrides;
     5 import javax.persistence.Column;
     6 import javax.persistence.Embeddable;
     7 import javax.validation.constraints.NotNull;
     8 import java.math.BigDecimal;
     9 
    10 @Embeddable
    11 @AttributeOverrides({
    12         @AttributeOverride(name = "name",
    13                 column = @Column(name = "DIMENSIONS_NAME")),
    14         @AttributeOverride(name = "symbol",
    15                 column = @Column(name = "DIMENSIONS_SYMBOL"))
    16 })
    17 public class Dimensions extends Measurement {
    18 
    19     public static Dimensions centimeters(BigDecimal width, BigDecimal height, BigDecimal depth) {
    20         return new Dimensions("centimeters", "cm", width, height, depth);
    21     }
    22 
    23     public static Dimensions inches(BigDecimal width, BigDecimal height, BigDecimal depth) {
    24         return new Dimensions("inches", """, width, height, depth);
    25     }
    26 
    27     @NotNull
    28     protected BigDecimal depth;
    29 
    30     @NotNull
    31     protected BigDecimal height;
    32 
    33     @NotNull
    34     protected BigDecimal width;
    35 
    36     // ...
    37     public Dimensions() {
    38     }
    39 
    40     public Dimensions(String name, String symbol, BigDecimal width, BigDecimal height, BigDecimal depth) {
    41         super(name, symbol);
    42         this.height = height;
    43         this.width = width;
    44         this.depth = depth;
    45     }
    46 
    47     public BigDecimal getDepth() {
    48         return depth;
    49     }
    50 
    51     public BigDecimal getHeight() {
    52         return height;
    53     }
    54 
    55     public BigDecimal getWidth() {
    56         return width;
    57     }
    58 
    59     public void setDepth(BigDecimal depth) {
    60         this.depth = depth;
    61     }
    62 
    63     public void setHeight(BigDecimal height) {
    64         this.height = height;
    65     }
    66 
    67     public void setWidth(BigDecimal width) {
    68         this.width = width;
    69     }
    70 
    71     public String toString() {
    72         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);
    73     }
    74 }

    3.

     1 package org.jpwh.model.inheritance.embeddable;
     2 
     3 import javax.persistence.AttributeOverride;
     4 import javax.persistence.AttributeOverrides;
     5 import javax.persistence.Column;
     6 import javax.persistence.Embeddable;
     7 import javax.validation.constraints.NotNull;
     8 import java.math.BigDecimal;
     9 
    10 @Embeddable
    11 @AttributeOverrides({
    12         @AttributeOverride(name = "name",
    13                 column = @Column(name = "WEIGHT_NAME")),
    14         @AttributeOverride(name = "symbol",
    15                 column = @Column(name = "WEIGHT_SYMBOL"))
    16 })
    17 public class Weight extends Measurement {
    18 
    19     public static Weight kilograms(BigDecimal weight) {
    20         return new Weight("kilograms", "kg", weight);
    21     }
    22 
    23     public static Weight pounds(BigDecimal weight) {
    24         return new Weight("pounds", "lbs", weight);
    25     }
    26 
    27     @NotNull
    28     @Column(name = "WEIGHT")
    29     protected BigDecimal value;
    30 
    31     // ...
    32     public Weight() {
    33     }
    34 
    35     public Weight(String name, String symbol, BigDecimal weight) {
    36         super(name, symbol);
    37         this.value = weight;
    38     }
    39 
    40     public BigDecimal getValue() {
    41         return value;
    42     }
    43 
    44     public void setValue(BigDecimal value) {
    45         this.value = value;
    46     }
    47 
    48     public String toString() {
    49         return String.format("%s%s", this.value, this.symbol);
    50     }
    51 }

    4.

     1 package org.jpwh.model.inheritance.embeddable;
     2 
     3 import org.jpwh.model.Constants;
     4 
     5 import javax.persistence.Entity;
     6 import javax.persistence.GeneratedValue;
     7 import javax.persistence.Id;
     8 import javax.validation.constraints.NotNull;
     9 import javax.validation.constraints.Size;
    10 
    11 @Entity
    12 public class Item {
    13 
    14 
    15     @Id
    16     @GeneratedValue(generator = Constants.ID_GENERATOR)
    17     protected Long id;
    18 
    19     @NotNull
    20     @Size(
    21             min = 2,
    22             max = 255,
    23             message = "Name is required, maximum 255 characters."
    24     )
    25 
    26     protected String name;
    27 
    28     protected Dimensions dimensions;
    29 
    30     protected Weight weight;
    31 
    32     // ...
    33 
    34     public Item() {
    35     }
    36 
    37     public Item(String name, Dimensions dimensions, Weight weight) {
    38         this.name = name;
    39         this.dimensions = dimensions;
    40         this.weight = weight;
    41     }
    42 
    43     public Dimensions getDimensions() {
    44         return dimensions;
    45     }
    46 
    47     public Long getId() { // Optional but useful
    48         return id;
    49     }
    50 
    51     public String getName() {
    52         return name;
    53     }
    54 
    55     public Weight getWeight() {
    56         return weight;
    57     }
    58 
    59     public void setDimensions(Dimensions dimensions) {
    60         this.dimensions = dimensions;
    61     }
    62 
    63     public void setName(String name) {
    64         this.name = name;
    65     }
    66 
    67     public void setWeight(Weight weight) {
    68         this.weight = weight;
    69     }
    70 
    71 }

    5.

  • 相关阅读:
    十一作业
    11.20
    11.13 第十二次、
    11.13 第十一次、
    11.06第十次、
    11.06第九次、
    10.30
    10.23
    10.16
    10.9
  • 原文地址:https://www.cnblogs.com/shamgod/p/5367987.html
Copyright © 2020-2023  润新知