您的位置:首页 > 编程语言 > Java开发

Hibernate4.3.x Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

2015-06-24 21:54 531 查看
          如下错误                Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set。                        今天没事看看新的hibernate-core-4.3.10.Final   看来官方文档。                      
public class Person {   public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getPassword() {return password;}public void setPassword(int password) {this.password = password;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}   private Integer id;   private String name;   private int password;   private Date birthday;}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/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.hibernate.org/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>
</hibernate-configuration>
测试类
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 setException in thread "main" java.lang.ExceptionInInitializerErrorat com.it.hibernate.utils.HibernateUtil2.buildSessionFactory(HibernateUtil2.java:21)at com.it.hibernate.utils.HibernateUtil2.<clinit>(HibernateUtil2.java:9)at com.it.hibernate.pojo.test.PersonTest.main(PersonTest.java:14)Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not setat org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104)at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209)at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)at com.it.hibernate.utils.HibernateUtil2.buildSessionFactory(HibernateUtil2.java:16)... 2 more
修改获取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.xmlreturn new Configuration().configure().buildSessionFactory(new StandardServiceRegistryBuilder().build() );}catch (Throwable ex) {// Make sure you log the exception, as it might be swallowedSystem.err.println("Initial SessionFactory creation failed." + ex);throw new ExceptionInInitializerError(ex);}}public static SessionFactory getSessionFactory() {return sessionFactory;}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java hibernate