Please solve the problem
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="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password"></property> <property name="hibernate.connection.url">jdbc:mysql://172.18.226.38:3306/hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <mapping class="net.roseindia.Product"/> </session-factory> </hibernate-configuration>
Product.java:
package net.roseindia;
// Generated May 18, 2009 7:40:50 PM by Hibernate Tools 3.2.4.GA
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table;
/** * Product generated by hbm2java */ @Entity @Table(name = "product", catalog = "hibernate") public class Product implements java.io.Serializable {
private int prodid; private String prodname; private float prodprice;
public Product() { }
public Product(int prodid, String prodname, float prodprice) { this.prodid = prodid; this.prodname = prodname; this.prodprice = prodprice; }
@Id @Column(name = "PRODID", unique = true, nullable = false) public int getProdid() { return this.prodid; }
public void setProdid(int prodid) { this.prodid = prodid; }
@Column(name = "PRODNAME", nullable = false, length = 15) public String getProdname() { return this.prodname; }
public void setProdname(String prodname) { this.prodname = prodname; }
@Column(name = "PRODPRICE", nullable = false, precision = 12, scale = 0) public float getProdprice() { return this.prodprice; }
public void setProdprice(float prodprice) { this.prodprice = prodprice; }
}
ProductData.java
package net.roseindia;
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import net.roseindia.Product; import org.hibernate.util.*; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration;
public class ProductData {
public static void main(String[] args)throws Exception { // TODO Auto-generated method stub /** Getting the Session Factory and session */ AnnotationConfiguration config = new AnnotationConfiguration(); config.addAnnotatedClass(net.roseindia.Product.class); config.configure(); /*The SessionFactory is obtained through the config object*/ SessionFactory factory = config.buildSessionFactory(); Session session=factory.openSession(); Transaction tx=session.beginTransaction(); /** Creating Pojo */ Product pojo = new Product(); pojo.setProdid(new Integer(5)); pojo.setProdname("XYZ"); /** Saving POJO */ session.save(pojo); /** Commiting the changes */ tx.commit(); System.out.println("Record Inserted"); /** Closing Session */ session.close(); }
}
I am getting error like this:
Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.util.ReflectHelper.classForName(Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Class; at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:752) at net.roseindia.ProductData.main(ProductData.java:30)
|