Compare commits
3 Commits
8fd72d599d
...
14d1264091
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14d1264091 | ||
|
|
0f8f09e0d7 | ||
|
|
7c89d637f7 |
@@ -8,8 +8,8 @@ services:
|
||||
MYSQL_DATABASE: testdb
|
||||
MYSQL_USER: user
|
||||
MYSQL_PASSWORD: password
|
||||
# ports:
|
||||
# - "3306:3306"
|
||||
ports:
|
||||
- "3366:3306"
|
||||
volumes:
|
||||
- mysql_data:/var/lib/mysql
|
||||
|
||||
@@ -26,13 +26,5 @@ services:
|
||||
DB_USER: user
|
||||
DB_PASSWORD: password
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
volumes:
|
||||
mysql_data:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.humanbooster;
|
||||
|
||||
import com.humanbooster.dao.ArticleDao;
|
||||
import com.humanbooster.dao.UserDao;
|
||||
import com.humanbooster.model.Article;
|
||||
import com.humanbooster.model.User;
|
||||
import com.humanbooster.service.ArticleService;
|
||||
import com.humanbooster.service.UserService;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.Metadata;
|
||||
@@ -32,10 +34,22 @@ public class App {
|
||||
));
|
||||
|
||||
UserService userService = new UserService(new UserDao(sessionFactory));
|
||||
ArticleService articleService = new ArticleService(new ArticleDao(sessionFactory));
|
||||
|
||||
List<User> existingUsers = userService.getAllUsers();
|
||||
existingUsers.forEach(u -> userService.deleteUser(u.getId()));
|
||||
|
||||
userService.createUser(user);
|
||||
|
||||
List<Article> articles = articleService.findArticlesByCriteria("test", 3L);
|
||||
|
||||
articles.forEach(article -> {
|
||||
System.out.println("\nArticle trouvé :");
|
||||
System.out.println("ID: " + article.getId());
|
||||
System.out.println("Titre: " + article.getTitle());
|
||||
System.out.println("Contenu: " + article.getContent());
|
||||
System.out.println("Auteur: " + article.getAuthor().getName());
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,14 @@
|
||||
package com.humanbooster.dao;
|
||||
|
||||
import com.humanbooster.model.Article;
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ArticleDao extends GenericDaoImpl<Article, Long> {
|
||||
|
||||
public ArticleDao(SessionFactory sessionFactory) {
|
||||
@@ -31,4 +36,38 @@ public class ArticleDao extends GenericDaoImpl<Article,Long> {
|
||||
return article;
|
||||
}
|
||||
}
|
||||
|
||||
public List<Article> findByCriteria(String keyword, Long authorId) {
|
||||
|
||||
Optional<String> optKeyword = Optional.ofNullable(keyword);
|
||||
Optional<Long> optAuthorId = Optional.ofNullable(authorId);
|
||||
|
||||
List<Article> articles;
|
||||
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
|
||||
CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
CriteriaQuery<Article> query = cb.createQuery(Article.class);
|
||||
|
||||
var root = query.from(Article.class);
|
||||
|
||||
query.select(root);
|
||||
|
||||
optKeyword.ifPresent(s -> query.where(
|
||||
cb.like(
|
||||
cb.lower(
|
||||
root.get("title")), "%" + s.toLowerCase() + "%")
|
||||
));
|
||||
|
||||
optAuthorId.ifPresent(l -> query.where(
|
||||
cb.equal(
|
||||
root.get("author"), l)
|
||||
|
||||
));
|
||||
|
||||
articles = session.createQuery(query).getResultList();
|
||||
}
|
||||
return articles;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.humanbooster.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@Entity
|
||||
public class Article {
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.humanbooster.dao.ArticleDao;
|
||||
import com.humanbooster.model.Article;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ArticleService {
|
||||
|
||||
@@ -40,4 +41,8 @@ public class ArticleService {
|
||||
public Article findArticleByTitle(String title) {
|
||||
return articleDao.findByTitle(title);
|
||||
}
|
||||
|
||||
public List<Article> findArticlesByCriteria(String keyword, Long authorId) {
|
||||
return articleDao.findByCriteria(keyword, authorId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
<property name="hibernate.hbm2ddl.auto">update</property>
|
||||
|
||||
|
||||
<property name="hibernate.show_sql">true</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"/>
|
||||
|
||||
Reference in New Issue
Block a user