i'm facing a problem with connection to MySQL database by java. I've tried to change versions in pom.xml of hibernate-core/hibernate-entitymanager/hibernate-jpa and nothing worked.
It seems that it is a problem with hibernate-jpa duplication or it not working with mysql-connecot. After 2 hours of trying all these different versions i've decided to ask for help.
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>Pracownia</groupId>
<artifactId>PracowniaMain</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<resources>
<resource>
<directory>src</directory>
<includes>
<include>META-INF/persistence.xml</include>
</includes>
</resource>
</resources>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<attach>false</attach>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>
hibernate.Manager
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Hibernate resources -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.2.Final</version>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<!-- Java Bind -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
</project>
persistence.xml:
<!--<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">-->
<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="hibernate-dynamic" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!--<class>org.halyph.sessiondemo.Event</class>-->
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/phpmyadmin?useSSL=false" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.connection.driver_class" value ="com.mysql.cj.jdbc.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
Then after running this:
package hibernate;
import hibernate.model.artysci;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.List;
class Manager {
public static void main(String[] args) {
System.out.println("Start");
EntityManager entityManager = null;
EntityManagerFactory entityManagerFactory = null;
System.out.println("Start2");
try {
System.out.println("Start3");
//taka nazwa jak w persistence.xml
entityManagerFactory = Persistence.createEntityManagerFactory("hibernate-dynamic");
//utworz entityManagera
System.out.println("Start4");
entityManager = entityManagerFactory.createEntityManager();
//rozpocznij transakcje
entityManager.getTransaction().begin();
//System.out.println("dis");
/*
Employee emp = new Employee();
emp.setFirstName("Jan");
emp.setLastName("Polak");
emp.setSalary(100);
emp.setPesel(100);
entityManager.persist(emp);
Employee employee = entityManager.find(Employee.class, emp.getId());
entityManager.remove(emp);
System.out.println("Employee " + employee.getId() + " " + employee.getFirstName() + employee.getLastName());
*/
//zakoncz transakcje
entityManager.getTransaction().commit();
System.out.println("Done");
entityManager.close();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
} finally {
entityManagerFactory.close();
}
}
}
Console:
Start
Start2
Start3
2021-01-06 18:52:56 DEBUG logging:38 - Logging Provider: org.jboss.logging.Log4jLoggerProvider
2021-01-06 18:52:56 INFO LogHelper:31 - HHH000204: Processing PersistenceUnitInfo [
name: hibernate-dynamic
...]
2021-01-06 18:52:57 INFO Version:37 - HHH000412: Hibernate Core {5.2.0.Final}
2021-01-06 18:52:57 INFO Environment:213 - HHH000206: hibernate.properties not found
2021-01-06 18:52:57 INFO Environment:318 - HHH000021: Bytecode provider name : javassist
2021-01-06 18:52:57 INFO Version:66 - HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2021-01-06 18:52:57 WARN connections:71 - HHH10001002: Using Hibernate built-in connection pool (not for production use!)
2021-01-06 18:52:57 INFO connections:127 - HHH10001005: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://localhost/phpmyadmin?useSSL=false]
2021-01-06 18:52:57 INFO connections:136 - HHH10001001: Connection properties: {user=root}
2021-01-06 18:52:57 INFO connections:141 - HHH10001003: Autocommit mode: false
2021-01-06 18:52:57 INFO DriverManagerConnectionProviderImpl:39 - HHH000115: Hiberna
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…