Compare commits
7 Commits
5f279e3e8a
...
3c330e9800
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3c330e9800 | ||
|
|
47ee6f7ef1 | ||
|
|
eaba56b92d | ||
|
|
7f3157b7a2 | ||
|
|
5de8594095 | ||
|
|
3da2712bdf | ||
|
|
24eab5f2cc |
@@ -57,16 +57,20 @@
|
|||||||
<groupId>jakarta.persistence</groupId>
|
<groupId>jakarta.persistence</groupId>
|
||||||
<artifactId>jakarta.persistence-api</artifactId>
|
<artifactId>jakarta.persistence-api</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jakarta.validation</groupId>
|
||||||
|
<artifactId>jakarta.validation-api</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.mysql</groupId>
|
<groupId>com.mysql</groupId>
|
||||||
<artifactId>mysql-connector-j</artifactId>
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
<version>9.3.0</version>
|
<version>9.3.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jetbrains</groupId>
|
<groupId>org.reflections</groupId>
|
||||||
<artifactId>annotations</artifactId>
|
<artifactId>reflections</artifactId>
|
||||||
<version>RELEASE</version>
|
<version>0.10.2</version>
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.humanbooster;
|
package com.humanbooster;
|
||||||
|
|
||||||
|
import com.humanbooster.config.HibernateConfig;
|
||||||
import com.humanbooster.dao.AdDao;
|
import com.humanbooster.dao.AdDao;
|
||||||
import com.humanbooster.dao.ArticleDao;
|
import com.humanbooster.dao.ArticleDao;
|
||||||
import com.humanbooster.dao.UserDao;
|
import com.humanbooster.dao.UserDao;
|
||||||
@@ -15,6 +16,7 @@ import org.hibernate.boot.MetadataSources;
|
|||||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
@@ -24,13 +26,18 @@ public class App {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("Démarrage de l'application");
|
System.out.println("Démarrage de l'application");
|
||||||
|
|
||||||
|
SessionFactory sessionFactory;
|
||||||
|
|
||||||
|
if (HibernateConfig.LOCAL) sessionFactory = HibernateConfig.getSessionFactory();
|
||||||
|
|
||||||
|
else {
|
||||||
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
||||||
.configure()
|
.configure()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Metadata metadata = new MetadataSources(registry).buildMetadata();
|
Metadata metadata = new MetadataSources(registry).buildMetadata();
|
||||||
SessionFactory sessionFactory = metadata.buildSessionFactory();
|
sessionFactory = metadata.buildSessionFactory();
|
||||||
System.out.println("Connexion réussie !");
|
}
|
||||||
|
|
||||||
User user = new User("Bob", "bob@example.com", null);
|
User user = new User("Bob", "bob@example.com", null);
|
||||||
|
|
||||||
@@ -39,7 +46,23 @@ public class App {
|
|||||||
new Article("Article 2", "Contenu de l'article 2", LocalDate.now(), user, 0)
|
new Article("Article 2", "Contenu de l'article 2", LocalDate.now(), user, 0)
|
||||||
));
|
));
|
||||||
|
|
||||||
Ad ad = new Ad("Ad 1", "Contenu de l'annonce 1", LocalDate.now(), LocalDate.now().plusDays(7), "contact@example.com", 12);
|
List<Ad> ads = List.of(
|
||||||
|
(new Ad(
|
||||||
|
"Ad 1",
|
||||||
|
"Contenu de l'annonce 1",
|
||||||
|
LocalDate.now(),
|
||||||
|
LocalDate.now().plusDays(7),
|
||||||
|
"contact@example.com",
|
||||||
|
BigDecimal.valueOf(12))),
|
||||||
|
(new Ad(
|
||||||
|
"Ad 2",
|
||||||
|
"Contenu de l'annonce 2",
|
||||||
|
LocalDate.now(),
|
||||||
|
LocalDate.now().plusDays(10),
|
||||||
|
"contact@example.com",
|
||||||
|
BigDecimal.valueOf(6.7)))
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
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));
|
||||||
@@ -54,7 +77,7 @@ public class App {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
adService.createAd(ad);
|
ads.forEach(adService::createAd);
|
||||||
|
|
||||||
sessionFactory.close();
|
sessionFactory.close();
|
||||||
System.out.print("Fin du programme");
|
System.out.print("Fin du programme");
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.humanbooster.annotation;
|
||||||
|
|
||||||
|
public @interface Pair {
|
||||||
|
|
||||||
|
String message() default "Le nombre doit être pair";
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.humanbooster.annotation;
|
||||||
|
|
||||||
|
import jakarta.validation.ConstraintValidator;
|
||||||
|
import jakarta.validation.ConstraintValidatorContext;
|
||||||
|
|
||||||
|
public class PairValidator implements ConstraintValidator<Pair, Integer> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
|
||||||
|
return (integer != null) && (integer % 2 == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package com.humanbooster.config;
|
||||||
|
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.cfg.Configuration;
|
||||||
|
import org.reflections.Reflections;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
|
||||||
|
public class HibernateConfig {
|
||||||
|
private static final SessionFactory sessionFactory;
|
||||||
|
public static final boolean LOCAL = true;
|
||||||
|
private static String url;
|
||||||
|
private static String username;
|
||||||
|
private static String password;
|
||||||
|
|
||||||
|
|
||||||
|
static {
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -2,7 +2,9 @@ package com.humanbooster.model;
|
|||||||
|
|
||||||
import jakarta.persistence.Column;
|
import jakarta.persistence.Column;
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.validation.constraints.Email;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@@ -12,14 +14,15 @@ public class Ad extends Publication {
|
|||||||
private LocalDate expirationDate;
|
private LocalDate expirationDate;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
|
@Email
|
||||||
private String contactEmail;
|
private String contactEmail;
|
||||||
|
|
||||||
private int price;
|
private BigDecimal price;
|
||||||
|
|
||||||
public Ad() {
|
public Ad() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Ad(String title, String content, LocalDate publishDate, LocalDate expirationDate, String contactEmail, int price) {
|
public Ad(String title, String content, LocalDate publishDate, LocalDate expirationDate, String contactEmail, BigDecimal price) {
|
||||||
super(title, content, publishDate);
|
super(title, content, publishDate);
|
||||||
this.expirationDate = expirationDate;
|
this.expirationDate = expirationDate;
|
||||||
this.contactEmail = contactEmail;
|
this.contactEmail = contactEmail;
|
||||||
@@ -42,11 +45,11 @@ public class Ad extends Publication {
|
|||||||
this.contactEmail = contactEmail;
|
this.contactEmail = contactEmail;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPrice() {
|
public BigDecimal getPrice() {
|
||||||
return price;
|
return price;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPrice(int price) {
|
public void setPrice(BigDecimal price) {
|
||||||
this.price = price;
|
this.price = price;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ import jakarta.persistence.*;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
|
@Inheritance(strategy = InheritanceType.JOINED)
|
||||||
@DiscriminatorColumn
|
@DiscriminatorColumn
|
||||||
public abstract class Publication {
|
public abstract class Publication {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@Column(nullable = false, length = 200)
|
@Column(nullable = false, length = 200)
|
||||||
|
|||||||
@@ -12,9 +12,9 @@
|
|||||||
|
|
||||||
<property name="hibernate.hbm2ddl.auto">update</property>
|
<property name="hibernate.hbm2ddl.auto">update</property>
|
||||||
|
|
||||||
|
|
||||||
<property name="hibernate.show_sql">false</property>
|
<property name="hibernate.show_sql">false</property>
|
||||||
<property name="hibernate.format_sql">true</property>
|
<property name="hibernate.format_sql">true</property>
|
||||||
|
|
||||||
<mapping class="com.humanbooster.model.User"/>
|
<mapping class="com.humanbooster.model.User"/>
|
||||||
<mapping class="com.humanbooster.model.Article"/>
|
<mapping class="com.humanbooster.model.Article"/>
|
||||||
<mapping class="com.humanbooster.model.Ad"/>
|
<mapping class="com.humanbooster.model.Ad"/>
|
||||||
|
|||||||
22
hibernate-project/target/classes/hibernate.cfg.xml
Normal file
22
hibernate-project/target/classes/hibernate.cfg.xml
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?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="jakarta.persistence.jdbc.driver">com.mysql.cj.jdbc.Driver</property>
|
||||||
|
<property name="jakarta.persistence.jdbc.url">jdbc:mysql://mysql:3306/testdb?useSSL=false&allowPublicKeyRetrieval=true</property>
|
||||||
|
<property name="jakarta.persistence.jdbc.user">root</property>
|
||||||
|
<property name="jakarta.persistence.jdbc.password">root</property>
|
||||||
|
|
||||||
|
<property name="hibernate.hbm2ddl.auto">update</property>
|
||||||
|
|
||||||
|
|
||||||
|
<property name="hibernate.show_sql">false</property>
|
||||||
|
<property name="hibernate.format_sql">true</property>
|
||||||
|
<mapping class="com.humanbooster.model.User"/>
|
||||||
|
<mapping class="com.humanbooster.model.Article"/>
|
||||||
|
<mapping class="com.humanbooster.model.Ad"/>
|
||||||
|
</session-factory>
|
||||||
|
</hibernate-configuration>
|
||||||
Reference in New Issue
Block a user