我在从版本 1.4.3 升级到 1.5+ 或 1.6.0 时启动时出错; 我使用的是 Hibernate 4.3.5
异常(exception)情况是: 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 相同,但将父类(super class)视为一个实体。
您知道这是我的错误还是错误以及任何解决方法吗?
非常感谢。
编辑
我的存储库如下:
@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
最佳答案
我从 1.4.3 升级到 hibernate 4.3.5 时遇到了完全相同的问题。我的存储库接口(interface)扩展了 PagingAndSortingRepository。我在 1.6.0 的 Spring 数据更改日志中找到了以下行:
- 无法对 @IdClass 实体使用分页(Pageable)(spring-data-jpa 1.4.3 和 Hibernate 4.1.9)。 (DATAJPA-472) (来源:http://docs.spring.io/spring-data/jpa/docs/1.6.0.RELEASE/changelog.txt)
将 PagingAndSortingRepository 替换为 JpaRespository,似乎工作正常。
关于java - 在 MappedSuperclass 上更新 Spring-data-jpa 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23827144/