first commit with existing project files
This commit is contained in:
39
hibernate-project/src/main/java/com/humanbooster/App.java
Normal file
39
hibernate-project/src/main/java/com/humanbooster/App.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package com.humanbooster;
|
||||
|
||||
import com.humanbooster.dao.UserDao;
|
||||
import com.humanbooster.model.Article;
|
||||
import com.humanbooster.model.User;
|
||||
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;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Démarrage de l'application");
|
||||
|
||||
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
||||
.configure()
|
||||
.build();
|
||||
|
||||
Metadata metadata = new MetadataSources(registry).buildMetadata();
|
||||
SessionFactory sessionFactory = metadata.buildSessionFactory();
|
||||
System.out.println("Connexion réussie !");
|
||||
|
||||
User user = new User("Bob", "bob@example.com", List.of(
|
||||
new Article("Article 1", "Contenu de l'article 1", null),
|
||||
new Article("Article 2", "Contenu de l'article 2", null)
|
||||
));
|
||||
|
||||
UserService userService = new UserService(new UserDao(sessionFactory));
|
||||
|
||||
List<User> existingUsers = userService.getAllUsers();
|
||||
existingUsers.forEach(u -> userService.deleteUser(u.getId()));
|
||||
|
||||
userService.createUser(user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.humanbooster.dao;
|
||||
|
||||
import com.humanbooster.model.Article;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
public class ArticleDto extends GenericDaoImpl<Article,Long> {
|
||||
|
||||
public ArticleDto(SessionFactory sessionFactory, Class<Article> entityClass) {
|
||||
super(sessionFactory, entityClass);
|
||||
}
|
||||
|
||||
public Article findByAuthor(String author) {
|
||||
try(Session session = sessionFactory.openSession()){
|
||||
session.beginTransaction();
|
||||
Article article = session.createQuery("FROM Article WHERE author = :author", Article.class)
|
||||
.setParameter("author", author)
|
||||
.uniqueResult();
|
||||
session.getTransaction().commit();
|
||||
return article;
|
||||
}
|
||||
}
|
||||
|
||||
public Article findByTitle(String title) {
|
||||
try(Session session = sessionFactory.openSession()){
|
||||
session.beginTransaction();
|
||||
Article article = session.createQuery("FROM Article WHERE title = :title", Article.class)
|
||||
.setParameter("title", title)
|
||||
.uniqueResult();
|
||||
session.getTransaction().commit();
|
||||
return article;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.humanbooster.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface GenericDao<T, ID> {
|
||||
void create(T entity);
|
||||
T read(ID id);
|
||||
void update(T entity);
|
||||
void delete(ID id);
|
||||
List<T> findAll();
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.humanbooster.dao;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class GenericDaoImpl<T, ID> implements GenericDao<T, ID> {
|
||||
|
||||
private final Class<T> entityClass;
|
||||
protected SessionFactory sessionFactory;
|
||||
|
||||
public GenericDaoImpl(SessionFactory sessionFactory, Class<T> entityClass) {
|
||||
this.sessionFactory = sessionFactory;
|
||||
this.entityClass = entityClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(T entity) {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
session.persist(entity);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public T read(ID id) {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
T entity = session.get(entityClass, id);
|
||||
session.getTransaction().commit();
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(T entity) {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
session.merge(entity);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(ID id) {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
T entity = session.get(entityClass, id);
|
||||
if (entity != null) session.remove(entity);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findAll() {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
List<T> entities = session.createQuery("from " + entityClass.getName(), entityClass).list();
|
||||
session.getTransaction().commit();
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.humanbooster.dao;
|
||||
|
||||
import com.humanbooster.model.User;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
public class UserDao extends GenericDaoImpl<User,Long> {
|
||||
|
||||
public UserDao(SessionFactory sessionFactory) {
|
||||
super(sessionFactory, entityClass);
|
||||
}
|
||||
|
||||
public User findByEmail(String email) {
|
||||
try(Session session = sessionFactory.openSession()){
|
||||
session.beginTransaction();
|
||||
User user = session.createQuery("FROM User WHERE email = :email", User.class)
|
||||
.setParameter("email", email)
|
||||
.uniqueResult();
|
||||
session.getTransaction().commit();
|
||||
return user;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.humanbooster.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Article {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "title", nullable = false, length = 100)
|
||||
private String title;
|
||||
|
||||
@Column(name = "content", nullable = false)
|
||||
private String content;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name="author_id", nullable=false)
|
||||
private User author;
|
||||
|
||||
public Article (String titre, String content, User author){
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public User getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(User author) {
|
||||
this.author = author;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.humanbooster.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue (strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
@OneToMany(mappedBy="author", cascade= CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
private List<Article> articles;
|
||||
|
||||
public User(String name, String email, List<Article> articles) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.articles = articles;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public List<Article> getArticles() {
|
||||
return articles;
|
||||
}
|
||||
|
||||
public void setArticles(List<Article> articles) {
|
||||
this.articles = articles;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.humanbooster.service;
|
||||
|
||||
import com.humanbooster.dao.ArticleDto;
|
||||
import com.humanbooster.model.Article;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ArticleService {
|
||||
|
||||
public final ArticleDto articleDto;
|
||||
|
||||
public ArticleService(ArticleDto articleDto) {
|
||||
this.articleDto = articleDto;
|
||||
}
|
||||
|
||||
public void createArticle(Article article) {
|
||||
articleDto.create(article);
|
||||
}
|
||||
|
||||
public void getArticleById(Long id) {
|
||||
articleDto.read(id);
|
||||
}
|
||||
|
||||
public void updateArticle(Article article) {
|
||||
articleDto.update(article);
|
||||
}
|
||||
|
||||
public void deleteArticle(Long id) {
|
||||
articleDto.delete(id);
|
||||
}
|
||||
|
||||
public List<Article> getAllArticles() {
|
||||
return articleDto.findAll();
|
||||
}
|
||||
|
||||
public Article findArticleByAuthor(String author) {
|
||||
return articleDto.findByAuthor(author);
|
||||
}
|
||||
|
||||
public Article findArticleByTitle(String title) {
|
||||
return articleDto.findByTitle(title);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.humanbooster.service;
|
||||
|
||||
import com.humanbooster.dao.UserDao;
|
||||
import com.humanbooster.model.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UserService {
|
||||
|
||||
private final UserDao userDao;
|
||||
|
||||
public UserService(UserDao userDao) {
|
||||
this.userDao = userDao;
|
||||
}
|
||||
|
||||
public void createUser(User user) {
|
||||
userDao.create(user);
|
||||
}
|
||||
|
||||
public void getUserById(Long id) {
|
||||
userDao.read(id);
|
||||
}
|
||||
|
||||
public void updateUser(User user) {
|
||||
userDao.update(user);
|
||||
}
|
||||
|
||||
public void deleteUser(Long id) {
|
||||
userDao.delete(id);
|
||||
}
|
||||
|
||||
public List<User> getAllUsers() {
|
||||
return userDao.findAll();
|
||||
}
|
||||
|
||||
public User findUserByEmail(String email) {
|
||||
return userDao.findByEmail(email);
|
||||
}
|
||||
}
|
||||
22
hibernate-project/src/main/resources/hibernate.cfg.xml
Normal file
22
hibernate-project/src/main/resources/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.dialect">org.hibernate.dialect.MySQLDialect</property>
|
||||
<property name="hibernate.hbm2ddl.auto">update</property>
|
||||
|
||||
|
||||
<property name="hibernate.show_sql">true</property>
|
||||
<property name="hibernate.format_sql">true</property>
|
||||
<mapping class="com.humanbooster.model.User"/>
|
||||
<mapping class="com.humanbooster.model.Article"/>
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
||||
Reference in New Issue
Block a user