Rodrigo
Rodrigo

Reputation: 61

JaxB cannot be cast to java.lang.Integer

I have an abstract class:

@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?

Upvotes: 3

Views: 3670

Answers (1)

bdoughan
bdoughan

Reputation: 149047

By default when your JAXB (JSR-222) implementation is creating metadata for Pessoa it is also going to create metadata for the super class BaseEntity. Since JAXB by default considers properties as mapped it is going to consider that it has a property called id of type Object. When JAXB doesn't know the type of the property it will convert it to a DOM Element. This is resulting in the ClassCastException.

Solution

The solution really depends upon whether or not you want BaseEntity considered part of the inheritance hierachy (see: http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html). But what I would recommend is either leveraging @XmlTransient or @XmlAccessorType(XmlAccessType.NONE) on BaseType to remove problematic properties:

Upvotes: 3

Related Questions