I have an abstract class "BaseUnit" to represent each unit in my application. I have defined it as @MappedSuperclass.
@MappedSuperclass
public class BaseUnit implements UnitMultiplierInterface, Serializable {
/**
*
*/
private static final long serialVersionUID = -2648650210129120993L;
@Enumerated(EnumType.STRING)
private UnitMultiplier multiplier;
@Enumerated(EnumType.STRING)
private UnitSymbol unit;
@Column(name = "value")
private double value;
@Override
public void setMultiplier(UnitMultiplier multiplier) {
this.multiplier = multiplier;
}
@Override
public UnitMultiplier getMultiplier() {
return multiplier;
}
@Override
public void setUnit(UnitSymbol unit) {
this.unit = unit;
}
@Override
public UnitSymbol getUnit() {
return unit;
}
@Override
public void setValue(double currentL1) {
this.value = currentL1;
}
@Override
public double getValue() {
return value;
}}
I have some subtypes like Percentage, Resistance.
Percentage: I have defined it as @Embeddable
@Embeddable
public class Percentage extends BaseUnit {
/**
*
*/
private static final long serialVersionUID = 2693623337277305483L;
public Percentage() {
super();
}
public Percentage(double value) {
setUnit(UnitSymbol.Percentage);
setValue(value);
}
}
Resistance:. I have defined it as @Embeddable
@Embeddable
public class Resistance extends BaseUnit {
/**
*
*/
private static final long serialVersionUID = -4171744823025503292L;
public Resistance() {
super();
}
public Resistance(double value) {
setUnit(UnitSymbol.Ohm);
setValue(value);
}
}
I can use same/different unit type while defining an entity.Here is an example.
@Entity
public class A implements Serializable {
/**
*
*/
private static final long serialVersionUID = 5167318523929930442L;
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Embedded
private Resistance resistance;
@Embedded
/** resistance of scenario. */
private Percentage percentage;
}
Now, my question do you think this jpa definitions can work without problem? Or can you please tell me most proper way to achieve my requirement?