我有一个抽象类:
@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);
简单身份验证帐户存储库:
@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
类的列。
在您的数据库中,您有 中的记录,但没有或 中
cas_base_accounts
的对应记录。cas_simple_auth_accounts
cas_oauth_accounts
Hibernate 认为这意味着此类记录具有类型
BaseAccount
并尝试实例化该类。