发现org.hibernate.InstantiationException:无法实例化抽象类或接口,但在恢复所有更改后仍然发生

问题描述 投票:0回答:1

我有一个抽象类:

@Entity
@Table(name = "cas_base_accounts", schema = "connectors_account_service")
@Inheritance(strategy = InheritanceType.JOINED)
@Data
@SuperBuilder
@NoArgsConstructor
public abstract class BaseAccount {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true)
    private Integer id;
//some fields here

还有两个子类:

@Entity
@Table(name = "cas_simple_auth_accounts", schema = "connectors_account_service")
@Inheritance(strategy = InheritanceType.JOINED)
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@SuperBuilder
public class SimpleAuthAccount extends BaseAccount {


    @Id
    @GeneratedValue
    private Integer id;

    @NotNull
    @JsonIgnore
    private String clientId;

    @NotNull
    @JsonIgnore
    private String clientSecret;
//some other fields

并且:

@Entity
@Table(name = "cas_oauth_accounts", schema = "connectors_account_service")
@Inheritance(strategy = InheritanceType.JOINED)
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@SuperBuilder
public class OAuthAccount extends BaseAccount {

    @Id
    @GeneratedValue
    private Integer id;

    @JsonIgnore
    private String state;

    @JsonIgnore
    private LocalDateTime stateExpirationTime;

    @Transient
    private String tokenLink;
//some fields

当我运行简单的 GET 请求时

http://localhost:8889/accountservice/accountIds?cabinetId=12
我收到错误:

threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Cannot instantiate abstract class or interface:  : 
test.connectorsaccountservice.accountservice.model.BaseAccount; nested exception is org.hibernate.InstantiationException: Cannot instantiate abstract class or interface:  : test.connectorsaccountservice.accountservice.model.BaseAccount] 

CabinetId = 12 的实体始终是 SimpleAuthAccount。当我运行例如 CabinetId = 10 时,它返回正确的结果。在这种情况下,id != 12(和 21)的实体是 OauthAccount。

说明该方法出现错误:

    public Mono<List<Integer>> getAccountIds(Integer cabinetId) {
        return findServiceByCabinetId(cabinetId)
                .then(Mono.fromCallable(() -> baseAccountRepository.findByCabinetId(cabinetId)))
                .flatMapIterable(e -> e)
                .filter(oAuthAccount -> !oAuthAccount.getIsDeleted()
                        && UserStatus.ENABLED.equals(oAuthAccount.getUserStatus()))
                .map(BaseAccount::getId)
                .collectList();
    }

在这一行:

.then(Mono.fromCallable(() -> baseAccountRepository.findByCabinetId(cabinetId)))

存储库:

public interface BaseAccountRepository extends JpaRepository<BaseAccount, Integer> {

    Optional<BaseAccount> findByIdAndCabinetId(Integer accountId, Integer cabinetId);

    Optional<BaseAccount> findByCabinetIdAndAccountLogin(Integer cabinetId, String accountLogin);

    List<BaseAccount> findByCabinetId(Integer cabinetId);

SimpleAuthAccountRepository:

@Repository
public interface SimpleAuthAccountRepository extends
        JpaRepository<SimpleAuthAccount, Integer>,
        CustomAccountRepository<SimpleAuthAccount, Integer> {

    @Query("SELECT a FROM SimpleAuthAccount a " +
           "JOIN BaseCabinet c ON a.cabinetId = c.id " +
           "WHERE " +
           "(:realmId IS NULL OR a.realmId = :realmId) AND " +
           "(:accountLogin IS NULL OR a.accountLogin = :accountLogin) AND " +
           "(:accountId IS NULL OR a.id = :accountId) AND " +
           "(:cabinetId IS NULL OR a.cabinetId = :cabinetId) AND " +
           "(:isDeleted IS NULL OR a.isDeleted = :isDeleted) AND " +
           "((:advertiserIds) IS NULL OR a.advertiserId IN (:advertiserIds)) " +
           "ORDER BY c.description, a.accountLogin")
    List<SimpleAuthAccount> findAccounts(
            @Param("realmId") Integer realmId,
            @Param("accountLogin") String accountLogin,
            @Param("accountId") Integer accountId,
            @Param("cabinetId") Integer cabinetId,
            @Param("isDeleted") Boolean isDeleted,
            @Param("advertiserIds") Set<Integer> advertiserIds
    );

}

顺便说一句,看起来它以前有效,但我做了一些改变。发生此错误后,我恢复了所有更改,但错误仍然发生。在这些更改中,我没有触及这篇文章中的任何课程,只触及其他课程。

如何解决?为什么会发生这种情况以及为什么在恢复更改后仍然会发生这种情况? 我想添加

@DiscriminatorColumn
@DiscriminatorValue
会有帮助吗?对我来说,现有的列
cabinetId
@DiscriminatorColumn
一样好,但是如果我有 SimpleAuthAccount 的 id 数组和 OAuthAccount 的 id 数组,我该如何设置它的值?我真的不想在这个表中添加另一列。但当然,如果这里没有更多解决方案,我可以添加像
auth_type
这样的列,并为这些类设置
SIMPLE_AUTH
OAUTH

java hibernate
1个回答
0
投票

在您的数据库中,

cas_base_accounts
中有记录,而
cas_simple_auth_accounts
cas_oauth_accounts
中没有相应的记录。

Hibernate 认为这意味着此类记录具有类型

BaseAccount
并尝试实例化该类。

© www.soinside.com 2019 - 2024. All rights reserved.