当使用SessionFactory时,Hibernate中的奇怪NumberFormatException [英] Strange NumberFormatException in Hibernate When Using SessionFactory
问题描述
[从今天早些时候查看我的相关但又截然不同的问题可能很有用 Hibernate ]
我有以下Java类
code> 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();
用户u = new User();
u.setPassword(abc123);
session.saveOrUpdate(u);
session.getTransaction()。commit();
System.out.println(Done。);
}
}
我有一个(MySQL)数据库,含有代码的用户表
create table User(id integer not null auto_increment,password varchar(255),primary key )
但是,当我尝试运行此代码时,我会得到以下堆栈跟踪
线程main中的异常java.lang.NumberFormatException:对于输入字符串:
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)
在org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverManagerConnectionProvider.java:47)
在org.hibernate.connection.ConnectionProviderFactory。 newConnectionProvider(ConnectionProviderFactory.java:124)
在org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:56)
在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类的第40行是
SessionFactory factory = config.buildSessionFactory();
有没有人知道为什么会发生这种情况?我不是在该行的任何地方输入一个空字符串,我已经从该书复制了代码。我毫不怀疑这本书是正确的;我的问题更多的是任何人都可以想到任何原因(xml文件中的错误,类路径排序问题,文件不在正确的目录等)为什么会发生这种行为。
此问题的答案也可能有助于解决导出模式的页面顶部链接中的问题。
谢谢, Conor。
所以我相信我已经解决了这个问题,结果是两个问题。首先,在类路径中有另一个较旧的hibernate副本。这个旧版本需要不再需要的配置元素,导致 NumberFormatException
,因为它试图将一个空字符串解析为缺少的事务隔离配置元素的int。
修正后,我们开始在这行代码中看到 NullPointerException
:
Environment.class.getClassLoader()。getResourceAsStream(stripped);
看看这行代码,唯一可能会抛出 NPE
是 getClassLoader
调用,但是怎么会是null呢?因为结果是 getClassLoader
可以返回null如果返回的类加载器是引导类加载器。我问Conor检查他是否把hibernate放到引导类路径中,结果证明他做了。
这里有两个教训:
- 仔细管理您的类路径。知道它上面有什么,并尽可能保持修剪。
- 不要把东西放在引导类路径上。如果您决定忽略此规则,请了解潜在的影响,因此您不会遇到像这样的问题。
[It may be useful to see my related, yet distinct, question from earlier today here Hibernate Schema Export in Eclipse .]
I have the following Java class
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
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
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?'.
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.
After fixing that, we started to see a NullPointerException
on this line of code:
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? 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.
So two lessons here:
- Manage your class path carefully. Know what's on it and keep it as trim as possible. The less 3rd party libs the better.
- 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.
这篇关于当使用SessionFactory时,Hibernate中的奇怪NumberFormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!