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

时间:2014-05-23 10:43:39

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

启动时从1.4.3升级到1.5+或1.6.0时出错; 我正在使用Hibernate 4.3.5

例外是: org.springframework.beans.factory.BeanCreationException:创建名称为' IAccountRepository'的init时出错:init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:此类[类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

3 个答案:

答案 0 :(得分:0)

我遇到了与使用hibernate 4.3.5从1.4.3升级相同的问题。我的存储库接口扩展了PagingAndSortingRepository。我发现Spring数据更改日志中的以下行为1.6.0:

用JpaRespository替换了PagingAndSortingRepository,似乎工作正常。

答案 1 :(得分:0)

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

因此,在改进我的仿制药之后,它就像以前一样工作。


自:

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

答案 2 :(得分:0)

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