在MappedSuperclass上更新Spring-data-jpa出错



从1.4.3版本升级到1.5+或1.6.0版本启动时出现错误;我使用Hibernate 4.3.5

例外是:org.springframework.beans.factory.BeanCreationException:创建名为'IAccountRepository'的bean时出错:init方法调用失败;嵌套异常是java.lang.IllegalArgumentException:这个类[class com.model.entities]。没有定义一个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相同的方式检查层次结构,但将超类视为实体。

你知道这是我的错误还是一个bug,还有什么解决办法吗?

许多谢谢。

编辑

我的存储库如下:

@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。我的存储库接口扩展了PagingAndSortingRepository。我在1.6.0的Spring数据更改日志中发现了下面一行:

  • 不能使用分页(Pageable)与@IdClass实体(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替换为jparerepository,似乎工作得很好。

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

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


来自:

@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,我的旧存储库也应该可以工作,不是吗?

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);
}

最新更新