[今天早些时候在这里查看我的相关但不同的问题可能很有用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)数据库并使用代码创建了用户表
create table User (id integer not null auto_increment, password varchar(255), primary key (id))
但是,当我尝试运行此代码时,我得到以下堆栈跟踪
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 类的第 40 行是
SessionFactory factory = config.buildSessionFactory();
有谁知道为什么会这样?我没有在该行的任何地方输入空字符串,并且我已经完全从书中复制了代码。我毫不怀疑这本书是正确的;我的问题更像是“谁能想到任何原因(xml 文件中的错误、类路径排序问题、文件不在正确的目录中等)为什么会发生这种行为?”。
对这个问题的回答也可能有助于解决此页面顶部链接中导出架构的问题。
谢谢,康纳。