我有继承工作,但它不是很干。每个新的 bolt
类型我都必须重复代码。最好展示我的类(class)然后进一步解释
我的 BoltSpec
父类(与紧固件有关的尺寸)
@Entity
@Table(name="BoltSpecs")
@IdClass(BoltSpecCK.class)
@DiscriminatorColumn(name="boltType" )
public abstract class BoltSpec implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String size;
@Id
@Enumerated(EnumType.STRING)
private EnumBoltType boltType;
private BigDecimal basic_major_diameter = BigDecimal.ZERO;
和我的
EnumBoltType
public enum EnumBoltType {
CYLINDER_HEAD_CAP_SCREW("CYLINDER HEAD CAP SCREW", EnumHeadType.CYL),
HEX_CAP_SCREW("HEX CAP SCREW", EnumHeadType.HEX),
HEAVY_HEX_CAP_SCREW("HEAVY HEX CAP SCREW", EnumHeadType.HEX),
HEX_BOLT("HEX BOLT", EnumHeadType.HEX),
HEAVY_HEX_BOLT("HEAVY HEX BOLT", EnumHeadType.HEX),
FLAT_COUNTERSUNK_HEAD_CAP_SCREW("FLAT COUNTERSUNK HEAD CAP SCREW", EnumHeadType.CONE);
然后我必须有重复的类,即
HexCapScrew
、 HeavyHexCapScrew
等和 HexCapScrewSpec
、 HeavyHexCapScrewSpec
等,即使它们具有相似的 BoltSpec
属性(不是数值)public class HexCapScrew extends Bolt {
private static final long serialVersionUID = 1L;
private static HexCapScrewSpec spec;
public HexCapScrew() {
super(spec);
}
}
public class HeavyHexCapScrew extends Bolt {
private static final long serialVersionUID = 1L;
private static HeavyHexCapScrewSpec spec;
public HeavyHexCapScrew () {
super(spec);
}
}
...
@Entity
@DiscriminatorValue("HEX_CAP_SCREW")
public class HexCapScrewSpec extends BoltSpec implements Serializable {
private static final long serialVersionUID = 1L;
public HexCapScrewSpec() {
super();
}
private BigDecimal flat_diameter = BigDecimal.ZERO;
...
@Entity
@DiscriminatorValue("HEAVY_HEX_CAP_SCREW")
public class HeavyHexCapScrewSpec extends BoltSpec implements Serializable {
private static final long serialVersionUID = 1L;
public HeavyHexCapScrewSpec() {
super();
}
private BigDecimal flat_diameter = BigDecimal.ZERO;
...
这个规范是不同的
@Entity
@DiscriminatorValue("FLAT_COUNTERSUNK_HEAD_CAP_SCREW")
public class FlatHeadCapScrewSpec extends BoltSpec implements Serializable {
private static final long serialVersionUID = 1L;
public FlatHeadCapScrewSpec() {
super();
}
private BigDecimal cone_angle = BigDecimal.ZERO;
...
这是一些示例 import.sql 数据
insert into BoltSpecs (basic_size, basic_major_diameter, boltType, flat_diameter)
values ('2-3/4', '2.75', 'HEX_CAP_SCREW', '3.988')
values ('3', '3', 'HEX_CAP_SCREW', '4.35')
values ('3/8', '0.375', 'HEAVY_HEX_CAP_SCREW', '0.669')
insert into BoltSpecs (basic_size, basic_major_diameter, boltType, cone_angle)
values ('2-3/4', '2.75', 'FLAT_COUNTERSUNK_HEAD_CAP_SCREW', '39.77')
我在前端有一个下拉菜单,用户可以在其中选择 bolt 类型并需要使用适用的 bolt 规范。我不想修改我的 import.sql 并放置 bolt 头类型。我冒着将
CONE
放在 HEX
类型 bolt 上的风险。有没有办法做多个 DiscriminatorValue
s?喜欢:@Entity
@DiscriminatorValue("HEX_CAP_SCREW, HEAVY_HEX_CAP_SCREW, HEX_BOLT, HEAVY_HEX_BOLT")
public class BoltSpecHexHead extends BoltSpec implements Serializable {
private static final long serialVersionUID = 1L;
public BoltSpecHexHead () {
super();
}
private BigDecimal flat_diameter = BigDecimal.ZERO;
...
public class BoltHexHead extends Bolt {
private static final long serialVersionUID = 1L;
private static BoltSpecHexHead spec;
public BoltSpecHexHead () {
super(spec);
}
}
或者我如何将 EnumHeadType 作为鉴别器值?具有相似头部的 bolt (EnumHeadType.CYL、HEX 和 CONE)之间的规范相似
最佳答案
我能够使用 @DiscriminatorFormula
实现解决方案
@Entity
@Table(name="BoltSpecs")
@IdClass(BoltSpecCK.class)
//@DiscriminatorColumn(name="boltType" )
@DiscriminatorFormula("case when boltType in ('CYLINDER_HEAD_CAP_SCREW') then 'HEX'
when boltType in ('HEX_CAP_SCREW','HEAVY_HEX_CAP_SCREW','HEX_BOLT','HEAVY_HEX_BOLT') then 'HEX'
when boltType in ('FLAT_COUNTERSUNK_HEAD_CAP_SCREW') then 'CONE' end")
public abstract class BoltSpec implements Serializable {
关于java - JPA 继承不是 DRY,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34665286/