JaxB无法强制转换为java.lang.Integer

时间:2013-05-15 12:35:59

标签: java jaxb

我有一个抽象类:

@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

**

@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);

**

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

默认情况下,当JAXB (JSR-222)实现为Pessoa创建元数据时,它还将为超类BaseEntity创建元数据。由于JAXB默认将属性视为已映射,因此它将考虑它具有类型为id的{​​{1}}属性。当JAXB不知道属性的类型时,它会将其转换为DOM Object。这导致Element

<强>解决方案

解决方案实际上取决于您是否希望ClassCastException被认为是继承层次结构的一部分(参见:http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html)。但我建议利用BaseEntity上的@XmlTransient@XmlAccessorType(XmlAccessType.NONE)删除有问题的属性: