JPA如何为可嵌入对象提供继承关系 [英] JPA how to supply inheritance relationships for embeddable objects

查看:104
本文介绍了JPA如何为可嵌入对象提供继承关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类BaseUnit来表示我的应用程序中的每个单元。我将它定义为 @MappedSuperclass

  @MappedSuperclass 
公共类BaseUnit实现UnitMultiplierInterface ,可序列化{
/ **
*
* /
private static final long serialVersionUID = -2648650210129120993L;
@Enumerated(EnumType.STRING)
private UnitMultiplier乘数;
@Enumerated(EnumType.STRING)
私人UnitSymbol单位;
@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(){
返回值;
}}

我有一些子类型,例如百分比,阻力。



百分比:我将它定义为@Embeddable

  @可嵌入的
public class Percentage extends BaseUnit {
/ **
*
* /
private static final long serialVersionUID = 2693623337277305483L;

public百分比(){
super();
}

public百分比(双值){
setUnit(UnitSymbol.Percentage);
setValue(value);
}

}

电阻:即可。我将它定义为@Embeddable

  @Embeddable 
公共类阻力扩展BaseUnit {
/ **
*
* /
private static final long serialVersionUID = -4171744823025503292L;

public Resistance(){
super();
}

公众抵抗(双值){
setUnit(UnitSymbol.Ohm);
setValue(value);
}

}

  @Entity 
公共类A实现Serializable {
/ **
*
* /
private static final long serialVersionUID = 5167318523929930442L;
@Id
@GeneratedValue
@Column(name =id)
私人长ID;
@嵌入
私人抵抗;
@Embedded
/ **方案阻力。 * /
私人百分比;

}



现在,我的问题是你认为这个jpa定义可以毫无问题地工作吗?或者你能告诉我最正确的方法来达到我的要求吗? 更好的方法是将 @Embeddable @MappedSuperclass ,然后以两种不同的方式使用它。如果百分比需要与阻力类似,那么根本不需要 @MappedSuperClass ,但如果不相似,则可以通过以下方式达到要求。 p>

  @MappedSuperclass 
@Embeddable
公共类BaseUnit实现Serializable {
...
}

案例1:BaseUnit作为主键

  @Entity 
public class AWithResistance实现可序列化{

@EmbeddedId
私有BaseUnit键;
....
}

案例2:BaseUnit as Super Class继承公共属性

  @Entity 
公共类AWithPercentage扩展BaseUnit实现Serializable {
....
}


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?

解决方案

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 {
....
}

这篇关于JPA如何为可嵌入对象提供继承关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆