设为首页 收藏本站
查看: 612|回复: 0

[经验分享] hibernate+mysql+ecliple第一次运行错误信息

[复制链接]

尚未签到

发表于 2016-10-18 10:06:57 | 显示全部楼层 |阅读模式
  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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-287840-1-1.html 上篇帖子: 懒人策略之:批量备份mysql数据库 下篇帖子: jQuery(Ajax)/PHP/MySQL实现搜索自动提示功能
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表