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

时间:2017-03-23 06:02:18

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

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

但是当我试图启动容器时,它告诉我NumberFormat numberFormat = NumberFormat.getIntegerInstance(); numberFormat.setMaximumFractionDigits(2); numberFormat.format(x);

我的Error creating bean with name 'accountRepository': Could not resolve matching constructor实体:

Account

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

Jpa配置:

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> {
}

1 个答案:

答案 0 :(得分:1)

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

在我的项目中添加最新版本后,会给出更具体的提示:

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

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

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

默认情况下,它的bean名称是&#39; localContainerEntityManagerFactoryBean&#39;,它与所需的entityManagerFactoryBean不匹配。

解决方案是将entityManagerFactoryRef = "localContainerEntityManagerFactoryBean"添加到@EnableJpaRepositories,例如:

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