我有一个名为lablab的db,其中有一个名为table的表,因此我的程序在注册时会注册,它会在数据库中保存userName,fullName,birthDay,password和age。我的数据库包含fullName,userName,passWord,birthDay和age的列。我的程序运行但无法插入数据库。
这是我的映射:
<hibernate-mapping>
<class name="hello.model" table="table" schema="lablab">
<id name="id" column="ID" type="int">
<generator class="native"/>
</id>
<property name="fullName" column="fullName" type="string"/>
<property name="userName" column="userName" type="string"/>
<property name="passWord" column="passWord" type="string"/>
<property name="birthDay" column="birthDay" type="string"/>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
这是我的cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/lablab</property>
<mapping resource="hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这是我的java类
package hello;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class model extends ActionSupport{
private String fullName, passWord, userName, birthDay, yearX, userLogin, passLogin;
private int age, year, yearN;
private static SessionFactory factory;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getBirthDay() {
return birthDay;
}
public void setBirthDay(String birthDay) {
this.birthDay = birthDay;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUserLogin() {
return userLogin;
}
public void setUserLogin(String userLogin) {
this.userLogin = userLogin;
}
public String getPassLogin() {
return passLogin;
}
public void setPassLogin(String passLogin) {
this.passLogin = passLogin;
}
public String register() throws Exception {
try
{
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
applySettings(configuration.getProperties());
factory = configuration.buildSessionFactory(builder.build());
}
catch (HibernateException ex)
{
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
addUser(fullName, userName, passWord, birthDay, age);
String success ="success";
return success;
}
private Integer addUser(String fullName, String userName, String passWord, String birthDay, int age) {
Session session = factory.openSession();
Transaction tx = null;
Integer uID = null;
try{
tx = session.beginTransaction();
User item = new User(fullName, userName, passWord, birthDay, age) {};
uID = (Integer) session.save(item);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
return uID;
}
}
这是我的用户类
class User {
private String fullName, userName, passWord, birthDay;
private int age;
public User(){}
public User(String fullName, String userName, String passWord, String birthDay, int age){
this.fullName = fullName;
this.userName = userName;
this.passWord = passWord;
this.birthDay = birthDay;
this.age = age;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getBirthDay() {
return birthDay;
}
public void setBirthDay(String birthDay) {
this.birthDay = birthDay;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
答案 0 :(得分:0)
如果您使用的是Hibernate 5,则应通过以下方式创建SessionFactory
:
factory = new Configuration().configure().buildSessionFactory();
这是一种不正确的方式:
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
applySettings(configuration.getProperties());
factory = configuration.buildSessionFactory(builder.build());