当前位置: 首页 > 知识库问答 >
问题:

MappingException:无法从资源中读取映射:

薛华容
2023-03-14

Hi在运行我的java类时得到以下错误:创建会话工厂失败,出现异常org.hibernate.mappingException:无法从资源中读取映射:software.hbm.xml org.hibernate.cfg.configuration.addResource(configuration.java:485),在org.hibernate.cfg.cfiguration.parsemappingElement(configuration.java:1465),在org.hibernate.cfg.cfiguration.parsesessionFactory(configuration.java:1433),在doConfigure(configuration.java:1390)在org.hibernate.cfg.configuration.configure(configuration.java:1310)在org.hibernate.cfg.configuration.configure(configuration.java:1310)在com.tcs.com.dbconnect.main(dbconnect.java:20)在org.hibernate.cfg.configuration.configure(configuration.java:1296)在com.tcs.com.dbconnect.main(guration.java:431)在org.hibernate.cfg.comfiguration.addresource(configuration.java:482)...7更多是由:org.dom4j.documentexception:www.hibernate.org嵌套异常:www.hibernate.org在org.dom4j.io.saxreader.read(saxreader.java:484)在org.hibernate.cfg.comfiguration.addinputstream(configuration.java:422)在com.tcs.com.dbconnect.add(一二三

    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>

software.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();
         }
         }
}

共有1个答案

壤驷喜
2023-03-14

似乎您有一个名为software.java的java类,software.hbm.xml文件需要在java文件的同一个包中。

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

 <mapping resource="the/package/where/your/hbm/is/Software.hbm.xml"/>
 类似资料:
  • 我有以下前缀: 有办法 在intellij中,idea工作正常,但启动jar时出现错误: JAVA尼奥。文件NoSuchFileException:文件:/app。罐子/BOOT-INF/classes/模板/请求订单/未标记/请求订单未标记。pdf

  • 我很难弄清楚这个问题,我几乎什么都试过了。 我希望我的程序从Jar文件中读取两个资源,并且它将在eclipse中运行良好。但是当我从命令提示符运行jar时,它会给出空指针异常。 结构: src/main/java/App。JAVA src/main/resources/properties/application。属性 src/main/resources/spring/applicationCo

  • Redis.Properties spring-redis.xml 代码 控制器

  • 我正在运行多个工作线程(大约10个)来访问redis Q的数据。 我正在使用infinte timeout For Jedis客户端。 但我仍然收到错误“无法从池中获取资源”。stacktrace如下所示。

  • 问题内容: 我有一个XML文件,需要在Android SDK中进行解析。 如何从资源中读取XML文件路径? XML包含: 问题答案: 将其放在文件夹下。然后,您可以使用以下命令打开它: 这里有一个有关如何使用的示例: http://android-er.blogspot.com/2010/04/read-xml-resources-in-android- using.html

  • 我想从我的jar中读取一个资源,如下所示: 在Eclipse中运行它时工作良好,但如果我将它导出到一个jar中,然后运行它,就会出现一个IllegalArgumentException: 我真的不知道为什么,但通过一些测试我发现如果我改变 至 然后它的工作方式相反(它在jar中工作,但不能在eclipse中工作)。 我使用的是Eclipse,我的文件所在的文件夹位于类文件夹中。