论坛首页 入门技术论坛

[hibernate入门]刚学hibernate就受打击啊!找不到.hbm.xml文件!不会改.

浏览 2003 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2008-03-11  
    我明明在项目中看到了hbm.xml文件,可报错就是找不到啊!就简单的小程序,刚学想把数据插入数据库中。

实体类: Student.java:
package util;

public class Student {
private String username;
private String userpwd;
private String userid;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
}
操作类的方法:
  
package util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateOperation {

/**
* @param args
*/
SessionFactory sf;
Session session;
public  HibernateOperation(){
Configuration cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
session = sf.openSession();
}
public void Insert(Student s){
Transaction tran = session.beginTransaction();
tran.begin();
session.save(s);
tran.commit();
session.close();
}
public void Delete(Student s){
Transaction tran = this.session.beginTransaction();
tran.begin();
tran.commit();
session.close();
}
}

测试类 :

package util;
public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Student student = new Student();
student.setUserid("00006");
student.setUsername("zhongguo");
student.setUserpwd("123");
HibernateOperation ho = new HibernateOperation();
ho.Insert(student);
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Student表的映射文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="util.Student" table="student" catalog="sa">
        <composite-id name="id" class="util.StudentId">
            <key-property name="userid" type="java.lang.String">
                <column name="userid" length="30" />
            </key-property>
            <key-property name="username" type="java.lang.String">
                <column name="username" length="50" />
            </key-property>
            <key-property name="userpwd" type="java.lang.String">
                <column name="userpwd" length="50" />
            </key-property>
        </composite-id>
    </class>

hibernate.cfg.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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/sa</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="myeclipse.connection.profile">abc</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<mapping resource="session/Student.hbm.xml" />

</session-factory>

</hibernate-configuration>
</hibernate-mapping>

出错信息:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.MappingException: Resource: session/Student.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:479)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1433)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1390)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1310)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1296)
at util.HibernateOperation.<init>(HibernateOperation.java:17)
at util.Test.main(Test.java:14)

不知道怎么该啊?谁能仔细的讲一讲吗?
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics