使用SessionFactory时Hibernate中的奇怪NumberFormatException异常 [英] Strange NumberFormatException in Hibernate When Using SessionFactory

查看:131
本文介绍了使用SessionFactory时Hibernate中的奇怪NumberFormatException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[从今天早些时候看到我的相关但不同的问题可能很有用 Hibernate Eclipse中的Schema导出。]

[It may be useful to see my related, yet distinct, question from earlier today here Hibernate Schema Export in Eclipse .]

我有以下Java类

package com.examscam.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

@Entity
public class User {
    private Long id;
    private String password;

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public String getPassword() {
        return password;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public static void main(String[] args) {
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(User.class);
        config.configure();
//      new SchemaExport(config).create(true, true);

        SessionFactory factory = config.buildSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();

        User u = new User();        
        u.setPassword("abc123");

        session.saveOrUpdate(u);
        session.getTransaction().commit();

        System.out.println("Done.");
    }
}

我有一个(MySQL)数据库,用户表与代码

I have a (MySQL) database and have created the User table with the code

create table User (id integer not null auto_increment, password varchar(255), primary key (id))

但是,当我尝试运行这段代码时,我给了以下堆栈跟踪

However, when I try running this code, I am given the following stack trace

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:470)
    at java.lang.Integer.valueOf(Integer.java:554)
    at org.hibernate.util.PropertiesHelper.getInteger(PropertiesHelper.java:37)
    at org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverManagerConnectionProvider.java:47)
    at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
    at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:56)
    at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:414)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
    at com.examscam.model.User.main(User.java:40)

User class的第40行是

Line 40 of the User class is

    SessionFactory factory = config.buildSessionFactory();



<我没有在该行的任何地方输入一个空字符串,我已经从书中完全复制了代码。我毫不怀疑这本书是正确的我的问题更多的是任何人都可以想到任何原因(xml文件中的错误,类路径排序问题,文件不在正确的目录等)为什么会发生这种行为?'。

Does anyone have any idea why this is happening? I am not entering an empty String anywhere on that line and I have copied the code exactly from the book. I have no doubt that the book is correct; my questions is more along the lines of 'Can anyone think of any reason (errors in xml files, classpath ordering issues, files not being in the correct directories etc) why this behaviour would occur?'.

此问题的答案也可能有助于解决此页面顶部的链接中导出模式的问题。

An answer to this question might also help solve the issue in the link at the very top of this page with exporting the schema.

谢谢, Conor。

推荐答案

所以我相信我已经解决了这个问题,最后是两个问题。首先,类路径中还有一个较旧的hibernate副本。这个旧版本需要不再需要的配置元素,导致 NumberFormatException ,因为它尝试将一个空字符串解析为缺少的事务隔离配置元素的int。

So I believe I've solved this problem which ended up being two problems. First, there was another older copy of hibernate in the classpath. This older version was requiring config elements that no longer were required, leading to the NumberFormatException as it was trying to parse an empty string into an int for the missing transaction isolation config element.

修复后,我们开始在这一行代码上看到一个 NullPointerException

After fixing that, we started to see a NullPointerException on this line of code:

Environment.class.getClassLoader().getResourceAsStream(stripped);

看看这一行代码,唯一可以抛出 NPE getClassLoader 调用,但是怎么可能是null?如果返回的类加载器是引导类加载器,那么<​​/ c $ c> getClassLoader 可以返回null。我问Conor,检查他是否将hibernate放入引导类路径,结果证明他是这样做的。从启动类中删除它修复了问题。

Looking at that line of code, the only thing that could have been throwing the NPE was the getClassLoader call, but how could that ever be null? As it turns out getClassLoader can return null if the class loader being returned is the boot class loader. I asked Conor to check if he had put hibernate into the boot class path and it turns out that he did. Removing it from the boot class fixed the issue.

所以这两个课程:


  1. 仔细管理你的课程路径。知道它是什么,并保持尽可能的修剪。较少的第三方库更好。

  2. 不要将东西放在启动类路径上。如果您决定忽略此规则,请了解潜在的影响因素,这样您就不会陷入这样的问题。

这篇关于使用SessionFactory时Hibernate中的奇怪NumberFormatException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆