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
Wie starten Sie Ihre Anwendung? –