休眠:无法加载请求的类
Posted
技术标签:
【中文标题】休眠:无法加载请求的类【英文标题】:Hibernate: could not load requested class 【发布时间】:2013-10-14 07:33:48 【问题描述】:我在 mysql 中使用 Hibernate,但在运行时出现以下异常:
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();
【问题讨论】:
详细介绍一下如何配置持久性。看起来应用程序正在尝试加载 H2 db 驱动程序,而您似乎在 hibernate cfg 中配置了 mySql 驱动程序。您的程序似乎没有使用您在此处发布的 hibernate.cfg.xml。可能你的类路径上有该文件的另一个版本。 我将 hibernate.cfg.xml 放在了 src 文件夹中。 当您运行应用程序时,该文件是否也在您的类路径中?例如。如果你使用的是maven,你最好把它放在resources文件夹中。 不,我没有使用 maven。是的,当我运行时,(完全相同)hibernate.cfg.xml 会在 bin 目录中创建,即它确实在二进制包中。 但是你是对的,因为无论我将 hibernate.cfg.xml 放在哪里,都会发生同样的错误(这意味着它不会抱怨找不到 cfg.xml)。但我仍然不知道如何纠正错误。 【参考方案1】:我认为(但我不确定)像您一样创建 Configuration
对象需要指定配置 xml 文件。
像这样尝试(对我来说它有效):
Configuration configuration = new AnnotationConfiguration().addPackage("models").addAnnotatedClass(MSISDN.class);
configuration.configure("hibernate.cfg.xml");
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
无论如何,我注意到您使用了很多已弃用的方法。我会建议你使用未弃用的方法。
【讨论】:
我解决了。尽管我没有像您那样指定 cfg.xml 文件,而是单独指定了属性。问题的原因是,hibernate 正在从我添加到类路径的 hibernate jar 中的全局 hibernate.properties 文件中选择数据库、方言和其他属性。【参考方案2】:它为类 org.h2.Driver 抛出 ClassNotFoundException .. 你有一个包含这个类的 jar 吗?检查http://www.jarfinder.com/index.php/java/info/org.h2.Driver
【讨论】:
它不应该要求那个类,因为我使用的是 mysql 而不是 h2。以上是关于休眠:无法加载请求的类的主要内容,如果未能解决你的问题,请参考以下文章
休眠延迟加载不适用于 Spring Boot => 无法延迟初始化角色集合无法初始化代理 - 无会话