我正在使用单表继承,如所解释的 here在 Glassfish 3.1 环境中。到目前为止,一切都按预期进行。但是在派生实体中添加一些代码来解析 xml 字符串后,JPA 不再加载基本实体的属性。
我已经设置了一个示例 netbeans 项目来重现此问题,您可以从 github here 下载它。 .
这是定义鉴别器字段和测试字段的基本实体:
@Entity
@DiscriminatorColumn(name="discriminator")
@XmlRootElement
public class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String testField;
private String discriminator;
public String getDiscriminator() {
return discriminator;
}
public void setDiscriminator(String discriminator) {
this.discriminator = discriminator;
}
public String getTestField() {
return testField;
}
public void setTestField(String testField) {
this.testField = testField;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof BaseEntity)) {
return false;
}
BaseEntity other = (BaseEntity) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.ce.entityloadingbug.entites.NewEntity[ id=" + id + " ]";
}
}
这是派生实体的代码,其中包含导致 jpa 不再加载字段的代码的私有(private)方法...
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
/**
*
* @author stefan
*/
@Entity
@DiscriminatorValue("derived")
public class DerivedEntity extends BaseEntity {
/**
* The problem code
*/
private void theProblemCode() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
// This is the line that makes Problems!!
Document doc = db.parse("");
}catch(Exception e) {
System.out.println(e);
}
}
}
一旦我删除“Document doc = db.parse("")”行,一切都会再次正常工作。正如您所看到的,尽管从未调用导致问题的代码,但问题仍然存在。
我当前的解决方案是将该代码移至内部类中。但我不喜欢它,因为我不知道问题是什么。
请随时从 github 下载我的示例项目并尝试一下。
这是我用来重现该错误的设置:
- Glassfish 3.1.1
- NetBeans IDE 7.0.1
- 数据库:Derby 10.6.2.1
- JPA:EclipseLink 2.3
事实上,我使用的所有库都随适用于 Linux 的 NetBeans IDE 7.0.1 安装程序一起提供。 。 感谢您的回答或建议。
最佳答案
这很奇怪。检查您的日志是否有错误或任何日志记录。在 persistence.xml 中的 EclipseLink 中启用最好的日志记录,因此发生了奇怪的情况。
确保这两个类都列在您的 persistence.xml 中。
另外尝试禁用编织(“eclipselink.weaving”=“false”),这会影响问题吗?
还包括记录的 SQL,它是否选择数据?
关于java - 当派生实体包含 SAX 代码时,不会加载派生实体的 JPA 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9567229/