2013-12-11 41 views
-1

我想用JPA 2.1來實現這個模型。我正在使用JSR 338規範和參考實現Eclipselink如何在使用JPA的編程模型中使用繼承?

只有第三級和關聯類的實體纔會被保留。 enter image description here

@MappedSuperclass 

public abstract class PessoaMaster implements Serializable { 

private static final long serialVersionUID = 1L; 
private long id; 
private List<Telefone> telefones; 
@Id 
@GeneratedValue(strategy=GenerationType.IDENTITY) 
@Column(name="ID_Pai", unique=true, nullable=false) 
public long getId() { 
    return id; 
} 
public void setId(long identificador) { 
    id = identificador; 
} 
/** 
* @return the telefones 
*/ 
@OneToMany 
public List<Telefone> getTelefones() { 
    return telefones; 
} 

/** 
* @param telefones the telefones to set 
*/ 
public void setTelefones(List<Telefone> telefones) { 
    this.telefones = telefones; 
} 
} 

我可以用它代替繼承MappedSuperclass這裏?

@Entity 
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)  
public abstract class FornecedorSuper extends PessoaMaster{ 

    //attributes and relationships 

} 

堅持的實體。

public class FornecedorPecas extends FornecedorSuper { 

private Double Valor; 

@Column(name="ValorPeca") 
public Double getValor() { 
    return Valor; 
} 

public void setValor(Double valor) { 
    Valor = valor; 
} 
} 

有必要用@實體標記FornecedorPeças類?

當我在FornecedorSuper中插入MappedSuperclass。這個異常被拋出:

Exception in thread "main" Local Exception Stack: Exception [EclipseLink-30005] (Eclipse Persistence Services - .5.0.v20130507-3faac2b):org.eclipse.persistence.exceptions.PersistenceUnitLoadingException Exception Description: An exception was thrown while searching for persistence archives ith ClassLoader: [email protected] Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.EntityManagerSetupException 
Exception Description: Predeployment of PersistenceUnit [modelo] failed. 
Internal Exception: Exception [EclipseLink-7161] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationException 
Exception Description: Entity class [class br.miltex.dominio.model.FornecedorPecas] 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. 

回答

0

我已經對屬性和方法進行了註釋。出於這個原因發生了例外。

感謝您的幫助。

1

Q1。我可以使用而不是繼承MappedSuperclass在這裏?

雖然我沒有找到符合該規範的任何實例,JPA規範說:

的MappedSuperclass註解指定一個類,其映射 信息被應用到繼承它實體。映射的 超類沒有爲其定義單獨的表。

所以說,映射信息被應用到實體,但它並沒有說一個MappedSuperclass不允許再延長MappedSuperclass,所以我相信你可以在你FornecedorSuper@MappedSuperclass註釋使用。第二季度銷售價格指數爲:

Q2。有必要用@實體標記FornecedorPeças類?

是的,這是必要的。

+0

嗨Andrei.The上述例外是拋出。謝謝! – Miltex

+0

安德烈,他的回答並沒有解決我的問題。規範可以爲所有人免費使用。你所引用的我已經閱讀過。他的回答是一種援助。所以我將其標記爲有用。謝謝。 – Miltex

+0

安德烈,因爲你刪除了以前的評論? – Miltex