gpt4 book ai didi

java - Hibernate:无法加载请求的类

转载 作者:行者123 更新时间:2023-11-29 08:21:16 24 4
gpt4 key购买 nike

我将 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;
}
}

最后我创建了 session

SessionFactory factory = new AnnotationConfiguration().addPackage("models").addAnnotatedClass(MSISDN.class).buildSessionFactory();
Session session = factory.openSession();

最佳答案

我认为(但我不确定)像您一样创建 Configuration 对象需要指定配置 xml 文件。尝试这样(对我来说它有效):

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

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

关于java - Hibernate:无法加载请求的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19355396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com