Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"IllegalArgumentException occurred while calling setter for property" from sqlserver numeric to java object

I already have such an error, when you try to perform unapropriate matching (date to boolean, and so) which I had been able to fix quite easilly.

But this time, I am quite confused, because hibernate refuses to match a "numeric" Id to a Java "Long" (and it also failed when setter is made for Double, Integer, Float, String, int, long, etc.)

The sql-server field "id" is a NUMERIC(19,0)

My DTO is :

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class DtoResult {

private Long id;
private String name;
// ...


public Long getId() {
    return id;
}
public void setId(final Long id) {
    this.id = id;
}   
public String getName() {
    return name;
}
public void setName(final String name) {
    this.name = name;
}
}

My hibernate query :

    final SQLQuery query= getCurrentSession().createSQLQuery(select + from + where);

    query.setParameter("manyFields", manyFields);
        query
        .addScalar("id")
        .addScalar("name")
        .setResultTransformer(Transformers.aliasToBean(DtoResult.class));

    return query.list(); // List<DtoResult>

Error:

IllegalArgumentException  occurred while calling setter for property [com.some.thing.DtoResult.id (expected type = java.lang.Long)]; target = [com.some.thing.DtoResult@77a70b79], property value = [269895]

I am really puzzled about this, thus any help is welcome.

Thanks for reading untill there.

like image 976
Marvin Avatar asked Jun 02 '26 13:06

Marvin


1 Answers

Just add the expected type, like:

    .addScalar("id", new LongType())
    .addScalar("name", new StringType())
like image 148
K139 Avatar answered Jun 04 '26 03:06

K139



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!