使用属性访问器方法时的EclipseLink实体映射问题 [英] EclipseLink entity mapping problem when using property accessor methods
本文介绍了使用属性访问器方法时的EclipseLink实体映射问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
鉴于下面的类,没有人知道为什么JPA的EclipseLink实现无法将它们映射到数据库实体吗?返回以下错误:
Given the class below does anyone know why EclipseLink implementation of JPA fails to map them to database entities? The following error is returned:
实体类[class com.my.entity.Y]没有指定主键.它应该定义@ Id,@ EmbeddedId或@IdClass.如果您使用这些注释中的任何一个定义了PK,请确保您在实体类层次结构中没有混合的访问类型(带有字段和属性的注释).
@Entity
@Access(AccessType.PROPERTY)
public interface Y {
void setId(Long id);
@Id
Long getId();
}
@Entity
public class Z implements Y {
long id;
@Override
public void setId(Long id) {
this.id = id;
}
@Override
public Long getId() {
return id;
}
}
非常感谢
推荐答案
Thanks to the person who linked to this question - it helped me come up with a solution to the issue as follows:
@Entity
public interface Y {
void setId(Long id);
@Id
Long getId();
}
// introduce intermediate abstract class which implements Y
@Entity
public abstract class X implements Y {
}
// make Z extends X
@Entity
public class Z extends X {
// use targetEntity = X.class where required
// leaving this class still free to use interface Y
long id;
@Override
public void setId(Long id) {
this.id = id;
}
@Override
public Long getId() {
return id;
}
}
这篇关于使用属性访问器方法时的EclipseLink实体映射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文