2016-05-13 72 views
1

的hibernate.cfg.xml不可能通過休眠連接到DB中蝕

<?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="connection.driver_class">org.postgresql.Driver</property> 
    <property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property> 
    <property name="connection.username">postgres</property> 
    <property name="connection.password">*****</property> 

    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property> 
    <property name="show_sql">true</property> 
    <property name="hbm2ddl.auto">update</property> 

<mapping resource="Product.hbm.xml"></mapping> 
</session-factory> 
</hibernate-configuration> 

Product.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> 
    <class name="str.Product" table="products"> 

    <id name="productId" column="pid" /> 
    <property name="proName" column="pname" length="10"/> 
    <property name="price"/> 

    </class> 
    </hibernate-mapping> 

Product.java

package Str; 

    public class Product{ 

     private int productId; 
     private String proName; 
     private double price; 

     public void setProductId(int productId) 
     { 
      this.productId = productId; 
     } 
     public int getProductId() 
     { 
      return productId; 
     } 

     public void setProName(String proName) 
     { 
      this.proName = proName; 
     } 
     public String getProName() 
     { 
      return proName; 
     } 

     public void setPrice(double price) 
     { 
      this.price = price; 
     } 
     public double getPrice() 
     { 
      return price; 
     } 
    } 

ClientProgram.java

package Str; 

       import org.hibernate.*; 
       import org.hibernate.cfg.*; 

       public class ClientProgram { 

        public static void main(String[] args) 
        { 

         Configuration cfg = new Configuration(); 
         cfg.configure(); 

         SessionFactory factory = cfg.buildSessionFactory(); 
         Session session = factory.openSession(); 
         Object o=session.load(Product.class,new Integer(101)); 
         Product s=(Product)o; 
         // For loading Transaction scope is not necessary... 
         System.out.println("Loaded object product name is___"+s.getProName()); 

         System.out.println("Object Loaded successfully.....!!"); 
         session.close(); 
         factory.close(); 
        }} 

輸出繼電器

NFO: Configuration resource: /hibernate.cfg.xml 
      Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found 
       at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147) 
       at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1402) 
       at org.hibernate.cfg.Configuration.configure(Configuration.java:1424) 
       at org.hibernate.cfg.Configuration.configure(Configuration.java:1411) 
       at Str.ClientProgram.main(ClientProgram.java:12) 

請糾正這種

這是代碼,當我試圖運行此其顯示上面提到的錯誤,請幫我在這感謝ü所有

+0

你是否在應用程序上下文中提到了hibernate.cfg.xml文件? – LearningPhase

+0

你還在哪裏放置了你的hibernate.cfg.xml文件? – LearningPhase

+0

當webapp啓動時,hibernate.cfg.xml必須位於類路徑的根目錄下。如果您使用maven構建項目,請將hibernate.cfg.xml放入src/main/resources目錄中,以便在構建war包時它將自動放置在/ WEB-INF/classes中。如果你不使用maven,把這個文件放在你的WEB-INF/classes目錄下。 – LearningPhase

回答

0

移動你的hibernate.cfg.xml到目錄src/main/resources。這將解決問題