gpt4 book ai didi

mysql - springboot 存储库 jpa ClassCastException

转载 作者:行者123 更新时间:2023-11-29 17:04:24 25 4
gpt4 key购买 nike

我有一个 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);
}

在我的 Controller 中我正在这样做:

@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 时它工作正常。

为什么我会遇到此异常,有人可以帮助我解决这个问题吗?

最佳答案

我认为问题出在您的查询中,请记住 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 部分中的别名。

关于mysql - springboot 存储库 jpa ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52119188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com