Hibernate IntelliJ-无法加载类[org.postgresql.Driver]

时间:2018-09-20 11:28:02

标签: java postgresql hibernate intellij-idea

我想在intellij中设置的psql数据库之上运行自己的Hibernate应用程序:

As you can see, I have the recent Postgresql Driver.

https://jdbc.postgresql.org/documentation/93/load.html 不能完全解释应该如何配置xml ...

这是我得到的错误:

/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/bin/java --add-modules java.xml.bind "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=50395:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/selfishound/Desktop/DBPRAK/3/Testat33/out/production/Testat33:/Users/selfishound/Desktop/DBPRAK/3/Testat33/lib/hibernate-commons-annotations-5.0.1.Final.jar:/Users/selfishound/Desktop/DBPRAK/3/Testat33/lib/dom4j-1.6.1.jar:/Users/selfishound/Desktop/DBPRAK/3/Testat33/lib/classmate-1.3.0.jar:/Users/selfishound/Desktop/DBPRAK/3/Testat33/lib/hibernate-jpa-2.1-api-1.0.0.Final.jar:/Users/selfishound/Desktop/DBPRAK/3/Testat33/lib/jandex-2.0.3.Final.jar:/Users/selfishound/Desktop/DBPRAK/3/Testat33/lib/antlr-2.7.7.jar:/Users/selfishound/Desktop/DBPRAK/3/Testat33/lib/jboss-logging-3.3.1.Final.jar:/Users/selfishound/Desktop/DBPRAK/3/Testat33/lib/jboss-transaction-api_1.2_spec-1.0.1.Final.jar:/Users/selfishound/Desktop/DBPRAK/3/Testat33/lib/javassist-3.22.0-GA.jar:/Users/selfishound/Desktop/DBPRAK/3/Testat33/lib/hibernate-core-5.2.16.Final.jar Main
Sep 20, 2018 1:21:51 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.16.Final}
Sep 20, 2018 1:21:51 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 20, 2018 1:21:52 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Failed to create sessionFactory object.
Sep 20, 2018 1:21:55 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
    at SessionFactoryClass.<init>(SessionFactoryClass.java:15)
    at PersonRelatedImpl.<init>(PersonRelatedImpl.java:16)
    at Menu.<init>(Menu.java:9)
    at Main.main(Main.java:4)
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.postgresql.Driver]
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:348)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:163)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator(DriverManagerConnectionProviderImpl.java:119)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:103)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:75)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
    ... 17 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.postgresql.Driver
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:336)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:563)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:375)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:345)
    ... 29 more

我在这里做错了什么?我尝试将驱动程序重命名为org.postgresql.Driver.jar并将jar移至根目录,但这无济于事……这可能与IntelliJ的配置有关,但这对我来说很好:{{3 }}

1 个答案:

答案 0 :(得分:1)

IDE数据源在任何方面都与您的应用程序无关,包括为其配置jdbc驱动程序的方式。您需要将postgres jdbc驱动程序jar添加到module's dependencies中。请注意,如果您使用的是Java 9模块,则可能需要通过module-info文件添加它,请参见How to use 3rd party library in Java9 module?