简体   繁体   English

使用SessionFactory时,Hibernate中出现奇怪的NumberFormatException

[英]Strange NumberFormatException in Hibernate When Using SessionFactory

[It may be useful to see my related, yet distinct, question from earlier today here Hibernate Schema Export in Eclipse .] [在今天的Eclipse中的Hibernate Schema Export中看到我的相关但又截然不同的问题可能是有用的。]

I have the following Java class 我有以下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.");
    }
}

I have a (MySQL) database and have created the User table with the code 我有一个(MySQL)数据库,并使用代码创建了User表

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)

Line 40 of the User class is User类的第40行是

    SessionFactory factory = config.buildSessionFactory();

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?'. 我的问题更像是“任何人都能想到为什么会发生此现象的任何原因(xml文件中的错误,类路径排序问题,文件不在正确的目录中等等)”。

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. 这个问题的答案也可能有助于导出本页最上方链接中的问题。

Thanks, Conor. 谢谢,康纳

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. 这个较旧的版本需要不再需要的配置元素,导致NumberFormatException因为它试图将空字符串解析为用于缺少事务隔离配置元素的int。

After fixing that, we started to see a NullPointerException on this line of code: 解决此问题后,我们开始在以下代码行上看到NullPointerException

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

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? 看看那行代码,唯一可以抛出NPE东西就是getClassLoader调用,但是那怎么可能为空呢? As it turns out getClassLoader can return null if the class loader being returned is the boot class loader. 事实证明,如果要返回的类加载器是引导类加载器,则getClassLoader可以返回null。 I asked Conor to check if he had put hibernate into the boot class path and it turns out that he did. 我问科纳尔(Conor)检查他是否已将休眠状态放入引导类路径,事实证明他这样做了。 Removing it from the boot class fixed the issue. 从引导类中删除它可以解决此问题。

So two lessons here: 所以这里有两课:

  1. Manage your class path carefully. 仔细管理您的课堂路径。 Know what's on it and keep it as trim as possible. 知道上面有什么,并使其尽可能修剪。 The less 3rd party libs the better. 第三方解放越少越好。
  2. Don't ever put stuff on the boot class path. 永远不要在启动类路径上放东西。 If you decide to ignore this rule, understand what the potential implications are so you're not stuck chasing issues like this. 如果您决定忽略此规则,请了解潜在的含义,以免您陷入这样的问题。

The database id column datatype is integer, but entity class's id datatype is long, not sure whether this is the problem. 数据库id列的数据类型是整数,但是实体类的id数据类型很长,不确定是否是问题所在。 But it is worth giving a try to changed the datatype of entity's id to int. 但是值得尝试将实体ID的数据类型更改为int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM