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