Refactor Hibernate configuration and update data factory methods for clarity
This commit is contained in:
@@ -14,10 +14,6 @@ import com.humanbooster.service.AdService;
|
|||||||
import com.humanbooster.service.ArticleService;
|
import com.humanbooster.service.ArticleService;
|
||||||
import com.humanbooster.service.UserService;
|
import com.humanbooster.service.UserService;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.boot.Metadata;
|
|
||||||
import org.hibernate.boot.MetadataSources;
|
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -34,18 +30,7 @@ public class App {
|
|||||||
System.out.println("Erreur lors du démarrage du serveur : " + e.getMessage());
|
System.out.println("Erreur lors du démarrage du serveur : " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
SessionFactory sessionFactory;
|
SessionFactory sessionFactory = new HibernateConfig(true).getSessionFactory();
|
||||||
|
|
||||||
if (HibernateConfig.isLocalEnvironment()) sessionFactory = HibernateConfig.getSessionFactory();
|
|
||||||
|
|
||||||
else {
|
|
||||||
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
|
||||||
.configure()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Metadata metadata = new MetadataSources(registry).buildMetadata();
|
|
||||||
sessionFactory = metadata.buildSessionFactory();
|
|
||||||
}
|
|
||||||
|
|
||||||
UserService userService = new UserService(new UserDao(sessionFactory));
|
UserService userService = new UserService(new UserDao(sessionFactory));
|
||||||
ArticleService articleService = new ArticleService(new ArticleDao(sessionFactory));
|
ArticleService articleService = new ArticleService(new ArticleDao(sessionFactory));
|
||||||
@@ -55,8 +40,8 @@ public class App {
|
|||||||
|
|
||||||
cleanDatabase(userService, articleService, adService);
|
cleanDatabase(userService, articleService, adService);
|
||||||
|
|
||||||
userService.createUser(dataFactory.createTestUser("Michel", "michel@test.fr"));
|
userService.createUser(dataFactory.createUser("Michel", "michel@test.fr"));
|
||||||
dataFactory.createTestAds().forEach(adService::createAd);
|
dataFactory.createAds().forEach(adService::createAd);
|
||||||
|
|
||||||
sessionFactory.close();
|
sessionFactory.close();
|
||||||
System.out.print("Fin du programme");
|
System.out.print("Fin du programme");
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package com.humanbooster.config;
|
package com.humanbooster.config;
|
||||||
|
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.boot.Metadata;
|
||||||
|
import org.hibernate.boot.MetadataSources;
|
||||||
|
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||||
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.reflections.Reflections;
|
import org.reflections.Reflections;
|
||||||
|
|
||||||
@@ -8,28 +12,22 @@ import jakarta.persistence.Entity;
|
|||||||
|
|
||||||
public class HibernateConfig {
|
public class HibernateConfig {
|
||||||
|
|
||||||
private static final SessionFactory sessionFactory;
|
private final boolean isLocalEnvironment;
|
||||||
private static final boolean LOCAL = true;
|
|
||||||
private static String url;
|
|
||||||
private static String username;
|
|
||||||
private static String password;
|
|
||||||
|
|
||||||
static {
|
public HibernateConfig(boolean isLocalEnvironment) {
|
||||||
|
this.isLocalEnvironment = isLocalEnvironment;
|
||||||
if (LOCAL) {
|
|
||||||
url = "jdbc:mysql://127.0.0.1:3306/testdb";
|
|
||||||
username = "admin";
|
|
||||||
password = "admin";
|
|
||||||
} else {
|
|
||||||
url = "jdbc:mysql://mysql:3306/testdb?useSSL=false&allowPublicKeyRetrieval=true";
|
|
||||||
username = "root";
|
|
||||||
password = "root";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SessionFactory getSessionFactory() {
|
||||||
|
|
||||||
|
SessionFactory sessionFactory;
|
||||||
|
|
||||||
|
if (isLocalEnvironment) {
|
||||||
|
|
||||||
Configuration config = new Configuration()
|
Configuration config = new Configuration()
|
||||||
.setProperty("hibernate.connection.url", url)
|
.setProperty("hibernate.connection.url", "jdbc:mysql://127.0.0.1:3306/testdb")
|
||||||
.setProperty("hibernate.connection.username", username)
|
.setProperty("hibernate.connection.username", "admin")
|
||||||
.setProperty("hibernate.connection.password", password)
|
.setProperty("hibernate.connection.password", "admin")
|
||||||
.setProperty("hibernate.connection.driver_class", "com.mysql.cj.jdbc.Driver")
|
.setProperty("hibernate.connection.driver_class", "com.mysql.cj.jdbc.Driver")
|
||||||
.setProperty("hibernate.hbm2ddl.auto", "update")
|
.setProperty("hibernate.hbm2ddl.auto", "update")
|
||||||
.setProperty("hibernate.show_sql", "false")
|
.setProperty("hibernate.show_sql", "false")
|
||||||
@@ -40,15 +38,16 @@ public class HibernateConfig {
|
|||||||
config.addAnnotatedClass(clazz);
|
config.addAnnotatedClass(clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionFactory = config.buildSessionFactory();
|
return sessionFactory = config.buildSessionFactory();
|
||||||
}
|
|
||||||
|
|
||||||
public static SessionFactory getSessionFactory() {
|
} else {
|
||||||
return sessionFactory;
|
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
||||||
}
|
.configure()
|
||||||
|
.build();
|
||||||
|
|
||||||
public static boolean isLocalEnvironment() {
|
Metadata metadata = new MetadataSources(registry).buildMetadata();
|
||||||
return LOCAL;
|
return sessionFactory = metadata.buildSessionFactory();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ public class ServerConfig extends ResourceConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void startServer() throws Exception {
|
public void startServer() throws Exception {
|
||||||
System.out.println("Starting server...");
|
System.out.println("Lancement du serveur...");
|
||||||
|
|
||||||
ResourceConfig config = this;
|
ResourceConfig config = this;
|
||||||
|
|
||||||
@@ -28,10 +28,10 @@ public class ServerConfig extends ResourceConfig {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
server.start();
|
server.start();
|
||||||
System.out.println("Server started on port 80");
|
System.out.println("Serveur démarré sur le port 80");
|
||||||
server.join();
|
server.join();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Failed to start server: " + e.getMessage());
|
System.out.println("Echec lors du lancement du serveur: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public class DataFactory {
|
public class DataFactory {
|
||||||
|
|
||||||
public User createTestUser(String name, String email) {
|
public User createUser(String name, String email) {
|
||||||
User user = new User(name, email, null);
|
User user = new User(name, email, null);
|
||||||
|
|
||||||
user.setArticles(List.of(
|
user.setArticles(List.of(
|
||||||
@@ -21,7 +21,7 @@ public class DataFactory {
|
|||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Ad> createTestAds() {
|
public List<Ad> createAds() {
|
||||||
return List.of(
|
return List.of(
|
||||||
(new Ad(
|
(new Ad(
|
||||||
"Ad 1",
|
"Ad 1",
|
||||||
|
|||||||
Reference in New Issue
Block a user