我正在升级到Hibernate 5和JPA,并且无法连接到MySQL Server 5.7.18(Java 8,ojdbc8)。从命令行使用mysql我可以很好地连接和访问数据库,所以我知道服务器已启动,用户/密码是正确的。 PeopleEntity.class使用注释而不是映射文件。
例外是“无法创建请求的服务[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]”。我试过“jdbc:oracle:thin:@localhost:3306:TestHibernate”和“jdbc:mysql:// localhost:3306 / TestHibernate”
任何有关如何纠正问题的建议都将不胜感激。
final Map<String, String> properties = new HashMap<String, String>();
properties.put("javax.persistence.jdbc.user", LoginInfo.getLoginInfo().getUsername());
properties.put("javax.persistence.jdbc.password", LoginInfo.getLoginInfo().getPassword());
properties.put("javax.persistence.jdbc.url", "jdbc:oracle:thin:@localhost:3306:TestHibernate");
// or
properties.put("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/TestHibernate");
try {
Persistence.createEntityManagerFactory("PeopleEntity", properties);
} catch (final PersistenceException error) {
Logger.error(getClass(), error.getMessage());
}
来自persistence.xml
<persistence
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit
name="PeopleEntity"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.billsdesk.util.hibernate.tests.PeopleEntity</class>
<properties>
<property
name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver"></property>
<property
name="javax.persistence.jdbc.driver"
value="oracle.jdbc.OracleDriver"></property>
<property
name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5Dialect" />
<!-- URL, user, and password are passed as properties -->
</properties>
</persistence-unit>
</persistence>
来自pom.xml
<dependencies>
<!-- Hibernate -->
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>
Hibernate跟踪
14:00:45.861 [main] INFO org.hibernate.Version - HHH000412: Hibernate Core {5.2.10.Final}
14:00:45.862 [main] INFO org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
14:00:45.910 [main] INFO org.hibernate.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
14:00:46.088 [main] WARN org.hibernate.orm.connections.pooling - HHH10001002: Using Hibernate built-in connection pool (not for production use!)
14:00:46.127 [main] INFO org.hibernate.orm.connections.pooling - HHH10001005: using driver [oracle.jdbc.OracleDriver] at URL [jdbc:oracle:thin:@localhost:3306:TestHibernate]
14:00:46.127 [main] INFO org.hibernate.orm.connections.pooling - HHH10001001: Connection properties: {user=sa, password=****}
14:00:46.128 [main] INFO org.hibernate.orm.connections.pooling - HHH10001003: Autocommit mode: false
14:00:46.130 [main] INFO org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000115: Hibernate connection pool size: 20 (min=1)
2017-07-06 14:00:46 SEVERE Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
答案 0 :(得分:0)
尽管MySQL是Oracle Corporation的产品,但“Oracle”数据库产品和“MySQL”数据库产品是两种非常不同的生物。使用“Oracle”JDBC驱动程序永远不会与MySQL数据库服务器一起使用。
作为一个最小的实现,我得到以下只使用四(4)个文件。它可能不是“最好的”或最“现代”的方式,但它对我有用。
<强>的pom.xml 强>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.demo</groupId>
<artifactId>so44959525</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
</project>
<强>的src /主/资源/ hibernate.cfg.xml中强>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection properties -->
<property name="connection.url">jdbc:mysql://localhost:3307/mydb</property>
<property name="connection.username">root</property>
<property name="connection.password">whatever</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--
Automatic schema generation. Possible values:
none (default value), create-only, drop, create, create-drop, validate, and update
https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#configurations-hbmddl
-->
<property name="hbm2ddl.auto">create</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<mapping class="com.example.demo.Person"/>
</session-factory>
</hibernate-configuration>
<强>的src /主/ JAVA / COM /示例/演示/ Person.java 强>
package com.example.demo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
public int getId() { return id; }
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Person() {
// no-argument constructor required by Hibernate
}
public Person (String name) {
this.name = name;
}
}
<强>的src /主/ JAVA / COM /示例/演示/ DemoMain.java 强>
package com.example.demo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class DemoMain {
public static void main(String[] args) {
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
try (SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory()) {
Session session = sessionFactory.openSession();
session.beginTransaction();
for (String newGuy : new String[] { "Rodd", "Todd" }) {
int newId = (int) session.save(new Person(newGuy));
System.out.printf("\"%s\" inserted as id %d%n", newGuy, newId);
}
session.getTransaction().commit();
session.close();
} catch (Exception e) {
e.printStackTrace(System.err);
} finally {
StandardServiceRegistryBuilder.destroy(registry);
}
}
}