gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:31:51 26 4
gpt4 key购买 nike

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

但是当我尝试启动容器时,它告诉我创建名为“accountRepository”的 bean 时出错:无法解析匹配的构造函数

我的帐户实体:

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 的存储库接口(interface):

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,例如:

@EnableJpaRepositories(
basePackages = "com.amastigote.ssp.repo",
entityManagerFactoryRef = "localContainerEntityManagerFactoryBean"
)

关于java - 通过 Spring Data JPA 自动生成 RepositoryImpl 时“无法解析匹配的构造函数”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42968346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com