尝试启动 Tomcat 时出现休眠异常

Posted

技术标签:

【中文标题】尝试启动 Tomcat 时出现休眠异常【英文标题】:Hibernate exception when try to launch Tomcat 【发布时间】:2019-03-02 06:32:04 【问题描述】:

我在 spring-mvc+jpa 上有应用程序。我建立战争并尝试在 tomcat 上开始。

数据配置:

import org.hibernate.ejb.HibernatePersistence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
@PropertySource("classpath:app.properties")
public class DataConfig 

    private static final String PROP_DATABASE_DRIVER = "db.driver";
    private static final String PROP_DATABASE_URL = "db.url";
    private static final String PROP_DATABASE_USERNAME = "db.username";
    private static final String PROP_HIBERNATE_DIALECT = "db.hibernate.dialect";
    private static final String PROP_HIBERNATE_SHOW_SQL = "db.hibernate.show_sql";
    private static final String PROP_ENTITYMANAGER_PACKAGES_TO_SCAN = "db.entitymanager.packages.to.scan";
    private static final String PROP_HIBERNATE_HBM2DDL_AUTO = "db.hibernate.hbm2ddl.auto";

    @Resource
    private Environment env;

    @Bean
    public DataSource dataSource() 
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(env.getRequiredProperty(PROP_DATABASE_DRIVER));
        dataSource.setUrl(env.getRequiredProperty(PROP_DATABASE_URL));
        dataSource.setUsername(env.getRequiredProperty(PROP_DATABASE_USERNAME));
        dataSource.setPassword(env.getRequiredProperty("db.password"));

        return dataSource;
    

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() 
        LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
        entityManager.setDataSource(dataSource());
        entityManager.setPackagesToScan(env.getRequiredProperty(PROP_ENTITYMANAGER_PACKAGES_TO_SCAN));
        entityManager.setPersistenceProviderClass(HibernatePersistence.class);
        entityManager.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        entityManager.setJpaProperties(getHibernateProperties());
        return entityManager;

    

    @Bean
    public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) 
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    

    private Properties getHibernateProperties() 
        Properties properties = new Properties();
        properties.put(PROP_HIBERNATE_DIALECT, env.getRequiredProperty(PROP_HIBERNATE_DIALECT));
        properties.put(PROP_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROP_HIBERNATE_SHOW_SQL));
        properties.put(PROP_HIBERNATE_HBM2DDL_AUTO, env.getRequiredProperty(PROP_HIBERNATE_HBM2DDL_AUTO));

        return properties;
    


app.properties 是:

#DB properties:
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/mydb
db.username=root
db.password=111111

#Hibernate Configuration:

db.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
db.hibernate.show_sql=true
db.entitymanager.packages.to.scan=ru.sbrf.risks.services.data.model
db.hibernate.hbm2ddl.auto = create-drop

所以,tomcat 日志中有错误消息:

引起:javax.persistence.PersistenceException:[PersistenceUnit: default] 无法在以下位置构建 EntityManagerFactory org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:924) 在 org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899) 在 org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:76) 在 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:287) 在 org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) ... 57 更多原因:org.hibernate.HibernateException:连接 当“hibernate.dialect”未设置为时不能为空 org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97) 在 org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67) 在 org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:170) 在 org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) 在 org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) 在 org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) 在 org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1818) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776) 在 org.hibernate.ejb.EntityManagerFactoryImpl.(EntityManagerFactoryImpl.java:96) 在 org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914) ... 63 更多

【问题讨论】:

【参考方案1】:

Hibernate 不理解您传递给它的属性。您已将 hibernate.dialect 定义为 db.hibernate.dialect 删除 db 部分并将其设置为 hibernate.dialect 并对您的其他休眠相关属性执行相同操作。

https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/session-configuration.html

【讨论】:

我的其他所有属性也必须不带前缀db? 仅在涉及键值映射时。您的非休眠属性只是通过关联的键(例如驱动程序、用户名、url 等)从您的环境中检索值,但是您的休眠时您正在指定要设置的键和值,这是它有问题的地方。【参考方案2】:
@PropertySource("classpath:app.properties") 

这个注释值替换为下面的传递值

@PropertySource(value = "classpath:application.properties","file:config/application.properties", ignoreResourceNotFound = true)

并重建你的项目。现在它工作正常。

【讨论】:

请解释您的答案为什么这可以解决问题。 file:config/application.properties 应该是外部 application.properties 文件的位置(文件的默认位置)。有时 IDE 找不到 application.properties 的路径。那个时候也看到同样的错误.

以上是关于尝试启动 Tomcat 时出现休眠异常的主要内容,如果未能解决你的问题,请参考以下文章

定义映射时出现休眠异常

在休眠中使用@Column 注释时出现异常

保存我的对象时出现休眠异常

在 Linux 下运行时出现奇怪的休眠异常

选择值时出现休眠错误

无法反序列化 - 休眠弹簧启动