Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringData JPA - Provided id of the wrong type for class . Expected: class java.lang.Integer, got class java.lang.Long

i'm facing this issue while using Spring JPA and trying to retrieve a List of objects.

This is the class i'm trying to retrieve

@Entity
@Table(name="OBJECTSTERMIC")
public class TermicObject {

    @Id
    @Column(name="TERMICID")
    private long termicId;

    @MapsId
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="OBJECTID",columnDefinition="INTEGER")
    private Object object;

    @Column(name="CONTECA_RIF")
    private int contecaRif;

    @Column(name="CONTECA_VAL")
    private int contecaVal;

    @Column(name="TYPE")
    private String type;

//getters and setters

The Object class has the primary key on MySQL stored as an Integer, indeed this is Object

@Entity
public class Object {

    @Column(name="OBJECTID")
    @Id
    @JsonProperty("OBJECTID")
    private int objectId;
    ....

So, nowhere is set a Long...

Now, i simply call in a service class

@Override
    public List<TermicObject> findAll() {

        return repository.findAll();
    }

and got this exception

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TypeMismatchException: Provided id of the wrong type for class it.besmart.db_eipo.persistence.model.Object. Expected: class java.lang.Integer, got class java.lang.Long; nested exception is java.lang.IllegalArgumentException: org.hibernate.TypeMismatchException: Provided id of the wrong type for class it.besmart.db_eipo.persistence.model.Object. Expected: class java.lang.Integer, got class java.lang.Long

Where is set that Object Id should be Long?

like image 690
MarioC Avatar asked Oct 26 '17 17:10

MarioC


1 Answers

Have a look at definition of your repository. Does it have right generic type? do you have Integer as second parameter? IMHO this can be root cause. See proposed correct version:

@RepositoryRestResource
public interface TermicObjectRepository extends JpaRepository<TermicObject, Integer> {
    public Optional<TermicObject> findById(Integer id);
    public List<TermicObject> findAll()
}
like image 79
Lubo Avatar answered Nov 15 '22 08:11

Lubo