Hibernate 错误和异常,无法生成hbm.xml、SessionFactory报空....

来源:互联网 发布:sql查询语句表别名 编辑:程序博客网 时间:2024/06/06 00:25

Hibernate 测试连接数据库时,报 transaction 空指针异常 等错误。

org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null   when 'hibernate.dialect' not set..Configured SessionFactory: null

这是因为在hibernate4中已经把原有的SessionFactory sessions = cfg.buildSessionFactory(); 这种方法标记为过时的了。
在hibernate4中,构建 SessionFactory 需要加入参数ServiceRegistry。

原来的代码:

/*错误的配置,一开始有效*/         //创建配置对象        Configuration config = new Configuration().configure();        //创建服务注册对象        StandardServiceRegistry standardRegistry = new                 StandardServiceRegistryBuilder().configure().build();        //创建会话工厂对象        sessionFactory =config.buildSessionFactory(standardRegistry);

修改的代码:

    private SessionFactory sessionFactory;    private Session session;    private Transaction transaction;    .    .        //创建配置对象        Configuration cfg = new Configuration().configure();         //创建服务注册对象        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()                  .applySettings(cfg.getProperties()).build();        //创建会话工厂对象        sessionFactory = cfg.buildSessionFactory(serviceRegistry);

头信息版本问题无法生成关系映射文件 hbm.xml文件

JBoss Tools 4.4.1 Final
Eclipse Neon.1a Release (4.6.1)
Hibernate 版本是4.3.11

在我的测试中,如果你的Eclipse还没有自动生成过hibernate.cfg.xml,就不能通过JBoss工具自动生成hbm.xml文件,而且hibernate.cfg.xml头信息只能写3.0的版本,我新装的Eclipse就是这样


错误:

Unable to create requested service [org.hibernate.engine.spi.CacheImple..
Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEn

注意hibernate.cfg.xml文件前缀,

3.0,这个版本可以通过jboss自动生成 hbm.xml,4.0不行,反正,cfg.xml,自动生成更好

<!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

4.0

<hibernate-configuration        xmlns="http://www.hibernate.org/xsd/hibernate-configuration"        xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

NoClassDefFoundError: com/mchange/v2/ser/Indirector

 Failed to instantiate [com.mchange.v2.c3p0.ComboPooledDataSource]: No default constructor found; nested exception is java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector

没有引入mchange-commons-java-0.2.3.4.jar这个包

这里写图片描述

HQL语句异常

node to traverse cannot be null

from 实体类名 where ...

from 要写对,from后不能跟表名,而是实体类的类名,否则会说不能maping什么的

$$_javassist_0 cannot be cast to javassist.util.proxy.Proxy

 cn.....domain.某个类_$$_javassist_0 cannot be cast to javassist.util.proxy.Proxy

包冲突了,hibernate和strus都有javassist-XX.jar这个包只需要导入一个就可以了

0 0
原创粉丝点击