简体   繁体   English

com.example.daoImpl.FileDaoImpl中的field entityManagerFactory需要找不到类型为'javax.persistence.EntityManagerFactory'的bean

[英]Field entityManagerFactory in com.example.daoImpl.FileDaoImpl required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found

i am getting the error Field entityManagerFactory in com.example.daoImpl.FileDaoImpl required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found. 我在com.example.daoImpl.FileDaoImpl中收到错误字段fieldentityManagerFactory,需要找不到类型为'javax.persistence.EntityManagerFactory'的bean。

here is my main class 这是我的主班

@SpringBootApplication(scanBasePackages = "com")
@EnableConfigurationProperties({ FileStorageProperties.class })
@ComponentScan({ "com" })
@EntityScan("com.example.model")

@EnableJpaRepositories(basePackages="com", entityManagerFactoryRef="emf")
public class DemoApplication {

    final static Logger logger = Logger.getLogger(DemoApplication.class);

    public static void main(String[] args) {

        logger.info("Application is Started");
        new File(FileUploadController.uploadDirectory).mkdir();
        SpringApplication.run(DemoApplication.class, args);
    }
}

Here is my FileDaoImpl class 这是我的FileDaoImpl类

@Repository
public class FileDaoImpl implements FileDao{

    @Autowired
    EntityManagerFactory entityManagerFactory;

    @Override
    public void encryptKey(EncryptKeys keys) {

        EntityManager em = entityManagerFactory.createEntityManager();
      em.persist(keys);
    }

service class 服务等级

@Service
public class FileStorageService {
 @Autowired
    FileDao filedao;
 public void encryptKey(EncryptKeys encryptKeys) {

         filedao.encryptKey(encryptKeys);
    }

what is wrong with this code. 此代码有什么问题。

try this 尝试这个

@PersistenceContext
private EntityManager entityManager;

and my test case below, it works for me 和下面的测试用例,它对我有用

@Service
@Transactional
public class TestRepo {

 @PersistenceContext
 private EntityManager entityManager;

 public void testPersist(){
    User user = new User();
    user.setUsername("persistTest");
    user.setPassword("testPwd");
    user.setRole("testRole");
    user.setUserId("testId");
    entityManager.persist(user);
 }
}
 @EnableJpaRepositories(basePackages="com", entityManagerFactoryRef="emf")

It trying to find bean "emf" for use it as entityManagerFactoryRef, i don't know mb you declare this bean somewhere else, but if not, do it 它试图找到将其用作entityManagerFactoryRef的bean“ emf”,我不知道mb您是否在其他地方声明了此bean,但是如果没有,请这样做

@Bean(name = "emf")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean em = builder
            .dataSource(dataSource)
            .packages("com")
            .persistenceUnit("someNameForUnit")
            .build();

   /* HashMap<String, Object> properties = new HashMap<>();
    properties.put("hibernate.hbm2ddl.auto", hbm2ddl);
    properties.put("hibernate.dialect", hibernateDialog);
    properties.put("hibernate.show_sql", showSql);
    properties.put("hibernate.format_sql", formatSql);
    em.setJpaPropertyMap(properties); */ this can set some property of jpa 
    return em;
}

But i think best solution let spring boot do this job as mentioned above. 但是我认为最好的解决方案是让Spring Boot像上面提到的那样做这项工作。

Please make changes as following and try 请进行以下更改并尝试

@SpringBootApplication(scanBasePackages = "com")
@EnableConfigurationProperties({ FileStorageProperties.class })
//@ComponentScan({ "com" }) not needed since SpringBootApplication will fulfill this
@EntityScan("com.example.model")

@EnableJpaRepositories("com") // allow spring-boot-starter-data-jpa to do the needful
public class DemoApplication {

    final static Logger logger = Logger.getLogger(DemoApplication.class);

    public static void main(String[] args) {

        logger.info("Application is Started");
        new File(FileUploadController.uploadDirectory).mkdir();
        SpringApplication.run(DemoApplication.class, args);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个组件需要一个无法找到的类型为“javax.persistence.EntityManagerFactory”的 bean - A component required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found Spring JPA(Hibernate)没有类型的限定bean:javax.persistence.EntityManagerFactory - Spring JPA (Hibernate) No qualifying bean of type: javax.persistence.EntityManagerFactory MultitenantConfiguration:未定义类型为[javax.persistence.EntityManagerFactory]的合格Bean:预期的单个匹配Bean,但找到2 - MultitenantConfiguration: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2 没有定义类型为[javax.persistence.EntityManagerFactory]的合格Bean ::期望单个匹配的Bean,但找到2 - No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined:: expected single matching bean but found 2 错误:没有定义类型为[javax.persistence.EntityManagerFactory]的合格bean:期望的单个匹配bean,但找到2 - error No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2 考虑在配置中定义类型为&#39;javax.persistence.EntityManagerFactory&#39;的bean - Consider defining a bean of type 'javax.persistence.EntityManagerFactory' in your configuration 没有可用于OSGi应用程序的&#39;javax.persistence.EntityManagerFactory&#39;类型的合格Bean - No qualifying bean of type 'javax.persistence.EntityManagerFactory' available for OSGi Application 没有定义类型为[javax.persistence.EntityManagerFactory]的合格Bean。“}} - No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined"}} 使用Spring 4未定义类型为[javax.persistence.EntityManagerFactory]的合格Bean - No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined Using Spring 4 NoSuchBeanDefinitionException:没有可用的“javax.persistence.EntityManagerFactory”类型的合格 bean - NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM