当前位置: 首页 > 知识库问答 >
问题:

自定义JpaRepository未找到属性保存实体

高峻
2023-03-14

您需要创建一个通用的JpaRepository,以便处理系统进行的所有事务。在这里遵循这个示例。

它与实现有点不同,因为我的目标不是执行搜索,而是操作save方法。

@NoRepositoryBean
public interface GenericRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
    <S extends T> S saveEntity(S entity);
}
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;
    }
}
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);
    }
}
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;
}

unsatisfiedDependencyException:创建名为“sistema menuservice”的bean时出错:通过字段“sistema menurepository”表示未满足的依赖关系;嵌套异常是org.springframework.beans.factory.beanCreationException:创建名为'sistema menurepository'的bean时出错:调用init方法失败;嵌套异常是org.springframework.data.mapping.PropertyReferenceException:没有找到类型sistemamenu的属性保存实体!

欢迎任何帮助!

共有1个答案

吕高雅
2023-03-14

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

@Configuration
@EnableJpaRepositories( basePackages = "repository.packagename",
                        entityManagerFactoryRef = "entityManagerFactory",
                        transactionManagerRef = "transactionManager",
                        repositoryFactoryBeanClass = GenericRepositoryFactoryBean.class)
@EnableTransactionManagement
public class JpaConfiguration {
...
}
 类似资料:
  • 我已经为Word创建了一个插件。我正在尝试通过单击按钮更新word文档中的自定义属性的值。但却得不到拯救。我写的代码是: 但如果我在文档中添加一个空格然后保存它。然后保存自定义属性的值。代码为: 为什么行为是这样的。我不想在我的文档中添加任何额外的空白处。请帮帮我。提前道谢。

  • 我正在遵循Spring手册为我的所有子存储库创建一个共享存储库,以在查询中提供更多功能。 然而,我得到的是“没有发现任何属性异常”基本上,我遵循步骤1.3.2,从该链接向所有存储库添加自定义行为http://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html 我试图更

  • 试图向OpenLDAP添加一个新属性,但总是碰壁。我正在尝试向架构添加ipPhone属性,因为我不能在默认的telephoneNumber属性中包含*数字。 下面是我的LDIF文件,用于创建新属性并将其与objectClass类似。 我已经测试和谷歌了几个小时,但一直无法解决这个问题或找出我错过了什么!

  • 我从一个xml模式生成java类,对于一个复杂类型,我希望jaxb使用一个现有的类,我有一个外部绑定定制文件。自定义类被解组为正确的,除了该类型的单个属性,该属性从未在java类中填充。 下面是类型/类问题的演示。 模式中定义的内容是: 读取匹配xml文件的代码段是: 在这个xml中阅读: 使用JAXB生成的Thing类(不使用自定义xjb),输出符合预期: 使用只有getters的自定义Thin

  • 问题内容: 我正在借助以下方法设置新的React:https : //github.com/facebookincubator/create-react- app 但是,我遇到了一个棉绒问题。我收到以下掉毛错误。 这是导致问题的代码: 我试着使用react / prop-types规则,但无济于事。 问题答案: 根据这个问题发表评论。 这似乎是因为您在安装时仅应使用create-react- ap

  • 问题内容: 有谁知道为什么存储属性文件时冒号会转义? 我正在这样做: 并使用存储: 它正在工作,但是输出由于某些原因冒号已转义: 有人知道解决方法吗? 问题答案: 在属性文件中,这两个都是合法的: 因此=和:必须都转义。 现在,如果您用Properties读回了东西,那没问题。否则,您将必须编写自定义代码