hibernate+mysql+ecliple第一次运行错误信息
UserInfo类://持久化类信息
package hibernate.ch1;
public class UserInfo {
private Integer id;
private String username;
private String password;
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;
}
}
UserInfo.hbm.xml信息:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--类和表之间的关联-->
<class name="hibernate.ch1.UserInfo" table="login">
<!--类对象的主键和表的主键关联 -->
<id name="id" type="integer">
<column name="id" />
<!-- 指明主键的自增长类型 -->
<generator class="identity" />
</id>
<!-- 以下为普通字段的关系 -->
<property name="username" type="String" column="username"/></property>
<property name="password" type="String" column="password"/></property>
</class>
</hibernate-mapping>
HibernateTest主类信息:
package hibernate.ch1;
import hibernate.ch1.UserInfo;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateTest {
public static void main(String args[]){
//加载配置文件
SessionFactory sessions=new Configuration().configure().buildSessionFactory();
Session session=sessions.openSession();
Transaction tx=null;
try{
//开始事务
tx=session.beginTransaction();
//给对象设定值
UserInfo u=new UserInfo();
u.setUserName("wuquan");
u.setPassword("rat");
System.out.println("Start to insert into database....");
//保存数据到数据库
session.save(u);
//从持久化类UserInfo中读取数据
UserInfo u1=(UserInfo) session.load(UserInfo.class, new Integer(1));
System.out.println("Loan from database username:"+u1.getUserName());
//结束事务
tx.commit();
tx=null;
System.out.println("Congratuation!It works!");
}
catch(HibernateException e){
e.printStackTrace();
if(tx!=null){
tx.rollback();
}
}
finally{
session.close();
}
}
}
Hibbernate.cfg.xml配置XML映射信息:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernat-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.name">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool.size">20</property>
<property name="hibernate.show_sql">true</property>
<property name="jdbc.fetch_size">50</property>
<property name="jdbc.batch_size">25</property>
<property name="jdbc.use_scrollable_resultset">false</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">gbk</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="hibernate/ch1/UserInfo.hbm.xml"/>
</session-factory>
</hibernat-configuration>
运行显示错误信息:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.hibernate.cfg.Configuration.reset(Configuration.java:249)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:216)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:220)
at hibernate.ch1.HibernateTest.main(HibernateTest.java:16)
Caused by: java.lang.NullPointerException
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:167)
at org.hibernate.cfg.Environment.<clinit>(Environment.java:579)
... 4 more
--基本环境是eclipse3.3,mysql5.0.51b,hibernate3.5-final
--jar包引入的错误问题基本已解决,从这几天的折腾来看应该不是jar包的问题。为了防止包jar版本冲突,也换过不同版本的slf4j包。
--代码本身也应该没问题。
--hibernate的版本也换过,还是不能解决问题。
求解决办法,已经弄了整整三天了,问题没解决。
刚刚入门hibernate,配置个环境都头疼。有遇到过此问题的朋友请不吝赐教!
页:
[1]