【Hibernate】Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect‘ not set
程序员文章站
2022-04-13 14:09:20
...
今天用hibernate框架写crm项目时遇到报错:
Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
说是hibernate的dialect没有设置,但是在hibernate.cfg,xml中我已经配置了。主要内容如下:
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 必选属性 (5个)-->
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库url -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<!-- 数据库用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库密码 -->
<property name="hibernate.connection.password"></property>
<!-- 数据库方言 : 不同的数据库中,sql语法略有区别 ,指定方言可以让hibernate框架在生成sql语句时,根据 数据库方言生成。 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 可选属性(3个) -->
<!-- 将hibernate生成的sql语句打印到控制台 -->
<property name="hibernate.show_sql">true</property>
<!-- 将hibernate生成的sql语句格式化 -->
<property name="hibernate.format_sql">true</property>
<!--## auto schema export 自动导出表,构建表
#hibernate.hbm2ddl.auto create(每次框架运行完之后都会创建新表,之前的表会被覆盖。)
#hibernate.hbm2ddl.auto create-drop (每次框架运行完之后都会将所有表删除)
#hibernate.hbm2ddl.auto update (如果有表变动,会自动更新有改变的表)
#hibernate.hbm2ddl.auto validate(校验 )不自动生成表-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 引入orm元数据 -->
<mapping resource="com/ysong/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Customer.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- package:填写一个包名,需要书写完整类名的属性,可以写简答类名 -->
<hibernate-mapping package="com.ysong.domain">
<!-- class元素:配置实体与表的关系 name:完整类名 table:数据库表名 -->
<class name="Customer" table="Customer">
<!-- id:配置主键列名映射属性 name:填写主键对应属性名 column:表中的主键列名 (不建议填,让hibernate自动指定类型)type(可选):填写列(属性)的类型。hibernate会自动检测实体的属性类型
每个类型有三种填法:java类型(java.long.String)|hibernate类型(String)|数据库类型(varchar) lenth(可选):配置数据库中列的长度,默认值
:数据库类型的最大长度 not-null(可选):配置该属性(列)是否不能为空,默认值:false -->
<id name="cust_id" column="cust_id">
<!-- generator:主键生成策略 -->
<generator class="native"></generator>
</id>
<!-- property:除id之外的列名 name:填写属性名 column:表中的除主键以外的列名 (不建议填,让hibernate自动指定类型)type(可选):填写列(属性)的类型。hibernate会自动检测实体的属性类型
每个类型有三种填法:java类型(java.long.String)|hibernate类型(String)|数据库类型(varchar) lenth(可选):配置数据库中列的长度,默认值
:数据库类型的最大长度 not-null(可选):配置该属性(列)是否不能为空,默认值:false -->
<property name="cust_name" column="cust_name"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
</class>
</hibernate-mapping>
Test
import com.newer.xxh.People;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class Test {
public static void main(String[] args) {
Configuration configuration = new Configuration();
Session session = configuration.buildSessionFactory().openSession();
People people = new People();
people.setName("张三");
people.setMoney(100.00);
session.save(people);
session.beginTransaction().commit();
session.close();
}
}
最后从百度到的一篇文章中找到答案:
原来,这里的new configuration();应该改成new Configuration().configure();
没有configure()就会去classpath找hibernate.properties文件,有configure就去找hibernate.cfg.xml文件。
import com.newer.xxh.People;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class Test {
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
Session session = configuration.buildSessionFactory().openSession();
People people = new People();
people.setName("张三");
people.setMoney(100.00);
session.save(people);
session.beginTransaction().commit();
session.close();
}
}
上一篇: js优化阿里云图片加载(二)
下一篇: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not Set
推荐阅读
-
Springboot启动出错:Access to DialectResolutionInfo cannot be null when hibernate.dialect not set
-
Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
-
【Hibernate】Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect‘ not set
-
Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not Set
-
Connection cannot be null when 'hibernate.dialect' not set
-
springboot启动失败:Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set
-
springboot启动出现Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set
-
Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 问题的解决方法
-
ExceptionInInitialize Access to DialectResolutionInfo cannot be null when 'hibernate.dialect'not set
-
Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set