简体   繁体   中英

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.

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

@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

@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.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM