【问题标题】:springboot repository jpa ClassCastExceptionspringboot 存储库 jpa ClassCastException
【发布时间】:2018-08-31 16:04:13
【问题描述】:

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

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

在我的控制器中,我正在这样做:

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

当我调用“/getlastbatchsproblemes”时出现此异常: java.lang.ClassCastException: [Ljava.lang.Object;无法转换为 example.domain.Exportbatch

但是当我调用 findAll 时它工作正常。

为什么会出现这个异常,有人可以帮我解决这个问题吗?

【问题讨论】:

  • 任何 JPA 文档都会告诉您,如果您选择某些字段,那么每行的结果类型为 Object[]

标签: mysql hibernate spring-boot jpa spring-data-jpa


【解决方案1】:

我认为问题出在您的查询中,请记住 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();

将注意力放在您要检索的每个字段开头的 ex. 中,并在 from 部分中作为别名。

【讨论】:

  • 它不能解决我的问题,也不能像这样工作,读取查询时出现错误
猜你喜欢
  • 1970-01-01
  • 2018-01-26
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-26
相关资源
最近更新 更多