自定义JpaRepository没有找到属性saveEntity

时间:2018-01-18 22:50:05

标签: spring-boot spring-data-jpa

您需要创建一个通用的JpaRepository,以便处理系统所做的所有事务。请遵循此示例here

它与实现有点不同,因为我的目标不是进行搜索而是操纵保存方法。

Inteface GenericRepository:

@NoRepositoryBean
public interface GenericRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
    <S extends T> S saveEntity(S entity);
}

Class GenericRepositoryFactory:

public class GenericRepositoryFactory extends JpaRepositoryFactory {

    /**
     * Creates a new {@link org.springframework.data.jpa.repository.support.JpaRepositoryFactory}.
     *
     * @param entityManager must not be {@literal null}
     */
    public GenericRepositoryFactory(EntityManager entityManager) {
        super(entityManager);
    }

    @Override
    protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
        return GenericRepositoryImpl.class;
    }
}

Class GenericRepositoryFactoryBean:

public class GenericRepositoryFactoryBean<T extends JpaRepository<S, ID>, S, ID extends Serializable>
    extends JpaRepositoryFactoryBean<T, S, ID> {

    /**
     * Creates a new {@link JpaRepositoryFactoryBean} for the given repository interface.
     *
     * @param repositoryInterface must not be {@literal null}.
     */
    public GenericRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
        super(repositoryInterface);
    }

    /**
     * Returns a {@link org.springframework.data.repository.core.support.RepositoryFactorySupport}.
     *
     * @param entityManager
     * @return
     */
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
        return new GenericRepositoryFactory(entityManager);
    }
}

Class GenericRepositoryImpl:

public class GenericRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements GenericRepository<T, ID> {

    private JpaEntityInformation<T, Serializable> entityInformation;
    private final EntityManager entityManager;

    public GenericRepositoryImpl(JpaEntityInformation<T, Serializable> entityInformation, EntityManager entityManager) {
        super(entityInformation, entityManager);
        this.entityInformation = entityInformation;
        this.entityManager = entityManager;
    }


    @Override
    public T saveEntity(Object entity) {
        //here custom code...
        return null;
    }
}

使用我的通用存储库我的界面:

public interface SistemaMenuRepository extends GenericRepository<SistemaMenu, Integer> {
}

我的服务:

@Service
public class SistemaMenuService {
    @Autowired
    SistemaMenuRepository sistemaMenuRepository;
}

生成以下错误以运行应用程序:

  

org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名为'sistemaMenuService'的bean时出错:不满意   通过字段'sistemaMenuRepository'表示的依赖关系;嵌套   异常是org.springframework.beans.factory.BeanCreationException:   创建名为'sistemaMenuRepository'的bean时出错:调用   init方法失败;嵌套异常是   org.springframework.data.mapping.PropertyReferenceException:没有   找到类型SistemaMenu的属性saveEntity!

欢迎任何帮助!

1 个答案:

答案 0 :(得分:0)

根据Jens的建议,您需要指定自定义存储库工厂bean类。

@Configuration
@EnableJpaRepositories( basePackages = "repository.packagename",
                        entityManagerFactoryRef = "entityManagerFactory",
                        transactionManagerRef = "transactionManager",
                        repositoryFactoryBeanClass = GenericRepositoryFactoryBean.class)
@EnableTransactionManagement
public class JpaConfiguration {
...
}