1

您好,在运行我的 java 类时出现以下错误:会话工厂的创建失败,出现异常 org.hibernate.MappingException:无法从资源中读取映射:Software.hbm.xml org.hibernate.MappingException:无法从资源中读取映射: org.hibernate.cfg.Configuration.addResource(Configuration.java:485) 的 Software.hbm.xml org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465) 的 org.hibernate.cfg.Configuration.parseSessionFactory (Configuration.java:1433) 在 org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414) 在 org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1390) 在 org.hibernate.cfg.Configuration。配置(配置.java:1310) at org.hibernate.cfg.Configuration.configure(Configuration.java:1296) at com.tcs.com.DbConnect.main(DbConnect.java:20) 原因:org.hibernate.MappingException:无法解析映射文档在 org.hibernate.cfg.Configuration.addResource(Configuration.java:482) 的 org.hibernate.cfg.Configuration.addInputStream(Configuration.java:431) 的输入流中... 7 更多原因:org.dom4j.DocumentException : www.hibernate.org 嵌套异常: www.hibernate.org at org.dom4j.io.SAXReader.read(SAXReader.java:484) at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:422) .. . com 的线程“main”java.lang.NullPointerException 中还有 8 个异常。tcs.com.DbConnect.add(DbConnect.java:46) 在 com.tcs.com.DbConnect.main(DbConnect.java:30) 在 sessionfactory 创建后的 main(DbConnect.java:30)123

    MY hbm.xml file

    <?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">

    <hibernate-mapping package="com.tcs.beans">

        <class name="Software" table="SOFTWARE">
            <id name="id" type="int" column="id">
            <generator class="native"></generator>
            </id>
            <property name="c" column="c" />
            <property name="cpp" column="cpp" />
            <property name="java" column="java" />
            <property name="val" column="val" />

        </class>


    </hibernate-mapping>


Please help me with this as soon as possible
]







MY 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">
<hibernate-configuration>
   <session-factory>
   <property name="hibernate.dialect">
      org.hibernate.dialect.Oracle10gDialect
   </property>
   <property name="hibernate.connection.driver_class">
      oracle.jdbc.OracleDriver
   </property>

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
     jdbc:oracle:thin:@localhost:1521/xe
   </property>
   <property name="hibernate.connection.username">
      ******
   </property>
   <property name="hibernate.connection.password">
      *******
   </property>

   <!-- List of XML mapping files -->
   <mapping resource="com/sss/beans/Software.hbm.xml"/>

</session-factory>
</hibernate-configuration>


My hbm.xml



<?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">

<hibernate-mapping package="com.tcs.beans">

    <class name="Software" table="SOFTWARE">
        <id name="id" type="int" column="id">
        <generator class="native"></generator>
        </id>
        <property name="c" column="c" type="String"/>
        <property name="cpp" column="cpp" type="String"/>
        <property name="java" column="java" type="String"/>
        <property name="val" column="val" type="int"/>

    </class>


</hibernate-mapping>

软件.java

   public class Software {
        private int id;
        private String c;
        private String cpp;
        private String java;
        private int val;
        public Software(){

        }
        public Software(String c,String cpp,String java,int val){
        this.c=c;
        this.cpp=cpp;
        this.java=java;
        this.val=val;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getC() {
            return c;
        }
        public void setC(String c) {
            this.c = c;
        }
        public String getCpp() {
            return cpp;
        }
        public void setCpp(String cpp) {
            this.cpp = cpp;
        }
        public String getJava() {
            return java;
        }
        public void setJava(String java) {
            this.java = java;
        }
        public int  getVal() {
            return val;
        }
        public void setVal(int val) {
            this.val = val;
        }



    }

我的主要java文件...

public class DbConnect {

    private static SessionFactory sessionFactory;
    public static void main (String[] args) {
    System.out.println("in main");
        try{
            System.out.println("in main before sessionfactory creation");
            sessionFactory = new Configuration().configure().buildSessionFactory();

            System.out.println("in main after sessionfactory creation");
        }catch(Exception xe){
            System.out.println("in main exc");
            System.err.println("Creation of session Factory failed with the exception "+xe);
            xe.printStackTrace();
        }
        System.out.println("in main after sessionfactory creation123");
        DbConnect soft = new DbConnect();
        Integer e1 = soft.add("c2","cpp2","java2",2);
        Integer e2 = soft.add("c3","cpp3","java3",3);
        Integer e3 = soft.add("c4","cpp4","java4",4);

        soft.show();
        soft.updateEmployee(e2, 5);
        soft.show();
        soft.deleteEmployee(e2);



    }



    public Integer add(String c,String cpp,String java ,int val){
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        Integer intID = null;
        Software soft = new Software(c,cpp,java,val);
        try{
            tx = session.beginTransaction();
            intID = (Integer) session.save(soft);
            tx.commit();
        }catch(Exception e){
            if(tx!=null)tx.rollback();
            e.printStackTrace();
        }
        finally{
            session.close();
        }
        return intID;
    }

    public void show(){
        Session session = sessionFactory.openSession();
        Transaction tx = null;

        try{

            tx = session.beginTransaction();
            List list = session.createQuery("FROM SOFTWARE").list();
            Iterator it = list.iterator();
            while(it.hasNext()){
                Software sw = (Software) it.next();
                System.out.println("c"+sw.getC());
                System.out.println("cpp"+sw.getCpp());
                System.out.println("java"+sw.getJava());
                System.out.println("val"+sw.getVal());
            }
            tx.commit();

        }catch(Exception xe){
            if(tx!=null)tx.rollback();
            xe.printStackTrace();
            }finally{
                session.close();
            }
    }

     public void updateEmployee(Integer EmployeeID, int salary ){
         Session session = sessionFactory.openSession();
         Transaction tx = null;
         try{
             tx = session.beginTransaction();
             Software sw = (Software) session.get(Software.class, EmployeeID);
             sw.setVal(salary);
             session.update(sw);
             tx.commit();
         }catch(Exception e){
             if(tx!=null)tx.rollback();
             e.printStackTrace();
         }
     }


         public void deleteEmployee(Integer EmployeeID){
         Session session = sessionFactory.openSession();
         Transaction tx= null;
         try{
             tx = session.beginTransaction();
             Software sw = (Software) session.get(Software.class , EmployeeID);
             session.delete(sw);
             tx.commit();
         }catch(Exception e){
             if(tx!=null)tx.rollback();
             e.printStackTrace();
         }
         }
}
4

1 回答 1

0

好像你有一个名为 Software.java 的 java 类,Software.hbm.xml 文件需要在 java 文件的同一个包中。

您的文件hibernate.cfg.xml需要一行用于映射

 <mapping resource="the/package/where/your/hbm/is/Software.hbm.xml"/>
于 2012-12-07T14:41:41.203 回答