为 class 提供的类型错误的 ID 得到了 class java.lang.Integer

Provided id of the wrong type for class got class java.lang.Integer

每次调用 getLocation 方法时都会出错

error - Provided id of the wrong type for class Expected: class got class java.lang.Integer

@Id
@Column(name = "emi")
public String getEmi() {
    return emi;
}

public void setEmi(String emi) {
    this.emi = emi;
}

@Id
@Column(name = "latitude")
public Double getLatitude() {
    return latitude;
}
public void setLatitude(Double latitude) {
    this.latitude = latitude;
}

@Id
@Column(name = "longitude")
public Double getLongitude() {
    return longitude;
}
public void setLongitude(Double longitude) {
    this.longitude = longitude;
}

管理器中的themetjod是这样写的class

public Location getLocation(int id ) {
    session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    Location location = (Location) session.get(Location .class, id);
    session.getTransaction().commit();
    session.close();
    return location;
}

提供给 session.get() 的 ID 类型错误。你应该已经通过错误信息自己识别出来了!

Provided id of the wrong type for class com.nsbm.entity.Location. Expected: class com.nsbm.entity.Location, got class java.lang.Integer

您应该更改 session.get() 方法调用以解决此问题。我不知道这个方法的逻辑,但是为了解决这个问题,我已经为你指出了错误。

public Location getLocation(int id ) {
    //some code here

    Location location = (Location) session.get(Location .class, id);
    // id that is being passed here in the sessin.get() method needs to be changed to type `com.nsbm.entity.Location` to resolve this error.

    //some code here
    return location;
}

希望对您有所帮助!

您的跟踪器中只有一个主键table,并且您已将每一列都定义为一个 Id,

您的位置 class 应该如下所示。

@Entity
@Table(name = "tracker")
public class Location {
    @Id
    @Column(name="id")
    public Integer getId(){
        return this.id;
    }
    public void setId(Integer id){
        this.id = id;
    }

    @Column(name = "emi")
    public String getEmi() {
        return emi;
    }

    public void setEmi(String emi) {
        this.emi = emi;
    }

    @Column(name = "latitude")
    public Double getLatitude() {
        return latitude;
    }
    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    @Column(name = "longitude")
    public Double getLongitude() {
        return longitude;
    }
    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }
}

其他一切正常。

试试这个 class。