我想是的,编辑cfg.xml文件,参考其中的classpath设置,将你的例子添加到其中..
试试可行否...

解决方案 »

  1.   

    我也碰到同样的问题
    但我根本没有cfg.xml,因方文档说明可以用properties代替但我的properties确实在classpath中,它也是死活说找不到xml.
      

  2.   

    package TEST;
    import net.sf.hibernate.cfg.Configuration;
    import net.sf.hibernate.SessionFactory;
    import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
    import net.sf.hibernate.Session;
    import net.sf.hibernate.Query;
    import net.sf.hibernate.Hibernate;
    import net.sf.hibernate.type.LongType;
    import net.sf.hibernate.Transaction;
    import java.io.*;
    public class CatDB
    {
        public static void main(String args[]) throws Exception
        {
            java.util.Properties pro = new java.util.Properties();
            final String HIB_PRO_FILE = "D:\\tomcat\\webapps\\hib\\WEB-INF\\classes\\hibernate.properties";
            pro.load(new java.io.FileInputStream(HIB_PRO_FILE));
            if(pro == null)
                System.out.println("Can not find the pro");        File f = new File(HIB_PRO_FILE);
            if((f.exists() )&& (  f.isFile()))
                System.out.println("File existing");
            else
                System.out.println("File not found");        Configuration confg = new Configuration();
            confg.setProperties(pro);
            confg.addFile("D:\\tomcat\\webapps\\hib\\WEB-INF\\classes\\TEST\\Cat.hbm.xml");
            SessionFactory sessionFactory = confg.buildSessionFactory();
            Session session = sessionFactory.openSession();
            Cat cat = new Cat();
            cat.setId("abc");
            cat.setName("cat1");
            cat.setSex('M');
            cat.setWeight(50);
            session.save(cat);
            session.flush();
        }}
      

  3.   

    I use the absolute path as the upper code,it work.