2016-06-01 3 views
0

Product.javakönnte nicht auf die Datenbank über Hibernate ... zu frustating

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 verbinden. 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 an Konsole ist Fehler >>>

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 diesem .since 10 Tage löst Ich mag diese jedes Mal, um Code Hibernate Beispielanwendung versuchen es mir zeigt error..i ist nur ein Anfänger. ich wünsche u guys bitte diese lösen und erklären mir in einfachen Weg .THANK u für alle Vorschläge

+0

Wie starten Sie Ihre Anwendung? –

Antwort

3

die Ausnahme tatsächlich sagt Ihnen, was das Problem ist:

java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/MetadataProvider 

zur Laufzeit kann es nicht Finde die Klasse org.hibernate.annota maps.common.reflection.MetadataProvider. Sie müssen das JAR, das diese Klasse enthält, im Klassenpfad Ihrer Anwendung angeben. Ich weiß nicht, wie Ihr Setup ist, aber aus der gleichen Quelle, in der Sie Ihren Hibernate-Core-4.3.1.Final.jar bekommen haben, sollten Sie einen Hibernate-Commons-Annotations-4.0.4.Final bekommen können .jar (vorausgesetzt, Sie verwenden die neuesten Versionen). Rufen Sie es ab, legen Sie es in Ihren Klassenpfad, und die Ausnahme sollte verschwinden.

Verwandte Themen