【问题标题】:Error updating Spring-data-jpa on MappedSuperclass在 MappedSuperclass 上更新 Spring-data-jpa 时出错
【发布时间】:2014-07-12 16:34:02
【问题描述】:

从版本 1.4.3 升级到 1.5+ 或 1.6.0 时出现错误; 我正在使用休眠 4.3.5

例外情况是: org.springframework.beans.factory.BeanCreationException:创建名为“IAccountRepository”的bean时出错:调用init方法失败;嵌套异常是 java.lang.IllegalArgumentException:此类 [class com.model.entities.BaseEntity] 未定义 IdClass

和实体:

@MappedSuperclass
@EntityListeners(EntityAuditListener.class)
public abstract class BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Audited
    @Basic(optional = false)
    @Column(name = "isactive", nullable = false, columnDefinition = "BOOLEAN DEFAULT TRUE")
    private boolean isActive = true;

    protected BaseEntity() {
    }

    protected BaseEntity(boolean isActive) {
        this.isActive = isActive;
    }

    ........... more attributes and getters and setters
}

@Entity
@Table(name = "accounts", schema = "public")
@SequenceGenerator(name = "seq_account", sequenceName = "seq_account", initialValue = 1, allocationSize = 1)
@Audited
public class Account extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_account")
    @Column(name = "accountid")
    private Long accountId;

    ---- more attributes and getters and setters
}

在我看来,Spring-data-jpa 以与 Hibernate 相同的方式检查层次结构,但将超类视为一个实体。

你知道这是我的错误还是错误以及任何解决方法?

非常感谢。

编辑

我的仓库如下:

@Transactional(propagation = Propagation.MANDATORY)
@NoRepositoryBean
public interface IBaseRepository<T extends BaseEntity, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {
    public Page<T> findByIsActiveTrue(Pageable pageable);

    public List<T> findByIsActiveTrue();
}

@Transactional(propagation = Propagation.MANDATORY)
public interface IAccountRepository extends IBaseRepository<Account, Long> {

    -- mix of queries by method name like:
    public Account findByAccountIdAndIsActiveTrue(Long accountId);

    -- and @Query like:
    @Query(value = "SELECT COALESCE(SUM(a.accountCreditLimit), 0) FROM Account a WHERE a.name = :name")
    public BigDecimal readAccountCreditLimits(@Param("name") String accountName);
}
------ and many more repositories as above

【问题讨论】:

  • 你在persistence.xml中声明了BaseEntity吗?
  • 不在persistence.xml中,而是在app-context.xml中: .... com.model .... AND
  • 因为你没有注释为@Entity 这应该不是问题。那我现在和你一样一无所知;o)

标签: java spring hibernate jpa spring-data-jpa


【解决方案1】:

Spring Data 尝试为您的 BaseRepository 创建一个实现。 这需要带有@Entity、@Id 的完整实体。

可以通过添加注解@NoRepositoryBean来告诉Spring您不需要BaseRepository的实现

这就是我的“BaseRepository”的样子:

import java.io.Serializable;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;

@NoRepositoryBean
public interface IsActiveAwareRepository<T extends AbstractEntity, ID extends Serializable> extends CrudRepository<T, ID>{
    public Iterable<T> findByIsActive(Boolean isActive);
}

【讨论】:

    【解决方案2】:

    经过一些研究和多次尝试,我发现错误出在我的存储库层次结构中。

    所以在改进了我的泛型之后,它现在可以像以前一样工作了。


    发件人:

    @Transactional(propagation = Propagation.MANDATORY)
    public interface IBaseAddressRepository<T extends BaseEntity> extends IBaseRepository<T, Long> {
    }
    

    收件人:

    @Transactional(propagation = Propagation.MANDATORY)
    public interface IBaseAddressRepository<T extends Address> extends IBaseRepository<T, Long> {
    }
    

    有谁知道这是否是预期的行为?尽管尽可能缩小泛型类型要好得多,但只要 Address 扩展 BaseRepository,我的旧存储库也应该可以工作,不是吗?

    【讨论】:

      【解决方案3】:

      我从 1.4.3 升级到休眠 4.3.5 时遇到了完全相同的问题。我的存储库接口扩展了 PagingAndSortingRepository。我在 1.6.0 的 Spring 数据更改日志中找到了以下行:

      将 PagingAndSortingRepository 替换为 JpaRespository 并且似乎工作正常。

      【讨论】:

      • 我的存储库已经扩展了 JpaRepository 和 JpaSpecificationExecutor。我会用该信息更新答案。
      • 好的,我在一些存储库/实体测试中仍然遇到同样的问题,但只有那些在共享我的基本实体和扩展它们的实体的特定包中的问题。我最终将我的基类移动到单独的包中,并确保这些包不包含在实体管理器扫描的包中。这似乎奏效了。希望有帮助
      猜你喜欢
      • 1970-01-01
      • 2023-03-29
      • 2012-08-07
      • 2018-10-04
      • 2013-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多