Hibernate:无法加载请求的类

时间:2013-10-14 07:33:49

标签: java mysql hibernate h2

我正在使用Hibernate和mysql,但在运行时我得到以下异常:

Exception in thread "main" org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver org.h2.Driver class not found
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:107)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:77)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2283)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2279)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1748)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1788)
    at driver.Main.main(Main.java:29)
Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [org.h2.Driver]
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:141)
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:104)
    ... 14 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.h2.Driver
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$1.findClass(ClassLoaderServiceImpl.java:99)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:138)
    ... 15 more

的hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
   <property name="hibernate.dialect">
      org.hibernate.dialect.MySQLDialect
   </property>
   <property name="hibernate.connection.driver_class">
      com.mysql.jdbc.Driver
   </property>

   <property name="hibernate.connection.url">
      jdbc:mysql://localhost/ict
   </property>
   <property name="hibernate.connection.username">
      root
   </property>
   <property name="hibernate.connection.password">
      root
   </property>

</session-factory>
</hibernate-configuration>

我正在使用注释类,如

@Entity
@Table (name="method_msisdn_info")
public class MSISDN {

    public MSISDN(){}

    @Id @GeneratedValue @Column(name="msisdn_id")
    int msisdn_id;
    public int getMsisdn_id() {
        return msisdn_id;
    }
    public void setMsisdn_id(int msisdn_id) {
        this.msisdn_id = msisdn_id;
    }

    @Column(name="destination_msisdn")
    BigInteger destination_msisdn;
    public BigInteger getDestination_msisdn() {
        return destination_msisdn;
    }
    public void setDestination_msisdn(BigInteger destination_msisdn) {
        this.destination_msisdn = destination_msisdn;
    }

    @Column(name="product_list")
    String product_list;
    public String getProduct_list() {
        return product_list;
    }
    public void setProduct_list(String product_list) {
        this.product_list = product_list;
    }

    @Column(name="country")
    String country;
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }

    @Column(name="operator")
    String operator;
    public String getOperator() {
        return operator;
    }
    public void setOperator(String operator) {
        this.operator = operator;
    }

    @Column(name="destination_currency")
    String destination_currency;
    public String getDestination_currency() {
        return destination_currency;
    }
    public void setDestination_currency(String destination_currency) {
        this.destination_currency = destination_currency;
    }

    @Column(name="error_code")
    int error_code;
    public int getError_code() {
        return error_code;
    }
    public void setError_code(int error_code) {
        this.error_code = error_code;
    }

    @Column(name="wholesale_price_list")
    String wholesale_price_list;
    public String getWholesale_price_list() {
        return wholesale_price_list;
    }
    public void setWholesale_price_list(String wholesale_price_list) {
        this.wholesale_price_list = wholesale_price_list;
    }

    @Column (name ="retails_price_list")
    String retail_price_list;
    public String getRetail_price_list() {
        return retail_price_list;
    }
    public void setRetail_price_list(String retail_price_list) {
        this.retail_price_list = retail_price_list;
    }

    @Column(name="marked_processed")
    int marked_processed;
    public int getMarked_processed() {
        return marked_processed;
    }
    public void setMarked_processed(int marked_processed) {
        this.marked_processed = marked_processed;
    }
}

最后我创建了像

这样的会话
SessionFactory factory = new AnnotationConfiguration().addPackage("models").addAnnotatedClass(MSISDN.class).buildSessionFactory();
        Session session = factory.openSession();

2 个答案:

答案 0 :(得分:2)

我认为(但我不确定)像您一样创建Configuration对象需要指定配置xml文件。 试试这样(和我一起工作):

Configuration configuration = new AnnotationConfiguration().addPackage("models").addAnnotatedClass(MSISDN.class);
configuration.configure("hibernate.cfg.xml");
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();

无论如何,我注意到你使用了很多弃用的方法。我建议你使用不弃用的方法。

答案 1 :(得分:0)

它为类org.h2.Driver抛出ClassNotFoundException ..你有一个包含这个类的jar吗?检查http://www.jarfinder.com/index.php/java/info/org.h2.Driver