feur
This commit is contained in:
43
pom.xml
43
pom.xml
@@ -13,5 +13,48 @@
|
|||||||
<maven.compiler.target>25</maven.compiler.target>
|
<maven.compiler.target>25</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<!-- JPA API -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>jakarta.persistence</groupId>
|
||||||
|
<artifactId>jakarta.persistence-api</artifactId>
|
||||||
|
<version>3.2.0</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Implementation JPA (Hibernate) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate</groupId>
|
||||||
|
<artifactId>hibernate-core</artifactId>
|
||||||
|
<version>7.1.11.Final</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Base de donnees PostgreSQL -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.postgresql</groupId>
|
||||||
|
<artifactId>postgresql</artifactId>
|
||||||
|
<version>42.7.8</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.42</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.42</version>
|
||||||
|
</path>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -1,17 +1,33 @@
|
|||||||
package bzh.risotto;
|
package bzh.risotto;
|
||||||
|
|
||||||
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
|
import bzh.risotto.dto.ClientDTO;
|
||||||
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
|
import bzh.risotto.service.ClientService;
|
||||||
public class Main {
|
import jakarta.persistence.EntityManager;
|
||||||
static void main() {
|
import jakarta.persistence.EntityManagerFactory;
|
||||||
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
|
import jakarta.persistence.Persistence;
|
||||||
// to see how IntelliJ IDEA suggests fixing it.
|
import java.util.Date;
|
||||||
IO.println(String.format("Hello and welcome!"));
|
|
||||||
|
|
||||||
for (int i = 1; i <= 5; i++) {
|
public class Main {
|
||||||
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
|
public static void main(String[] args) {
|
||||||
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
|
EntityManagerFactory emf = Persistence.createEntityManagerFactory("banquePU");
|
||||||
IO.println("i = " + i);
|
EntityManager em = emf.createEntityManager();
|
||||||
}
|
|
||||||
|
ClientService clientService = new ClientService(em);
|
||||||
|
|
||||||
|
// Exemple de creation d'un client
|
||||||
|
ClientDTO clientDTO = new ClientDTO();
|
||||||
|
clientDTO.setNumClient(1);
|
||||||
|
clientDTO.setNomClient("Dupont");
|
||||||
|
clientDTO.setPrenomClient("Jean");
|
||||||
|
clientDTO.setAdClient("123 Rue de Paris");
|
||||||
|
clientDTO.setDateNaissClient(new Date());
|
||||||
|
clientDTO.setAgeClient(30);
|
||||||
|
//clientDTO.setNumAgent(1);
|
||||||
|
|
||||||
|
ClientDTO createdClient = clientService.createClient(clientDTO);
|
||||||
|
System.out.println("Client cree: " + createdClient.getNomClient());
|
||||||
|
|
||||||
|
em.close();
|
||||||
|
emf.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
72
src/main/java/bzh/risotto/dao/AbstractDAO.java
Normal file
72
src/main/java/bzh/risotto/dao/AbstractDAO.java
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package bzh.risotto.dao;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.EntityTransaction;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public abstract class AbstractDAO<T, K> implements DAO<T, K> {
|
||||||
|
private final Class<T> entityClass;
|
||||||
|
protected EntityManager entityManager;
|
||||||
|
|
||||||
|
public AbstractDAO(Class<T> entityClass, EntityManager entityManager) {
|
||||||
|
this.entityClass = entityClass;
|
||||||
|
this.entityManager = entityManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<T> find(K id) {
|
||||||
|
return Optional.ofNullable(entityManager.find(entityClass, id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<T> findAll() {
|
||||||
|
return entityManager.createQuery("SELECT e FROM " + entityClass.getSimpleName() + " e", entityClass)
|
||||||
|
.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(T entity) {
|
||||||
|
EntityTransaction transaction = entityManager.getTransaction();
|
||||||
|
try {
|
||||||
|
transaction.begin();
|
||||||
|
entityManager.persist(entity);
|
||||||
|
transaction.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (transaction != null && transaction.isActive()) {
|
||||||
|
transaction.rollback();
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(T entity) {
|
||||||
|
EntityTransaction transaction = entityManager.getTransaction();
|
||||||
|
try {
|
||||||
|
transaction.begin();
|
||||||
|
entityManager.merge(entity);
|
||||||
|
transaction.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (transaction != null && transaction.isActive()) {
|
||||||
|
transaction.rollback();
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(T entity) {
|
||||||
|
EntityTransaction transaction = entityManager.getTransaction();
|
||||||
|
try {
|
||||||
|
transaction.begin();
|
||||||
|
entityManager.remove(entityManager.contains(entity) ? entity : entityManager.merge(entity));
|
||||||
|
transaction.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (transaction != null && transaction.isActive()) {
|
||||||
|
transaction.rollback();
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/main/java/bzh/risotto/dao/ClientDAO.java
Normal file
19
src/main/java/bzh/risotto/dao/ClientDAO.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package bzh.risotto.dao;
|
||||||
|
|
||||||
|
import bzh.risotto.model.Client;
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ClientDAO extends AbstractDAO<Client, Integer> {
|
||||||
|
public ClientDAO(EntityManager entityManager) {
|
||||||
|
super(Client.class, entityManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Client> findByAgent(int numAgent) {
|
||||||
|
return entityManager.createQuery(
|
||||||
|
"SELECT c FROM Client c WHERE c.agent.numAgent = :numAgent", Client.class)
|
||||||
|
.setParameter("numAgent", numAgent)
|
||||||
|
.getResultList();
|
||||||
|
}
|
||||||
|
}
|
||||||
12
src/main/java/bzh/risotto/dao/DAO.java
Normal file
12
src/main/java/bzh/risotto/dao/DAO.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package bzh.risotto.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface DAO<T, K> {
|
||||||
|
Optional<T> find(K id);
|
||||||
|
List<T> findAll();
|
||||||
|
void save(T entity);
|
||||||
|
void update(T entity);
|
||||||
|
void delete(T entity);
|
||||||
|
}
|
||||||
18
src/main/java/bzh/risotto/dto/ClientDTO.java
Normal file
18
src/main/java/bzh/risotto/dto/ClientDTO.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package bzh.risotto.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ClientDTO {
|
||||||
|
private int numClient;
|
||||||
|
private String nomClient;
|
||||||
|
private String prenomClient;
|
||||||
|
private String adClient;
|
||||||
|
private Date dateNaissClient;
|
||||||
|
private int ageClient;
|
||||||
|
private int numAgent;
|
||||||
|
|
||||||
|
// Constructeurs, getters et setters
|
||||||
|
}
|
||||||
30
src/main/java/bzh/risotto/mapper/ClientMapper.java
Normal file
30
src/main/java/bzh/risotto/mapper/ClientMapper.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package bzh.risotto.mapper;
|
||||||
|
|
||||||
|
import bzh.risotto.dto.ClientDTO;
|
||||||
|
import bzh.risotto.model.Client;
|
||||||
|
|
||||||
|
public class ClientMapper {
|
||||||
|
public static ClientDTO toDTO(Client client) {
|
||||||
|
ClientDTO dto = new ClientDTO();
|
||||||
|
dto.setNumClient(client.getNumClient());
|
||||||
|
dto.setNomClient(client.getNomClient());
|
||||||
|
dto.setPrenomClient(client.getPrenomClient());
|
||||||
|
dto.setAdClient(client.getAdClient());
|
||||||
|
dto.setDateNaissClient(client.getDateNaissClient());
|
||||||
|
dto.setAgeClient(client.getAgeClient());
|
||||||
|
dto.setNumAgent(client.getAgent() != null ? client.getAgent().getNumAgent() : 0);
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Client toEntity(ClientDTO dto) {
|
||||||
|
Client client = new Client();
|
||||||
|
client.setNumClient(dto.getNumClient());
|
||||||
|
client.setNomClient(dto.getNomClient());
|
||||||
|
client.setPrenomClient(dto.getPrenomClient());
|
||||||
|
client.setAdClient(dto.getAdClient());
|
||||||
|
client.setDateNaissClient(dto.getDateNaissClient());
|
||||||
|
client.setAgeClient(dto.getAgeClient());
|
||||||
|
// Note: La relation avec Agent doit etre geree separement
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
}
|
||||||
21
src/main/java/bzh/risotto/model/Agence.java
Normal file
21
src/main/java/bzh/risotto/model/Agence.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package bzh.risotto.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
@Table(name = "Agence")
|
||||||
|
public class Agence {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(name = "numAgence")
|
||||||
|
private int numAgence;
|
||||||
|
|
||||||
|
@Column(name = "telAgence")
|
||||||
|
private String telAgence;
|
||||||
|
|
||||||
|
@Column(name = "adAgence")
|
||||||
|
private String adAgence;
|
||||||
|
}
|
||||||
27
src/main/java/bzh/risotto/model/Agent.java
Normal file
27
src/main/java/bzh/risotto/model/Agent.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package bzh.risotto.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
@Table(name = "Agent")
|
||||||
|
public class Agent {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(name = "numAgent")
|
||||||
|
private int numAgent;
|
||||||
|
|
||||||
|
@Column(name = "nomAgent")
|
||||||
|
private String nomAgent;
|
||||||
|
|
||||||
|
@Column(name = "prenomAgent")
|
||||||
|
private String prenomAgent;
|
||||||
|
|
||||||
|
@Column(name = "salaire")
|
||||||
|
private double salaire;
|
||||||
|
|
||||||
|
@Column(name = "estDirecteur")
|
||||||
|
private boolean estDirecteur;
|
||||||
|
}
|
||||||
46
src/main/java/bzh/risotto/model/Client.java
Normal file
46
src/main/java/bzh/risotto/model/Client.java
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package bzh.risotto.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
@Table(name = "Client")
|
||||||
|
public class Client {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(name = "numClient")
|
||||||
|
private int numClient;
|
||||||
|
|
||||||
|
@Column(name = "nomClient", nullable = false)
|
||||||
|
private String nomClient;
|
||||||
|
|
||||||
|
@Column(name = "prenomClient", nullable = false)
|
||||||
|
private String prenomClient;
|
||||||
|
|
||||||
|
@Column(name = "adClient")
|
||||||
|
private String adClient;
|
||||||
|
|
||||||
|
@Column(name = "dateNaissClient")
|
||||||
|
@Temporal(TemporalType.DATE)
|
||||||
|
private Date dateNaissClient;
|
||||||
|
|
||||||
|
@Column(name = "ageClient")
|
||||||
|
private int ageClient;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "numAgent", referencedColumnName = "numAgent")
|
||||||
|
private Agent agent;
|
||||||
|
|
||||||
|
@ManyToMany
|
||||||
|
@JoinTable(
|
||||||
|
name = "Compte_Client",
|
||||||
|
joinColumns = @JoinColumn(name = "numClient"),
|
||||||
|
inverseJoinColumns = @JoinColumn(name = "numCompte")
|
||||||
|
)
|
||||||
|
private List<Compte> comptes;
|
||||||
|
|
||||||
|
// Constructeurs, getters et setters
|
||||||
|
}
|
||||||
26
src/main/java/bzh/risotto/model/Compte.java
Normal file
26
src/main/java/bzh/risotto/model/Compte.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package bzh.risotto.model;
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "Compte")
|
||||||
|
public class Compte {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(name = "numCompte")
|
||||||
|
private int numCompte;
|
||||||
|
|
||||||
|
@Column(name = "solde")
|
||||||
|
private double solde;
|
||||||
|
|
||||||
|
@Column(name = "typeCompte", nullable = false)
|
||||||
|
private String typeCompte;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "compte", cascade = CascadeType.ALL)
|
||||||
|
private List<Operation> operations;
|
||||||
|
|
||||||
|
@ManyToMany(mappedBy = "comptes")
|
||||||
|
private List<Client> clients;
|
||||||
|
|
||||||
|
// Constructeurs, getters et setters
|
||||||
|
}
|
||||||
31
src/main/java/bzh/risotto/model/Operation.java
Normal file
31
src/main/java/bzh/risotto/model/Operation.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package bzh.risotto.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.sql.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
@Table(name = "Operation")
|
||||||
|
public class Operation {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(name = "numOperation")
|
||||||
|
private int numOperation;
|
||||||
|
|
||||||
|
@Column(name = "dateOperation")
|
||||||
|
private Date dateOperation;
|
||||||
|
|
||||||
|
@Column(name = "typeOperation")
|
||||||
|
private String typeOperation;
|
||||||
|
|
||||||
|
@Column(name = "montant")
|
||||||
|
private double montant;
|
||||||
|
|
||||||
|
@OneToOne()
|
||||||
|
@JoinColumn(name = "numCompte", referencedColumnName = "numCompte")
|
||||||
|
private Compte compte;
|
||||||
|
}
|
||||||
41
src/main/java/bzh/risotto/service/ClientService.java
Normal file
41
src/main/java/bzh/risotto/service/ClientService.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package bzh.risotto.service;
|
||||||
|
|
||||||
|
import bzh.risotto.dao.ClientDAO;
|
||||||
|
import bzh.risotto.dto.ClientDTO;
|
||||||
|
import bzh.risotto.mapper.ClientMapper;
|
||||||
|
import bzh.risotto.model.Client;
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class ClientService {
|
||||||
|
private final ClientDAO clientDAO;
|
||||||
|
|
||||||
|
public ClientService(EntityManager entityManager) {
|
||||||
|
this.clientDAO = new ClientDAO(entityManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientDTO createClient(ClientDTO clientDTO) {
|
||||||
|
Client client = ClientMapper.toEntity(clientDTO);
|
||||||
|
clientDAO.save(client);
|
||||||
|
return ClientMapper.toDTO(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientDTO getClient(int id) {
|
||||||
|
return clientDAO.find(id)
|
||||||
|
.map(ClientMapper::toDTO)
|
||||||
|
.orElseThrow(() -> new RuntimeException("Client not found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ClientDTO> getAllClients() {
|
||||||
|
return clientDAO.findAll().stream()
|
||||||
|
.map(ClientMapper::toDTO)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ClientDTO> getClientsByAgent(int numAgent) {
|
||||||
|
return clientDAO.findByAgent(numAgent).stream()
|
||||||
|
.map(ClientMapper::toDTO)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/main/resources/META-INF/persistence.xml
Normal file
20
src/main/resources/META-INF/persistence.xml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<persistence version="3.1" xmlns="https://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_1.xsd">
|
||||||
|
<persistence-unit name="banquePU" transaction-type="RESOURCE_LOCAL">
|
||||||
|
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||||
|
<class>bzh.risotto.model.Client</class>
|
||||||
|
<class>bzh.risotto.model.Compte</class>
|
||||||
|
<class>bzh.risotto.model.Operation</class>
|
||||||
|
<class>bzh.risotto.model.Agent</class>
|
||||||
|
<class>bzh.risotto.model.Agence</class>
|
||||||
|
<properties>
|
||||||
|
<property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/banque_db"/>
|
||||||
|
<property name="jakarta.persistence.jdbc.user" value="user"/>
|
||||||
|
<property name="jakarta.persistence.jdbc.password" value="password"/>
|
||||||
|
<property name="jakarta.persistence.jdbc.driver" value="org.postgresql.Driver"/>
|
||||||
|
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
|
||||||
|
<property name="hibernate.hbm2ddl.auto" value="update"/>
|
||||||
|
<property name="hibernate.show_sql" value="true"/>
|
||||||
|
<property name="hibernate.format_sql" value="true"/>
|
||||||
|
</properties>
|
||||||
|
</persistence-unit>
|
||||||
|
</persistence>
|
||||||
Reference in New Issue
Block a user