2013-05-15 44 views
3

我有一個抽象類: JaxB不能轉換爲java.lang.Integer

@MappedSuperclass 
public abstract class BaseEntity<K> 
{ 

@Temporal(value = TemporalType.TIMESTAMP) 
private Date cadastrado; 
@Temporal(value = TemporalType.TIMESTAMP) 
private Date modificado; 
@Column(length = 30) 
private String ip; 
private String autorModificacao; 

public abstract K getId(); 

public abstract void setId(K id); 

...

and a derived class:

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Pessoa extends BaseEntity<Integer> implements Serializable { 

    @Id 
    @ColumnGridPF 
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "pessoa") 
    private Integer id; 

.... 

@Override 
Integer getId() { 
    return id; 
} 

    @Override 
    public void setId(Integer id) { 
     this.id = id; 
    } 

....

when my application try to unmarshall the object, I get an error

**

SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container 
java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to java.lang.Integer 
    at br.com.sigmaonline.entity.cadastro.pessoa.Pessoa.setId(Pessoa.java:46) 
    at br.com.sigmaonline.entity.common.generic.BaseEntity$JaxbAccessorM_getId_setId_java_lang_Object.set(MethodAccessor_Ref.java:60) 

**

Can Any one help me?

+0

您使用該JAXB的實施?此代碼適用於JDK(6&7)JAXB實現(除非您在ID元素中看到名稱空間定義) –

回答

3

By default when your JAXB (JSR-222) implementation is creating metadata for Pessoa它也將爲超類BaseEntity創建元數據。由於默認情況下JAXB將屬性視爲映射,因此它將認爲它具有類型的名爲id的屬性。當JAXB不知道屬性的類型時,它會將其轉換爲DOM Element。這導致ClassCastException

解決方案

該解決方案實際上取決於你是否希望在繼承層次的BaseEntity認爲一部分(見:http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html)。但我會建議要麼利用@XmlTransient@XmlAccessorType(XmlAccessType.NONE)BaseType刪除有問題的性質:

相關問題