spring数据classcastexception代理不能转换为-findbyid

snz8szmq  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(246)

我有一个使用spring引导和spring数据在java上开发的api。当我试图从jparepository使用findbyid(id)方法获取一个实体时,我遇到了一个classcastexception错误
收到的异常是:

java.lang.ClassCastException: class com.sun.proxy.$Proxy83 cannot be cast to class package.repository.FAQRepository (com.sun.proxy.$Proxy83 and package.repository.FAQRepository are in unnamed module of loader 'app')

我在尝试调用jparepository.findbyid(id)时收到了它,但是在调用其他spring数据jpa方法(如reposory.save(entity))的其他地方也遇到了同样的错误。

FAQ faq = this.repository.findById(updateFAQ.getId()).orElseThrow(() -> new NotFoundEntityException("FAQ not found"));

常见问题建议:

@Repository
public interface  FAQRepository extends GenericRepository<FAQ> {
    List<FAQ> findByOperative(Operative operative);
}

一般报告:

@Repository
public interface GenericRepository<Entity extends GenericPersistentEntity> extends JpaRepository<Entity, Long> {
}

我的实体:

@Entity
@Table(name = "faqs")
@Getter
@Setter
@SQLDelete(sql = "UPDATE faqs SET deleted_date=NOW() WHERE id=?")
public class FAQ extends GenericPersistentEntity {
    @Lob
    private String question;

    @Lob
    private String answer;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "operative_id")
    private Operative operative;
}

超类实体:

@MappedSuperclass
@Where(clause = "deleted_date is null")
public abstract class GenericPersistentEntity implements GenericPersistentInterface {
    @Getter
    @Setter
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Long id;

    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "creation_date", nullable = false)
    private Date creationDate;

    @UpdateTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_update_date")
    private Date updateDate;

    @Column(name = "deleted_date")
    private Date deletedDate;
}

任何帮助都将不胜感激。谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题