java - 为什么我在 Hibernate 中遇到错误?

标签 java hibernate

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to
 create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createServ
ice(AbstractServiceRegistryImpl.java:244)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initialize
Service(AbstractServiceRegistryImpl.java:208)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService
(AbstractServiceRegistryImpl.java:189)
        at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcSer
vicesImpl.java:51)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.conf
igureService(StandardServiceRegistryImpl.java:94)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initialize
Service(AbstractServiceRegistryImpl.java:217)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService
(AbstractServiceRegistryImpl.java:189)
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTy
pes(MetadataBuildingProcess.java:352)
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete
(MetadataBuildingProcess.java:111)
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(Me
tadataBuildingProcess.java:83)
        at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilder
Impl.java:418)
        at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilder
Impl.java:87)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.jav
a:692)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.jav
a:724)
        at Test.main(Test.java:14)
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: U
nable to load class [com.mysql.jdbc.Driver]
        at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceI
mpl.classForName(ClassLoaderServiceImpl.java:229)
        at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectio
nProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:161)

        at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectio
nProviderImpl.buildCreator(DriverManagerConnectionProviderImpl.java:117)
        at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectio
nProviderImpl.configure(DriverManagerConnectionProviderImpl.java:73)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.conf
igureService(StandardServiceRegistryImpl.java:94)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initialize
Service(AbstractServiceRegistryImpl.java:217)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService
(AbstractServiceRegistryImpl.java:189)
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.build
JdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initi
ateService(JdbcEnvironmentInitiator.java:66)
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initi
ateService(JdbcEnvironmentInitiator.java:35)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.init
iateService(StandardServiceRegistryImpl.java:88)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createServ
ice(AbstractServiceRegistryImpl.java:234)
        ... 14 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : co
m.mysql.jdbc.Driver
        at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceI
mpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:217)
        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.boot.registry.classloading.internal.ClassLoaderServiceI
mpl.classForName(ClassLoaderServiceImpl.java:226)
        ... 25 more

我正在尝试在 Hibernate 中构建我的简单应用程序。

我使用的是mysql数据库。

我已经编译了所有类......并使用命令提示符运行测试(包含主方法)类。

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  

<hibernate-configuration>  

    <session-factory>  
        <property name="hbm2ddl.auto">update</property>  
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
      <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

    <mapping resource="student.hbm.xml"/>  
    </session-factory>  

</hibernate-configuration>  

这与 hibernate 配置错误有关吗?

最佳答案

你得到一个ClassNotFoundException

com.mysql.jdbc.Driver ,您需要将 mysql 驱动程序 jar 添加到构建路径中。

关于java - 为什么我在 Hibernate 中遇到错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35361045/

相关文章:

java - 在另一台机器上运行 Spring Boot 应用程序

java - 双向 OneToMany 和 ManyToOne 给出 null

java - 如何从不同的存储库添加 Maven 依赖项

java - 如何在另一个 servlet 中传递值?

java - 正则表达式不匹配数据,但正则表达式已确认工作

java - 使用 ZipOutputStream 创建的 .war 文件无法部署

java - 如何为每个 RDD Spark Streaming

java - hibernate 可滚动结果立即获取mysql中的所有行

java - hibernate 中没有外键的一对多

java - @JoinColumn 注解中名称和引用列名的区别?