使用自定义查询时,Spring JPA抛出ClassCastException

时间:2019-03-15 21:13:48

标签: java spring jpa spring-data-jpa

我试图从查询中仅获取特定的列集,但是,我正在获取ClassCastException。

2019-03-16 00:03:19.321 ERROR 8440 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.metadata.entity.FileItem] with root cause

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.metadata.entity.FileItem
    at com.metadata.service.FileItemService.findFileWithVersion(FileItemService.java:39) ~[classes/:na]
    at com.metadata.service.FileItemService$$FastClassBySpringCGLIB$$7a805cbb.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]

这是我结构的基础。

FileItem

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;
import java.util.UUID;

@Entity
@IdClass(FileItemId.class)
@Table(name = "files")
@AllArgsConstructor
@NoArgsConstructor
@Data
public class FileItem {

    @Id
    private UUID id;

    @Id
    private long version;

    @Column(name = "total_size")
    private long totalSize;

    private String name;
    private byte[] file;

    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_at")
    private Date createdAt;
}

FileItemId.java

import lombok.Data;
import java.io.Serializable;
import java.util.UUID;

@Data
public class FileItemId implements Serializable {
    private UUID id;
    private Long version;
}

FileItemRepository

import com.metadata.entity.FileItem;
import com.metadata.entity.FileItemId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.UUID;

public interface FileItemRepository extends JpaRepository<FileItem, UUID> {
    @Query("select f.id, f.version, f.name, f.totalSize, f.createdAt " +
            "from FileItem f " +
            "where f.id = ?1 and f.version = ?2")
    List<FileItem> findByFileIdAndVersion(UUID id, Long version);

}

2 个答案:

答案 0 :(得分:1)

很长一段时间我没有使用JPA,但是您的查询似乎返回了一个数组Object []

然后方法的签名应该是:

@Query("select f.id, f.version, f.name, f.totalSize, f.createdAt " +
            "from FileItem f " +
            "where f.id = ?1 and f.version = ?2")
    Object[] findByFileIdAndVersion(UUID id, Long version);

否则,如果要保留方法签名:

@Query("select f " +
            "from FileItem f " +
            "where f.id = ?1 and f.version = ?2")
    List<FileItem> findByFileIdAndVersion(UUID id, Long version);

另一种解决方案:

@Query("select new com.mycompany.mypackage.MyDTO(f.id, f.version, f.name, f.totalSize, f.createdAt) " +
            "from FileItem f " +
            "where f.id = ?1 and f.version = ?2")
    MyDTO findByFileIdAndVersion(UUID id, Long version);

MyDTO是您根据需要编写的自定义Java bean。

答案 1 :(得分:0)

我在春季使用projections找到了解决方案。但是,我不确定这是否是最佳解决方案。

FileItemNoBlob

import java.util.Date;
import java.util.UUID;

public interface FileItemNoBlob {
    UUID getId();
    Long getVersion();
    Date getCreatedAt();
    Long getTotalSize();
    String getName();
}
存储库中的

FileItemNoBlob findByIdAndVersion(UUID id, Long version);似乎可以正常工作。最好为投影类创建另一个存储库,以使其更加整洁。