Reputation: 21
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();
}
}
Output at Console is error>>>
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 solve this .since 10 days i am trying to code hibernate sample application like this every time it shows me error..i am just a beginner .i request u guys please solve this and explain me in easy way ..thank u for all suggestions
Upvotes: 0
Views: 39
Reputation: 4273
The exception is actually telling you what the problem is:
java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/MetadataProvider
At run time, it can't find the class org.hibernate.annotations.common.reflection.MetadataProvider. You need to provide the jar that has this class in your application's classpath. I don't know what your setup is like, but from the same source where you got your hibernate-core-4.3.1.Final.jar, you should be able to get a hibernate-commons-annotations-4.0.4.Final.jar (assuming you're using the latest versions). Retrieve it, put it into your classpath, and the exception should go away.
Upvotes: 3