Mysql springboot存储库jpa ClassCastException

Mysql springboot存储库jpa ClassCastException,mysql,hibernate,spring-boot,jpa,spring-data-jpa,Mysql,Hibernate,Spring Boot,Jpa,Spring Data Jpa,我有一个springboot项目,我正在连接我的mysql数据库以执行请求。 我有一个实体导出批次: @Entity(name = "Exportbatch") @Table(name = "exportbatch") public class Exportbatch { @EmbeddedId private ExportbatchId id; @Column(name = "container") private String container; @Column(name = "ser

我有一个springboot项目,我正在连接我的mysql数据库以执行请求。 我有一个实体导出批次:

@Entity(name = "Exportbatch")
@Table(name = "exportbatch")
public class Exportbatch
{
@EmbeddedId
private ExportbatchId id;

@Column(name = "container")
private String container;

@Column(name = "serverurl")
private String serverURL;

@Column(name = "owner")
private String owner;

@Column(name = "batchlastupdate")
private String batchlastupdate;

@Column(name = "size")
private int size;

public Exportbatch()
{

}

public Exportbatch(ExportbatchId id, String container, String serverURL, String owner, String date, int size)
{
    this.id = id;
    this.container = container;
    this.serverURL = serverURL;
    this.owner = owner;
    this.batchlastupdate = date;
    this.size = size;

}

public ExportbatchId getId()
{
    return id;
}

public void setId(ExportbatchId id)
{
    this.id = id;
}

public String getContainer()
{
    return container;
}

public void setContainer(String container)
{
    this.container = container;
}

public String getServerURL()
{
    return serverURL;
}

public void setServerURL(String serverURL)
{
    this.serverURL = serverURL;
}

public String getOwner()
{
    return owner;
}

public void setOwner(String owner)
{
    this.owner = owner;
}

public String getBatchlastupdate()
{
    return batchlastupdate;
}

public void setBatchlastupdate(String batchlastupdate)
{
    this.batchlastupdate = batchlastupdate;
}

public int getSize()
{
    return size;
}

public void setSize(int size)
{
    this.size = size;
}

public String toString()
{
    String string = "project name: " + this.id.getProjectname() + "\n" + "building id: " + this.id.getBuildingId()
            + "\n" + "container id: " + this.container + "\n" + "hemis url: " + this.serverURL + "\n"
            + "lastTimeUpdate: " + this.batchlastupdate;

    return string;
}
exportbatch的唯一键是一个组合对象exportbatchId:

@Embeddable
public class ExportbatchId implements Serializable
{

private static final long serialVersionUID = 1L;

@Column(name = "projectname")
private String projectname;

@Column(name = "buildingid")
private String buildingId;

@Column(name = "timestamp")
private Timestamp timestamp;

public ExportbatchId(String projectname, String buildingId, Timestamp timestamp)
{
    this.projectname = projectname;
    this.buildingId = buildingId;
    this.timestamp = timestamp;
}

public ExportbatchId()
{

}

public String getProjectname()
{
    return projectname;
}

public void setProjectname(String projectname)
{
    this.projectname = projectname;
}

public String getBuildingId()
{
    return buildingId;
}

public void setBuildingId(String buildingId)
{
    this.buildingId = buildingId;
}

public Timestamp getTimestamp()
{
    return timestamp;
}

public void setTimestamp(Timestamp timestamp)
{
    this.timestamp = timestamp;
}
}
我在ExportbatchRepository中定义了一个新方法:

public interface ExportbatchRepository extends JpaRepository<Exportbatch, ExportbatchId>
{

@Query(value = "select id.buildingId, id.projectname, id.timestamp, container, serverURL, owner, batchlastupdate, size,  max(timestamp)  from Exportbatch group by id.buildingId")
List<Exportbatch> findlatest();

Page<Exportbatch> findAll(Pageable pageable);
}
公共接口ExportbatchRepository扩展了JpaRepository
{
@查询(value=“按id.buildingId从Exportbatch组中选择id.buildingId、id.projectname、id.timestamp、容器、服务器URL、所有者、batchlastupdate、大小、最大(时间戳))
列出findlatest();
页面findAll(可分页可分页);
}
在我的控制器中,我正在这样做:

@RestController
public class Controller
{

@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private ExportbatchRepository exportbatchRepository;

@RequestMapping("/getlastbatchsproblemes")
public void getbatchinfo()
{
    try
    {
        List<Exportbatch> list = exportbatchRepository.findlatest();

        for (Exportbatch exportbatch : list)
        {
            // System.out.println(exportbatch.getBatchlastupdate());
            System.out.println(exportbatch.toString());
        }

        System.out.println(list.size());
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
}
}
@RestController
公共类控制器
{
@Autowired//这意味着获取名为userRepository的bean
//它是由Spring自动生成的,我们将使用它来处理数据
专用ExportbatchRepository ExportbatchRepository;
@RequestMapping(“/GetLastBatchsProblems”)
public void getbatchinfo()
{
尝试
{
List List=exportbatchRepository.findlatest();
用于(导出批次导出批次:列表)
{
//System.out.println(exportbatch.getBatchlastupdate());
System.out.println(exportbatch.toString());
}
System.out.println(list.size());
}
捕获(例外情况除外)
{
例如printStackTrace();
}
}
}
调用“/getlastbatchsproblemes”时,我遇到此异常: java.lang.ClassCastException:[Ljava.lang.Object;无法强制转换为example.domain.Exportbatch

但当我打电话给芬德尔时,效果很好


为什么会出现这种异常,有人能帮我解决吗?

我认为问题出在您的查询中,请记住jpql要求您在表中放置别名,并且别名应在每个字段中使用,请尝试以下操作:

@Query(value = "select ex.id.buildingId, ex.id.projectname, ex.id.timestamp, ex.container, ex.serverURL, ex.owner, ex.batchlastupdate, ex.size,  max(ex.id.timestamp)  from Exportbatch ex group by ex.id.buildingId") 
List<Exportbatch> findlatest();
@Query(value=“按ex.id.buildingId从Exportbatch ex组中选择ex.id.buildingId、ex.id.projectname、ex.id.timestamp、ex.container、ex.serverURL、ex.owner、ex.batchlastupdate、ex.size、max(ex.id.timestamp))
列出findlatest();

请注意示例。在您要检索的每个字段的开头,以及作为“发件人”部分的别名。

正如任何JPA文档都会告诉您的那样,如果您选择一些字段,则每行的结果类型都是
Object[]
。这并不能解决我的问题,也不能像这样工作,我在读取查询时出错