我在Windows下使用MyEclipse 10可以正常运行,并在Linux上运行,以便在出错时使用Eclipse。我将DTD下载到本地(对于Linux无法访问hibernate DTD地址)。
我只是想知道为什么这是错的?谢谢
ERROR INFO:
<code>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Sep 02, 2013 11:42:55 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.5.Final}
Sep 02, 2013 11:42:55 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: `{hibernate.connection.driver_class=org.h2.Driver, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5}`
Sep 02, 2013 11:42:55 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 02, 2013 11:42:55 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Sep 02, 2013 11:42:55 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Sep 02, 2013 11:42:55 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cdd/model/Medicine.hbm.xml
Sep 02, 2013 11:42:55 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.cdd.util.HibernateUtil
at com.cdd.util.Delete.main(Delete.java:30)
Medicine medicine = (Medicine) session.load(Medicine.class,new Integer(1));
</code>
的hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"/home/../dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/Test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/cdd/model/Medicine.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Medicine.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"/home/puruidong/JavaEEworkspace/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.cdd.model.Medicine" table="tb_medicine">
<id name="id">
<generator class="native"/>
</id>
<property name="name" not-null="true" length="200" />
<property name="price" not-null="true"/>
<property name="factoryAdd" length="200"/>
<property name="description" type="text"/>
</class>
</hibernate-mapping>
Medicine.java
Package the CDD. Model;
Public class Medicine {
Private Integer id; / / id no.
Private String name; / / the name of the drug
Private double price; / / price
Private String factoryAdd; / / the factory address
Private String Description; / / description
Public Integer getId () {
Return the id;
}
Public void setId (Integer id) {
This. Id = id;
}
Public String getName () {
Return the name;
}
Public void elegantly-named setName (String name) {
This. Name = name;
}
Public double getPrice () {
The return price;
}
Public void setPrice (double price) {
This. Price = price;
}
Public String getFactoryAdd () {
Return factoryAdd;
}
Public void setFactoryAdd (String factoryAdd) {
Enclosing factoryAdd = factoryAdd;
}
Public String getDescription () {
Return the Description;
}
Public void setDescription (String description) {
Description = the Description;
}
}
Delete.java
package com.cdd.util;
import com.cdd.util.HibernateUtil;
import org.hibernate.Session;
import com.cdd.model.Medicine;
public class Delete {
public static void main(String[] args) {
Session session = null;
try {
session = HibernateUtil.getSession();
session.beginTransaction();
Medicine medicine = (Medicine)session.load(Medicine.class, new Integer(1));
session.delete(medicine);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtil.closeSession(session);
}
}
}
HibernateUtil.java
package com.cdd.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory factory = null;
static {
try {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
}
}
public static Session getSession() {
Session session = (factory != null) ? factory.openSession() : null;
return session;
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
}
目录是正确的,已经过测试。