user725455
user725455

Reputation: 475

JPA how to supply inheritance relationships for embeddable objects

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?

Upvotes: 2

Views: 1223

Answers (1)

Narayana Nagireddi
Narayana Nagireddi

Reputation: 749

A better way is to mix both @Embeddable and @MappedSuperclass in your abstract class and then use it in 2 different ways. If Percentage needs to be similar to Resistance, then you wouldn't need @MappedSuperClass at all but if not similar, then below is the way you can achieve the requirement.

@MappedSuperclass
@Embeddable
public class BaseUnit implements Serializable {
...
}

Case 1: BaseUnit as Primary Key

@Entity 
public class AWithResistance implements Serializable {

@EmbeddedId
private BaseUnit key;
....
}

Case 2: BaseUnit as Super Class to inherit common properties

@Entity 
public class AWithPercentage extends BaseUnit implements Serializable {
....
}

Upvotes: 1

Related Questions