当前位置:编程学习 > JAVA >>

关于Hibernate的一个错误!!!

Hibernate.cfg.xml



<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<!-- 数据库url -->
<property name="connection.url">jdbc:mysql://localhost:3306/userinfo
</property>
<!-- 数据库驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库账户 -->
<property name="connection.username">root</property>
<!-- 数据库密码 -->
<property name="connection.password">bdqn</property>
<!-- 每个数据库都有其对应的dialect(方言)以匹配其平台特性 数据库的连接方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 指定当前session范围和上下文 -->
<property name="current_session_context_class">thread</property>

<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- 是否将SQL输出到日志以供调试 -->
<property name="show_sql">true</property>
<!-- 映射文件配置 -->
<mapping resource="pds/bdqn/entityxml/User.hbm.xml" />
</session-factory>
</hibernate-configuration>



User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping package="pds.bdqn.entity">
<class name="User" table="tbl_user">
<id name="id"></id>
<property name="username"></property>
<property name="password"></property>
<property name="telephone"></property>
<property name="isadmin"></property>
</class>
</hibernate-mapping>

实体类  User

public class User implements java.io.Serializable {

private Integer id;

private String username;

private String password;

private String telephone;

private String isadmin;

public Integer getId() {
return id;
}

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

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 getTelephone() {
return telephone;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}

public String getIsadmin() {
return isadmin;
}

public void setIsadmin(String isadmin) {
this.isadmin = isadmin;
}

public User() {
}

}


测试类

package pds.bdqn.entity;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class AddTest {

public void add() {
User user = new User();
user.setUsername("BBQQ");
user.setPassword("123456");
user.setTelephone("15038898150");
user.setIsadmin("啊圣诞快乐哈萨克大世界的话");
Transaction tc = null;
Session session = null;
SessionFactory sf = null;
try {
Configuration conf = new Configuration().configure();
sf = conf.buildSessionFactory();
session = sf.openSession();
tc = session.beginTransaction();
session.save(user);
tc.commit();
} catch (Exception e) {
tc.rollback();
e.printStackTrace();
} finally {
session.close();
sf.close();
}
}
}


提示的异常是::

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NullPointerException
at pds.bdqn.entity.AddTest.add(AddTest.java:30)
at pds.bdqn.service.UserService.main(UserService.java:9)

怎么解决啊???大家  。。。。求助!!! --------------------编程问答-------------------- 你看下你有没有这个slf4j jar包他报一个空指针异常在AddTest类的第三十行.. --------------------编程问答-------------------- 没有,这个包是哪的? --------------------编程问答-------------------- 实力映射文件怎么写啊?
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,