Product.java
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;
}
}
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="Product" table="PRODUCTS">
<id name="productId" column="pid" >
<generator class="assigned" />
</id>
<property name="proName" column="pname" />
<property name="price"/>
</class>
</hibernate-mapping>
的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>
<!-- Related to the connection START -->
<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">paris</property>
<!-- Related to the connection END -->
<!-- Related to hibernate properties START -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<!-- Related to hibernate properties END -->
<!-- Related to mapping START -->
<mapping resource="product.hbm.xml" />
<!-- Related to the mapping END -->
</session-factory>
</hibernate-configuration>
ClientForSave.java
import org.hibernate.*;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.*;
public class ClientForSave {
public static void main(String[] args)
{
Configuration cfg = new Configuration();
cfg.configure();
StandardServiceRegistryBuilder builder=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());
SessionFactory factory=cfg.buildSessionFactory(builder.build());
Session session = factory.openSession();
Product p=new Product();
p.setProductId(101);
p.setProName("iPhone");
p.setPrice(25000);
Transaction tx = session.beginTransaction();
session.save(p);
System.out.println("Object saved successfully.....!!");
tx.commit();
session.close();
factory.close();
}
}
控制台输出错误&gt;&gt;&gt;
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/MetadataProvider
at ClientForSave.main(ClientForSave.java:12)
Caused by: java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.MetadataProvider
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
P lease解决这个问题。因为10天我试图编写像这样的hibernate示例应用程序每次它都显示我的错误..我只是一个初学者.i请求你们请解决这个并以简单的方式解释我..谢谢你的所有建议
答案 0 :(得分:3)
异常实际上是告诉你问题是什么:
java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/MetadataProvider
在运行时,它找不到类org.hibernate.annotations.common.reflection.MetadataProvider。您需要在应用程序的类路径中提供具有此类的jar。我不知道你的设置是什么样的,但是从你得到你的hibernate-core-4.3.1.Final.jar的同一个源码,你应该能得到一个hibernate-commons-annotations-4.0.4.Final .jar(假设您使用的是最新版本)。检索它,将它放入类路径中,异常就会消失。