通过 Spring Data JPA 自动生成 RepositoryImpl 时“无法解析匹配的构造函数”

2024-02-18

我正在使用 Spring Data Jpa 自动生成一个类来实现我的存储库。

但是当我尝试启动容器时它告诉我Error creating bean with name 'accountRepository': Could not resolve matching constructor.

My Account entity:

package com.amastigote.ssp.model;

import javax.persistence.*;

/**
 * Created by hwding on 3/22/17.
 */
@Entity
@Table(name = "User")
public class Account {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String nickName;
    private String plainPassword;

    public Account() {
    }

    public Account(String nickName, String plainPassword) {
        this.nickName = nickName;
        this.plainPassword = plainPassword;
    }

    public String getNickName() {
        return nickName;
    }

    public String getPlainPassword() {
        return plainPassword;
    }

    public long getId() {
        return id;
    }

}

存储库接口Account:

package com.amastigote.ssp.repo;

import com.amastigote.ssp.model.Account;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * Created by hwding on 3/22/17.
 */
@SuppressWarnings("unchecked")
public interface AccountRepository extends JpaRepository<Account, Long> {
}

Jpa配置:

package com.amastigote.ssp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

/**
 * Created by hwding on 3/23/17.
 */
@Configuration
@EnableJpaRepositories("com.amastigote.ssp.repo")
public class JPAConf {

    @Bean
    public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean(
            DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
        LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean
                = new LocalContainerEntityManagerFactoryBean();

        localContainerEntityManagerFactoryBean
                .setDataSource(dataSource);
        localContainerEntityManagerFactoryBean
                .setJpaVendorAdapter(jpaVendorAdapter);
        localContainerEntityManagerFactoryBean
                .setPackagesToScan("com.amastigote.ssp.model");

        return localContainerEntityManagerFactoryBean;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter
                = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setDatabase(Database.H2);
        hibernateJpaVendorAdapter.setShowSql(true);
        hibernateJpaVendorAdapter.setGenerateDdl(false);

        return hibernateJpaVendorAdapter;
    }

    @Bean
    public JpaTransactionManager jpaTransactionManager(EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

事实上,这个错误消息是不明确的,因为我使用的是低版本的spring-data-jpa(这是我的 IDE 推荐的...)。

将最新版本添加到我的项目后,给出了更具体的提示:

error when creating inner bean '...', 
could not found bean named 'entityManagerFactoryBean'.

我注意到我的实体工厂 bean 的配置如下:

@Bean
public LocalContainerEntityManagerFactoryBean 
    localContainerEntityManagerFactoryBean(...) {}

默认情况下,其 bean 名称为“localContainerEntityManagerFactoryBean”,与要求的不匹配entityManagerFactoryBean.

解决方案是添加entityManagerFactoryRef = "localContainerEntityManagerFactoryBean"给你的@EnableJpaRepositories, like:

@EnableJpaRepositories(
    basePackages = "com.amastigote.ssp.repo",
    entityManagerFactoryRef = "localContainerEntityManagerFactoryBean"
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过 Spring Data JPA 自动生成 RepositoryImpl 时“无法解析匹配的构造函数” 的相关文章

随机推荐