我有继承工作,但它不是很干。每个新的bolt
类型我都必须重复代码。最好展示我的课程,然后进一步解释
我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')
我在前端有一个下拉菜单,用户选择螺栓类型并需要使用适用的螺栓规格。我不想修改我的import.sql并放置螺栓头类型。我冒着为CONE
类型螺栓说HEX
的风险。有没有办法做多个DiscriminatorValue
?像:
@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作为鉴别器值进行ping操作?具有相似磁头的螺栓(EnumHeadType.CYL,HEX和CONE)之间的规格类似
答案 0 :(得分:0)
我能够使用@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 {
答案 1 :(得分:0)
为什么不能你为所有这些使用相同的类(比方说Bolt
)?它们看起来相似,它们共享相同的属性,它们的行为是否会改变?如果是这样,那么只需使用基于实例的螺栓类型的装饰器。