我正在努力使用Hibernate,JavaEE Persistence和嵌入式H2数据库。
我的persistence.xml中有以下部分:
<persistence-unit name="iot_pu">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.connection.url" value="jdbc:h2:/tmp/databases/iot;AUTO_SERVER=TRUE;MVCC=true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
我得到以下堆栈跟踪:
javax.persistence.PersistenceException: Unable to build entity manager factory
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:66)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at iot.utils.AbstractTestSuite.setUpClass(AbstractTestSuite.java:22)
at iot.utils.AbstractTestSuite.getEmf(AbstractTestSuite.java:37)
at iot.utils.AbstractTest.init
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
Caused by: org.h2.jdbc.JdbcSQLException: Falscher Benutzer Name oder Passwort
Wrong user name or password [28000-191]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.message.DbException.get(DbException.java:144)
at org.h2.engine.Engine.validateUserAndPassword(Engine.java:333)
at org.h2.engine.Engine.createSessionAndValidate(Engine.java:161)
at org.h2.engine.Engine.createSession(Engine.java:136)
at org.h2.engine.Engine.createSession(Engine.java:28)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:349)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:107)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:91)
at org.h2.Driver.connect
如果我测试与IntelliJ的连接,它表示连接没有问题 - 没有用户名或密码。
导致问题的行(AbstractTestSuite.java:22)说
Persistence.createEntityManagerFactory("iot_pu");
可能是这样吗,我忘记了通过Maven导入的内容了吗?
.........
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.191</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
.........
答案 0 :(得分:2)
您在hibernate.connection.username
中缺少hibernate.connection.password
和persistence.xml
个属性,或者您可以将它们直接插入到您的连接网址中(例如:"jdbc:oracle:thin:user/password@127.0.0.1:1521:SMIDVINT"
)。
答案 1 :(得分:0)
使用此方法测试您的连接,以确保您的设置正确无误:
/**
* Connect to H2db.
* calling example: connect("jdbc:h2:/Resources/databases/dbName", "JohnD", "secret")
*/
public static Connection connect (String dbPath, String userName, String password)
throws ClassNotFoundException, SQLException, JdbcSQLException {
Class.forName("org.h2.Driver");
String dbURL= dbPath + ";IGNORECASE=TRUE;MODE=MySQL;IFEXISTS=TRUE";
return DriverManager.getConnection(dbURL,userName,password);
}