JPA - InheritanceType.Joined 生成错误的表
我试图在数据库中表示一个具有多种字段类型的可编辑表。 我的方法如下所示:
TABLES
- uuid //Primary key
- cols
- rows
- name
VALUE
-col //Key element
-row //Key element
-parent //Key element - table to witch the value belongs
-v_type //@DiscriminatorColumn
STRING_VALUE
-col //Key
-row //Key
-parent //Key
-value
这适用于更多值类型。
我已经使用 eclipselink 2.3 映射它,如下所示:
Table.java
@SuppressWarnings("serial")
@Entity(name="TABLES")
public class Table implements Serializable{
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
long uuid;
String name;
int rows=0,cols=0;
@OneToMany(targetEntity=Value.class,mappedBy="parent",cascade={CascadeType.ALL},orphanRemoval=true)
@OrderBy("row ASC")
List<Value> values;
}
Value.java
@SuppressWarnings("serial")
@Entity
@IdClass(ValueId.class)
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="V_TYPE",length=1,discriminatorType=DiscriminatorType.STRING)
public abstract class Value implements Serializable{
@Id
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="PARENT")
Table parent;
@Id @Column(unique=false)
int row;
@Id @Column(unique=false)
int col;
}
ValueId.java
@SuppressWarnings("serial")
public class ValueId implements Serializable{
int row,col;
long parent;
// Getters setters etc
}
StringValue.java
@SuppressWarnings("serial")
@Entity
@DiscriminatorValue("S")
public class StringValue extends Value {
String value;
}
但通过此实现,生成的 STRING_VALUE 表 缺少 [parent] 字段。这很糟糕,因为我无法解析一个值。
我做错了什么? ;) 我无法“谷歌解决”这个问题。
I'm trying to represent in data base a editable table, with multiple field types.
My approach looks like this:
TABLES
- uuid //Primary key
- cols
- rows
- name
VALUE
-col //Key element
-row //Key element
-parent //Key element - table to witch the value belongs
-v_type //@DiscriminatorColumn
STRING_VALUE
-col //Key
-row //Key
-parent //Key
-value
This goes on for some more value types.
I've mapped it using eclipselink 2.3 as follows:
Table.java
@SuppressWarnings("serial")
@Entity(name="TABLES")
public class Table implements Serializable{
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
long uuid;
String name;
int rows=0,cols=0;
@OneToMany(targetEntity=Value.class,mappedBy="parent",cascade={CascadeType.ALL},orphanRemoval=true)
@OrderBy("row ASC")
List<Value> values;
}
Value.java
@SuppressWarnings("serial")
@Entity
@IdClass(ValueId.class)
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="V_TYPE",length=1,discriminatorType=DiscriminatorType.STRING)
public abstract class Value implements Serializable{
@Id
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="PARENT")
Table parent;
@Id @Column(unique=false)
int row;
@Id @Column(unique=false)
int col;
}
ValueId.java
@SuppressWarnings("serial")
public class ValueId implements Serializable{
int row,col;
long parent;
// Getters setters etc
}
StringValue.java
@SuppressWarnings("serial")
@Entity
@DiscriminatorValue("S")
public class StringValue extends Value {
String value;
}
But with this implementation the resulting STRING_VALUE table
is lacking [parent] field. Thats bad because i can't resolve a value.
What have i done wrong ? ;)
I could not "google resolve" this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Value
中parent
字段上的@Id
注释应替换为@MapsId
。但是,如果您在
Value
中拥有一个技术性的、非功能性的自动生成 ID,并让row
、col
和parent
字段是实体的常规字段。这肯定会提高性能,并且在应用程序的所有部分中更易于使用。The
@Id
annotation on theparent
field inValue
should be replaced by@MapsId
.But you would really do yourself a favor by having a technical, non-functional, auto-generated ID in
Value
, and let therow
,col
andparent
field be regular fields of the entity. This wouldcertainly make things more performant, and much easier to use in all the parts of the application.