Hello everyone. I’m quite a beginner using Hibernate but i’m using it for a javaFX application using my school’s Database. It’s a school project but I really want it to work. I know that the connection with the DB as i tested it and succesfuly inserted a user in my Table.
So I tried many things to fix the error like changing the mysql-connector-java version , changing the properties name removing the “hibernate.” or even changing the methods in the Hibernate building session class as you can see in my code at the very end. Thanks for the helm you will bring me and the is just below the different informations of my project.
The school server is a MySQL server on 5.7.28 Version.
I’m using the latest JDK ( jdk-19.0.1)
I’m working on Intellij Ultimate with Maven
There is my 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>project</groupId>
<artifactId>Application</artifactId>
<version>1.0-SNAPSHOT</version>
<name>applicationClavardageITJ</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.9.1</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>19</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>19</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.6.14.Final</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.naming</groupId>
<artifactId>jndi</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>19</source>
<target>19</target>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<executions>
<execution>
<!-- Default configuration for running with: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>project.application/project.application.App.App</mainClass>
<launcher>app</launcher>
<jlinkZipName>app</jlinkZipName>
<jlinkImageName>app</jlinkImageName>
<noManPages>true</noManPages>
<stripDebug>true</stripDebug>
<noHeaderFiles>true</noHeaderFiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
There is my module-info.java
module project.application {
requires javafx.controls;
requires javafx.fxml;
requires org.hibernate.orm.core;
requires java.naming;
requires java.sql;
requires mysql.connector.java;
requires jakarta.persistence;
opens project.application to javafx.fxml;
exports project.application.App;
opens project.application.App to javafx.fxml;
exports project.application.Controlers;
opens project.application.Controlers to javafx.fxml;
exports project.application.Manager;
opens project.application.Manager to javafx.fxml;
}
There is my configuration file hibernate.cfg.xml ( I tried to map with a configuration file and directly in my class with the jakarta.persistence API but both didn’t work
<?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>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://********.insa-toulouse.fr:3306/DBINSA</property>
<property name="connection.username">*******</property>
<property name="connection.password">********</property>
<property name="show_sql">true</property>
<property name="generate_statistics">true</property>
<mapping class="project.application.Models.Utilisateur"/>
<!-- <mapping resource="/Utilisateur.hbm.xml"/>-->
</session-factory>
</hibernate-configuration>
There is the Utilistaeur.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="project.application.Models.Utilisateur" table="Utilisateur">
<id name="idUser" type="java.lang.String" column="idUser">
<generator class="native"/>
</id>
<property name="password" type="String" column="password" not-null="true"/>
</class>
</hibernate-mapping>
There is the Utilisateur class
package project.application.Models;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.net.*;
@Entity
@Table(name="Utilisateur")
public class Utilisateur {
private String idUser;
private String password;
public String userPseudo;
private InetAddress ipUser;
//Constructeur pour créer un utilisateur sans attribut ( utile au lancement de l'app )
public Utilisateur() throws UnknownHostException {
this.ipUser = InetAddress.getLocalHost();
}
//Constructeur pour construire l autre utilisateur
public Utilisateur(String pseudoOtherUser,InetAddress adress){
this.ipUser = adress;
this.idUser = null;
this.password = null;
this.userPseudo = pseudoOtherUser;
}
//Constructeur pour construire l'utilisateur
public Utilisateur(String idUser,String pseudo){
try {
this.idUser = idUser;
this.userPseudo = pseudo;
this.ipUser = InetAddress.getLocalHost();
this.password = null;
}
catch(UnknownHostException e){
System.out.println("Erreur venant du constructeur Utilisateur"+e);
}
}
@Id
@Column(name="idUser")
public String getIdUser() {
return this.idUser;
}
public void setIdUser(String idUser) {
this.idUser = idUser;
}
public InetAddress getIpUser() {
return this.ipUser;
}
public void setIpUser(InetAddress ip){this.ipUser = ip;}
public String getUserPseudo() {
return userPseudo;
}
public void setUserPseudo(String newPseudo) {
this.userPseudo = newPseudo;
}
public void setId(String idUser){this.idUser = idUser;}
@Id
@Column(name="password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void erazeInfo(){
this.setPassword("");
this.setUserPseudo("");
}
}
And finaly there is my Hibernate class with the Session Builder
package project.application.Models;
import org.hibernate.SessionFactory;
import org.hibernate.MappingException;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class Hibernate {
public static void innit() throws Exception {
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build();
SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
}
private static SessionFactory buildSessionFactory(){
/*try{
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure("applicationClavardageITJ/src/main/resources/hibernate.cfg.xml").build();
Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build();
return metadata.getSessionFactoryBuilder().build();
} catch(Throwable Exception){
System.err.println("La création de la sessionFactory a échoué : "+Exception);
throw new ExceptionInInitializerError(Exception);
}*/
SessionFactory sessionFactory = null;
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
/*Configuration configuration = new Configuration().configure("/hibernate.cfg.xml");
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
serviceRegistryBuilder.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);*/
} catch (IllegalStateException e) {
System.out.println("Peut être problème de driver ");
e.printStackTrace();
}
return sessionFactory;
}
private static SessionFactory setUp(){
SessionFactory sessionFactory = null;
ServiceRegistry registery = new StandardServiceRegistryBuilder()
.configure()
.build();
try{
System.out.println("Building sessionsFactory");
sessionFactory = new MetadataSources(registery)
.buildMetadata()
.buildSessionFactory();
}
catch (Exception e){
StandardServiceRegistryBuilder.destroy(registery);
e.printStackTrace();
}
return sessionFactory;
}
public static SessionFactory getSessionFactory(){
return setUp();
}
public static void shutdown(){
getSessionFactory().close();
}
}
And Finaly the Exception StackTrace ( i just removed the lines from JavaFX )
janv. 20, 2023 6:10:05 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH10001115: Connection pool size: 20 (min=1)
janv. 20, 2023 6:10:05 PM org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata
java.lang.IllegalStateException: Cannot get a connection as the driver manager is not properly initialized