我有一个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时,一切正常。
为什么我会收到此例外,有人可以帮助我吗?
最佳答案
我认为问题出在查询中,请记住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();
将注意力放在前。在您要检索的每个字段的开头,并作为from部分的别名。