2013-03-10 45 views
0

我正在嘗試與處女座和EclipseLink合作並實施基於Greenpage項目的應用程序。EclipseLink層次結構錯誤

我實現了一個層次,但我得到(使用的EclipseLink)一個奇怪的錯誤:

Internal Exception: Exception [EclipseLink-7161] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.ValidationException 
Exception Description: Entity class [class model.Person] has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass. If you have defined PK using any of these annotations then make sure that you do not have mixed access-type (both fields and properties annotated) in your entity class hierarchy. 

層次:

實體< - NamedEntity < - 人< - ...... - 其他類,擴展人

import java.io.Serializable; 

import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.MappedSuperclass; 

@MappedSuperclass 
public abstract class Entity implements Serializable { 

    private static final long serialVersionUID = 1L; 

    private Long id; 

    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 
} 


import javax.persistence.MappedSuperclass; 

@MappedSuperclass 
public abstract class NamedEntity extends Entity { 
    private static final long serialVersionUID = 1L; 

    private String name; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @Override 
    public String toString() { 
     return name; 
    } 
} 



import javax.persistence.DiscriminatorColumn; 
import javax.persistence.Entity; 
import javax.persistence.Inheritance; 
import javax.persistence.InheritanceType; 

import pl.com.mgr.model.NamedEntity; 

@Entity 
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) 
@DiscriminatorColumn(name = "discriminator") 
public abstract class Person extends NamedEntity { 
    private static final long serialVersionUID = 1L; 

} 

編輯:由凱文Bowersox(非常感謝!)發佈的答覆...部分;)。問題進一步深入。我們現在是:實體< - NamedEntity < - 屬性< - LecturerAttribute

import javax.persistence.Column; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.MappedSuperclass; 

import model.NamedEntity; 

@MappedSuperclass 
public abstract class Attribute<T extends NamedEntity> extends NamedEntity { 
    private static final long serialVersionUID = 1L; 

    private T entity; 
    private String value; 

    @ManyToOne 
    @JoinColumn(name = "entity", nullable = false) 
    public T getEntity() { 
     return entity; 
    } 

    public void setEntity(T entity) { 
     this.entity = entity; 
    } 

    @Column(nullable = false) 
    public String getValue() { 
     return value; 
    } 

    public void setValue(String value) { 
     this.value = value; 
    } 


} 




import javax.persistence.Entity; 

@Entity 
public class LecturerAttribute extends Attribute<Lecturer> { 
    private static final long serialVersionUID = 1L; 

} 

和異常:

Internal Exception: Exception [EclipseLink-7161] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.ValidationException 
Exception Description: Entity class [class model.LecturerAttribute] has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass. If you have defined PK using any of these annotations then make sure that you do not have mixed access-type (both fields and properties annotated) in your entity class hierarchy. 

回答

3

移動從訪問到字段聲明註釋改變域無障礙保護。

@MappedSuperclass 
public abstract class Entity implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    protected Long id; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 
} 
+0

屬性訪問是一個完全有效的使用。 – 2013-03-10 18:47:19

+0

謝謝,但請參閱我的編輯;) – PastorPL 2013-03-10 18:48:49

+0

@ user872846你能發佈新的錯誤嗎?嘗試使抽象類中的所有字段都受保護。我懷疑EclipseLink可能需要直接訪問該字段,而私有作用域正在阻止該字段。 – 2013-03-10 18:52:26

-1

好的,凱文鮑爾斯科克回答指示我正確的答案:我改變所有字段保護和更改註釋字段。這導致良好的工作;)。

+0

在凱文的解決方案中哪些內容不適合您?我沒有看到任何區別。 – tak3shi 2016-02-12 09:47:46