Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate数据库操作错误_Java_Hibernate - Fatal编程技术网

Java Hibernate数据库操作错误

Java Hibernate数据库操作错误,java,hibernate,Java,Hibernate,我使用MyEclipse 10在Windows下可以正常运行,并且在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

我使用MyEclipse 10在Windows下可以正常运行,并且在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>
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(); 
            }
        }
    }
}

正确的目录,已经过测试

这是完整的stacktrace吗?我猜您的
静态
代码块正在抛出
运行时异常
,因此无法加载。您可能有数据库权限问题。HibernateUtil。Java是我处理的类,数据库初始化后,我应该如何重写该类?但是MyEclipse下的类运行完全没有错误
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; 
} 
} 
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);
        }
    }
}
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(); 
            }
        }
    }
}