为什么我在Hibernate中出错?

时间:2016-02-12 11:06:34

标签: 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数据库。

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

<?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配置错误有关吗?

2 个答案:

答案 0 :(得分:2)

你得到ClassNotFoundException

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

答案 1 :(得分:1)

您必须在项目中添加mysql驱动程序jar。 如果您使用的是maven,只需在pom文件中添加依赖项:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
</dependency>

如果没有,请从mysql.com网站下载并安装jar。