更新时嵌入的AttributeOverride无法正常工作 [英] Embedded AttributeOverride on update not working
问题描述
我有一个嵌入两个ModelId
类的实体,其中一个是EmbeddedId
,另一个则引用了另一个实体.
@Entity
public class Report implements Serializable {
@EmbeddedId
private final ModelId id;
@AttributeOverride(name = "id", column = @Column(name = "scheme_id")
@Embedded
private ModelId schemeId;
public void changeScheme(ModelId schemeId) {
this.schemeId = schemeId;
}
}
@Embeddable
public class ModelId implements Serializable {
private Integer id;
}
我可以插入并选择 Report
,但是当我更新字段时,我得到:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int privateId;
:
org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [id] of class [mypackage.ModelId] is mapped
to a primary key column in the database. Updates are not allowed.
我将GlassFish 4.1与EclipseLink 2.5一起使用.
我遗漏了一些东西还是EclipseLink错误?
问题在于只有id
做主键,schemeId
是外键.当我将schemeId
更改为关联时,它正常运行:
@ManyToOne
@JoinColumn(name = "scheme_id", referencedColumnName = "id")
private Scheme scheme;
public void changeScheme(Scheme scheme) {
this.scheme = scheme;
}
我已经在使用多种注释组合进行了测试,并且在GF4.0和EclipseLink 2.6中进行了测试,结果相同.
似乎EclipseLink在更新过程中忽略了@AttributeOverride
,并将内部schemeId.id
与id.id
混合在一起.
我们遇到了同样的问题.看来该缺陷已被eclipselink的现有错误所覆盖(相对于2.6.0版,我仍然在2.6.3中看到此问题):
https://bugs.eclipse.org/bugs/show_bug.cgi ?id = 477638
似乎还没有任何活动,因此与此同时,我将@EmbeddedId
更改为@Embedded
并添加了一个私钥,如下所示:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int privateId;
我还为以前属于主键的属性添加了一个复合唯一约束.我还必须更改EntityManager.find(dataType, id)
调用来进行查询.我希望这会有所帮助.
I have entity with embedded two ModelId
classes, where one is EmbeddedId
and the other references another entity.
@Entity
public class Report implements Serializable {
@EmbeddedId
private final ModelId id;
@AttributeOverride(name = "id", column = @Column(name = "scheme_id")
@Embedded
private ModelId schemeId;
public void changeScheme(ModelId schemeId) {
this.schemeId = schemeId;
}
}
@Embeddable
public class ModelId implements Serializable {
private Integer id;
}
I can insert and select Report
but when I update the field schemeId
I get:
org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [id] of class [mypackage.ModelId] is mapped
to a primary key column in the database. Updates are not allowed.
I use GlassFish 4.1 with EclipseLink 2.5.
Am I missing something or is it EclipseLink bug?
The problem is that only id
makes primary key, schemeId
is foreign key. When I change schemeId
to association it works fine:
@ManyToOne
@JoinColumn(name = "scheme_id", referencedColumnName = "id")
private Scheme scheme;
public void changeScheme(Scheme scheme) {
this.scheme = scheme;
}
I've been testing with many annotation combinations and also in GF4.0 and EclipseLink 2.6 with same result.
It seems like EclipseLink is ignoring @AttributeOverride
during update and is mixing inner schemeId.id
with id.id
.
We had the same issue. It would appear that this defect is covered by an existing bug for eclipselink (against version 2.6.0, I still see this issue in 2.6.3):
https://bugs.eclipse.org/bugs/show_bug.cgi?id=477638
It doesn't appear that there is any activity on this yet, so in the meantime I changed the @EmbeddedId
to be just @Embedded
and added a private primary key instead, like so:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int privateId;
I also added a composite unique constraint for the properties that were formerly part of the primary key. I had to change my EntityManager.find(dataType, id)
calls as well to do a query instead. I hope this helps.
这篇关于更新时嵌入的AttributeOverride无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!