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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 中,我们可以通过配置 `LocalSessionFactoryBean` Bean 来加载 Hibernate 的配置文件和映射文件。下面是一个示例配置: ```java @Configuration @EnableTransactionManagement public class HibernateConfig { @Bean public LocalSessionFactoryBean sessionFactory() { LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setPackagesToScan("com.example.demo.entity"); sessionFactory.setHibernateProperties(hibernateProperties()); sessionFactory.setMappingLocations(new ClassPathResource("com/example/demo/entity/User.hbm.xml")); return sessionFactory; } @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8"); dataSource.setUsername("root"); dataSource.setPassword("password"); return dataSource; } private final Properties hibernateProperties() { Properties hibernateProperties = new Properties(); hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "update"); hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); hibernateProperties.setProperty("hibernate.show_sql", "true"); return hibernateProperties; } } ``` 在上面的配置中,我们通过 `setMappingLocations` 方法设置了 Hibernate 的映射文件位置。这里我们将 `User.hbm.xml` 文件放在了 `com/example/demo/entity/` 目录下,所以需要设置为 `new ClassPathResource("com/example/demo/entity/User.hbm.xml")`。如果有多个映射文件,可以设置多个 `ClassPathResource` 对象。当然,你也可以使用 `PathMatchingResourcePatternResolver` 来扫描指定目录下的所有映射文件。最后,将 `LocalSessionFactoryBean` Bean 注入到其他需要使用 Hibernate 的 Bean 中即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值