当前位置: 首页 > 知识库问答 >
问题:

SpringBoot找不到EntityManagerFactory,即使它已声明

强烨
2023-03-14

我有这个SpringBoot的代码。它加载mybatis和冬眠。我得到一个异常类型EntityManagerFactory不可用,但它是在这里定义。记录输出包含'xxx'全部打印。

package com.ritchey.timesheet;


import java.sql.SQLException;
import java.util.Arrays;
import java.util.Properties;

import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.hibernate.SessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jndi.JndiTemplate;
import org.springframework.orm.hibernate5.HibernateExceptionTranslator;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "org.browsexml.timesheetjob")
@ComponentScan(basePackages = "org.browsexml.timesheetjob")
@MapperScan(basePackages = "com.ritchey.timesheet.mapper.TimeSheet", sqlSessionTemplateRef="TimeSheetSqlSessionTemplate")
@MapperScan(basePackages = "com.ritchey.timesheet.mapper.powercampus", sqlSessionTemplateRef="powercampusSqlSessionTemplate")
@SpringBootApplication()
public class Timesheet  {
    private static final Logger LOGGER = LoggerFactory.getLogger(Timesheet.class);
    
    static public String version = "77777";

        
    public static void main(String[] args) throws NamingException {
        SpringApplication application = new SpringApplication(Timesheet.class);

        LOGGER.info("RUN TIMESHEET ");
        ApplicationContext ctx = application.run();
        LOGGER.info("messages TIMESHEET");
        String basename = ctx.getEnvironment().getProperty("spring.messages.basename", "messages");
        LOGGER.info("basename = " + basename);
        LOGGER.info("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            LOGGER.info(beanName);
        }
        
    }
    
    @Bean
    public DataSource getCampusDatasource() throws NamingException {
        JndiTemplate jndiTemplate = new JndiTemplate();
        DataSource ds = null;
        ds = (DataSource) jndiTemplate.lookup("java:comp/env/jdbc/powerCampus");
        return ds;
    }

    
    @Bean
    @Primary
    public DataSource getTimeSheetDatasource() throws NamingException {
        JndiTemplate jndiTemplate = new JndiTemplate();
        DataSource ds = null;
        ds = (DataSource) jndiTemplate.lookup("java:comp/env/jdbc/TimeSheet");
        return ds;
    }
    
    @Bean
    public SqlSessionFactory TimeSheetSqlSessionFactory() throws Exception {
      SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
      factoryBean.setDataSource(getTimeSheetDatasource());
      return factoryBean.getObject();
    }
    
    @Bean
    @Primary
    public SqlSessionFactory powercampusSqlSessionFactory() throws Exception {
      SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
      factoryBean.setDataSource(getCampusDatasource());
      return factoryBean.getObject();
    }
    
    @Bean
    @Primary
    public SqlSessionTemplate TimeSheetSqlSessionTemplate(@Qualifier("TimeSheetSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    
    @Bean
    @Primary
    public SqlSessionTemplate powercampusSqlSessionTemplate(@Qualifier("powercampusSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    
    
    @Bean
    @Primary
    public SessionFactory sessionFactory() throws Exception {
        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
      factoryBean.setDataSource(getTimeSheetDatasource());
      factoryBean.setMappingResources("org/browsexml/timesheetjob/model/Awards.hbm.xml",
              "org/browsexml/timesheetjob/model/HoursWorked.hbm.xml",
              "org/browsexml/timesheetjob/model/Job.hbm.xml",
              "org/browsexml/timesheetjob/model/PeopleProperties.hbm.xml",
              "org/browsexml/timesheetjob/model/CodeTerm.hbm.xml",
              "org/browsexml/timesheetjob/model/Holiday.hbm.xml",
              "org/browsexml/timesheetjob/model/PayPer.hbm.xml",
              "org/browsexml/timesheetjob/model/Awarded.hbm.xml",
              "org/browsexml/timesheetjob/model/Agreements.hbm.xml",
              "org/browsexml/timesheetjob/model/Timesheets.hbm.xml",
              "org/browsexml/timesheetjob/model/FulltimeAgreements.hbm.xml",
              "org/browsexml/timesheetjob/model/Supervisor.hbm.xml"
              );
      factoryBean.setAnnotatedClasses(org.browsexml.timesheetjob.model.WorkCode.class
              , org.browsexml.timesheetjob.model.ProcessErrors.class
              , org.browsexml.timesheetjob.model.UserLog.class
              , org.browsexml.timesheetjob.model.Properties.class
              , org.browsexml.timesheetjob.model.AuditTrail.class
              , org.browsexml.timesheetjob.model.PositionCode.class
              , org.browsexml.timesheetjob.model.Position.class
              , org.browsexml.timesheetjob.model.CodeRegions.class);
      
      Properties p = new Properties();
     
      p.setProperty("hibernate.dialect", "org.browsexml.dialect.SQLServerDialect");
      p.setProperty("hibernate.hbm2ddl.auto", "validate");
      p.setProperty("hibernate.query.substitutions", "true 1; false 0; yes 'Y'; no 'N'; year dbo.year");
      p.setProperty("hibernate.show_sql", "true");

      factoryBean.setHibernateProperties(p);
      return factoryBean.getObject();
    }
    
    @Bean
    public SessionFactory pcSessionFactory() throws Exception {
        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
      factoryBean.setDataSource(getCampusDatasource());
      factoryBean.setMappingResources(
              "org/browsexml/timesheetjob/model/PeopleBasic.hbm.xml",
              "org/browsexml/timesheetjob/model/Address.hbm.xml"
              );
      
      Properties p = new Properties();
         
      p.setProperty("hibernate.dialect", "org.browsexml.dialect.SQLServerDialect");
      p.setProperty("hibernate.hbm2ddl.auto", "validate");
      p.setProperty("hibernate.query.substitutions", "true 1; false 0; yes 'Y'; no 'N'; year dbo.year");
      p.setProperty("hibernate.show_sql", "true");

      factoryBean.setHibernateProperties(p);
      
      return factoryBean.getObject();
    }
    
    @Bean(name="entityManagerFactory")
    public EntityManagerFactory entityManagerFactory() throws SQLException, NamingException {
        LOGGER.debug("xxx entity manager factory");
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setShowSql(true);
        vendorAdapter.setGenerateDdl(true);
        
        HibernateJpaDialect jpd = new HibernateJpaDialect();

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setJpaDialect(jpd);
        factory.setPackagesToScan("org.browsexml.timesheetjob.model");
        factory.setDataSource(getTimeSheetDatasource());
        factory.afterPropertiesSet();

        EntityManagerFactory ret = factory.getObject();
        LOGGER.debug("xxx entity manager factory  " + ret);
        return ret;
    }

    @Bean
    public EntityManager entityManager(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
        LOGGER.debug("xxx entity manager");
        EntityManager ret = entityManagerFactory.createEntityManager();
        LOGGER.debug("xxx entity manager  " + ret);
        return ret;
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws SQLException, NamingException {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }

    @Bean
    public HibernateExceptionTranslator hibernateExceptionTranslator() {
        return new HibernateExceptionTranslator();
    }
    
}

在SpringBoot中,我遇到了以下异常:

共有1个答案

胡锋
2023-03-14

通过设置application.properties绕过此错误:

 spring.jpa.open-in-view=false

不知道这里发生了什么。

 类似资料:
  • 我还没有在网上找到解决方案。我的所有活动都是声明的,但Android Studio经常会告诉我它找不到它们。它并不总是发生,而且似乎是随机发生的。我已经清理、无效并重新启动、重建,有时这会得到修复,然后在一段时间后再次发生。我在这个项目的团队中工作,我似乎是唯一一个有bug的人: 这是我的毕业生

  • 问题内容: 我想用主方法创建一个简单的Java类,但是在编译代码时,出现以下错误消息: 错误:在类error.TestErrors中找不到主方法,请将主方法定义为:public static void main(String [] args) 这是源代码: 为什么我看到此错误,如您所见,我已经声明了main方法! 问题答案: 正如我在评论中所说,您似乎已经在自己的类中声明了一个类。为了证明这一点,

  • 这是我用来使用铬Web驱动程序和python查找元素的代码行 这是每次尝试运行代码时抛出的错误 这些是标题栏上的按钮,我试图让我的代码单击html按钮 这是努力按钮的html代码,我试图使用selenium来定位它,以便它可以点击这里,但它总是说没有找到这样的元素 我甚至创建了一个单独的函数来一次又一次地找到元素并在每次成功尝试后Hibernate5秒,直到它点击了元素,但仍然没有运气它永远运行而

  • 我正在开发Spring Boot应用程序,在启动服务器时遇到了这个错误。我不确定是否错误地定义了任何注释或缺少任何依赖项。任何帮助都将不胜感激。 主要类: UserService类: UserDAO类: @repository公共类UserDAO实现IUserDAO{ Build.gradle: 错误消息: 我看到了所有相关的答案,并尝试实现这些建议,如添加依赖项、在主类中添加符号等。但它显示了相

  • 我有一个使用JPA、Web和PostgreSQL的简单Spring Boot项目。我使用的是最新的Spring Boot版本2.1.3。