的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">
<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 :(得分:0)
将hibernate.cfg.xml
移至目录src/main/resources
。这将解决问题