2

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?

user725455
  • 465
  • 10
  • 36
  • I don't think it will work. I doubt an `@Embeddable` can inherit from a `@MappedSuperclass`. Have you tested this? Also, since `@Embeddable` properties basically become columns in the reference entity's table, I think there would be naming conflicts when trying to create the table because of both `resistance` and `percentage` inheriting the same properties from `BaseUnit`. In that case you would have to qualify the column names with `@AttributeOverrides`. – Donovan Muller Jun 30 '14 at 14:35
  • What is your advice for defining this embeddabşe inheritance relationships? I do not want to store all units in seperate tables, So I am not defining as Entity. – user725455 Jun 30 '14 at 14:55
  • I would remove the `@MappedSuperclass` annotation from `BaseUnit` and then actually go test this app. It will probably fail when trying to create the table (assuming JPA is doing that for you) because of duplicate column names (from `BaseUnit`), then go read up on `@AttributeOverrides` and how to avoid this problem. Look at the answers [to this question](http://stackoverflow.com/q/4432748/2408961) as a starting point. – Donovan Muller Jun 30 '14 at 15:13

1 Answers1

1

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 {
....
}
Narayana Nagireddi
  • 729
  • 1
  • 13
  • 33