如下错误 Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set。
今天没事看看新的hibernate-core-4.3.10.Final 看来官方文档。
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www./dtd/hibernate-mapping-3.0.dtd"> <!-- 持久化映射文件(将java对象映射到数据库表) --> <hibernate-mapping> <class name="com.it.hibernate.pojo.Person" table="t_person"> <id name="id"> <generator class="native"></generator> </id> <property name="name" column="t_name" length="12"/> <property name="password" length="6"/> <property name="birthday" /> </class> </hibernate-mapping> 官方获取sessionfactory public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new Configuration().configure().buildSessionFactory( new StandardServiceRegistryBuilder().build() ); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } } hibernate.cfg.xml 如下
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www./dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate_db</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <!-- 数据库方言 加五提高速度 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 展现sql --> <property name="show_sql">true</property> <!-- 格式化sql --> <property name="hibernate.format_sql">true</property> <!-- 自动创建sql create 每次执行都都要先删除表再创建表 update 没有就创建表 有就直接插入 --> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/it/hibernate/pojo/Person.hbm.xml" /> </session-factory> 测试类
public class PersonTest {
public static void main(String[] args) {
SessionFactory factory =HibernateUtil.getSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Person p = new Person();
p.setName("MOMO");
p.setPassword(123456);
p.setBirthday(new Date());
session.save(p);
tx.commit();
session.close();
}
}
如下错误
Initial SessionFactory creation failed.org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
修改获取sessionFactory的公用方法
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// 从 hibernate.cfg.xml 创建SessionFactory
Configuration cfg = new Configuration().configure();
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build();
return cfg.buildSessionFactory(
serviceRegistry);
}
catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
修改完就可以顺利插入数据。 有个疑问是官方给错了?还是我其他地方有问题有问题呢! 但是我改了sessionfactory获取方式就好了呀!求大神指点。 private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new Configuration().configure().buildSessionFactory( new StandardServiceRegistryBuilder().build() ); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } } |
|