Compare commits
2 Commits
86bdf6e81a
...
8fd72d599d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8fd72d599d | ||
|
|
ff55029ce2 |
38
docker-compose.yml
Normal file
38
docker-compose.yml
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
services:
|
||||||
|
mysql:
|
||||||
|
image: mysql:8.3
|
||||||
|
container_name: mysql
|
||||||
|
|
||||||
|
environment:
|
||||||
|
MYSQL_ROOT_PASSWORD: root
|
||||||
|
MYSQL_DATABASE: testdb
|
||||||
|
MYSQL_USER: user
|
||||||
|
MYSQL_PASSWORD: password
|
||||||
|
# ports:
|
||||||
|
# - "3306:3306"
|
||||||
|
volumes:
|
||||||
|
- mysql_data:/var/lib/mysql
|
||||||
|
|
||||||
|
app:
|
||||||
|
build: ./hibernate-project
|
||||||
|
container_name: app
|
||||||
|
depends_on:
|
||||||
|
- mysql
|
||||||
|
restart: on-failure
|
||||||
|
environment:
|
||||||
|
DB_HOST: mysql
|
||||||
|
DB_PORT: 3306
|
||||||
|
DB_NAME: testdb
|
||||||
|
DB_USER: user
|
||||||
|
DB_PASSWORD: password
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
mysql_data:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
19
hibernate-project/Dockerfile
Normal file
19
hibernate-project/Dockerfile
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
FROM maven:3.9.6-eclipse-temurin-21 AS build
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY pom.xml .
|
||||||
|
|
||||||
|
RUN mvn dependency:go-offline -B
|
||||||
|
COPY src ./src
|
||||||
|
|
||||||
|
RUN mvn clean package -DskipTests
|
||||||
|
|
||||||
|
|
||||||
|
FROM eclipse-temurin:21-jre-alpine
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY --from=build /app/target/hibernate-project-1.0-SNAPSHOT.jar app.jar
|
||||||
|
|
||||||
|
ENTRYPOINT [ "java", "-jar", "app.jar" ]
|
||||||
150
hibernate-project/pom.xml
Normal file
150
hibernate-project/pom.xml
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.humanbooster</groupId>
|
||||||
|
<artifactId>hibernate-project</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit</groupId>
|
||||||
|
<artifactId>junit-bom</artifactId>
|
||||||
|
<version>5.11.0</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate.orm</groupId>
|
||||||
|
<artifactId>hibernate-platform</artifactId>
|
||||||
|
<version>6.6.13.Final</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- Optionally: parameterized tests support -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-params</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate.orm</groupId>
|
||||||
|
<artifactId>hibernate-core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jakarta.transaction</groupId>
|
||||||
|
<artifactId>jakarta.transaction-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jakarta.persistence</groupId>
|
||||||
|
<artifactId>jakarta.persistence-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<version>9.3.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains</groupId>
|
||||||
|
<artifactId>annotations</artifactId>
|
||||||
|
<version>RELEASE</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.5.2</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<transformers>
|
||||||
|
<transformer
|
||||||
|
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
|
<mainClass>com.humanbooster.App</mainClass>
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||||
|
<plugins>
|
||||||
|
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-clean-plugin</artifactId>
|
||||||
|
<version>3.4.0</version>
|
||||||
|
</plugin>
|
||||||
|
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-resources-plugin</artifactId>
|
||||||
|
<version>3.3.1</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.13.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>3.3.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<version>3.4.2</version>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<addClasspath>true</addClasspath>
|
||||||
|
<mainClass>com.humanbooster.App</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-install-plugin</artifactId>
|
||||||
|
<version>3.1.2</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-deploy-plugin</artifactId>
|
||||||
|
<version>3.1.2</version>
|
||||||
|
</plugin>
|
||||||
|
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-site-plugin</artifactId>
|
||||||
|
<version>3.12.1</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||||
|
<version>3.6.1</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</pluginManagement>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
41
hibernate-project/src/main/java/com/humanbooster/App.java
Normal file
41
hibernate-project/src/main/java/com/humanbooster/App.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
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", null);
|
||||||
|
|
||||||
|
user.setArticles(List.of(
|
||||||
|
new Article("Article 1", "Contenu de l'article 1", user),
|
||||||
|
new Article("Article 2", "Contenu de l'article 2", user)
|
||||||
|
));
|
||||||
|
|
||||||
|
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 ArticleDao extends GenericDaoImpl<Article,Long> {
|
||||||
|
|
||||||
|
public ArticleDao(SessionFactory sessionFactory) {
|
||||||
|
super(sessionFactory, Article.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
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> {
|
||||||
|
|
||||||
|
protected 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, User.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
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,62 @@
|
|||||||
|
package com.humanbooster.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
@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() {}
|
||||||
|
|
||||||
|
public Article (String title, 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,59 @@
|
|||||||
|
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() {}
|
||||||
|
|
||||||
|
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.ArticleDao;
|
||||||
|
import com.humanbooster.model.Article;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ArticleService {
|
||||||
|
|
||||||
|
public final ArticleDao articleDao;
|
||||||
|
|
||||||
|
public ArticleService(ArticleDao articleDao) {
|
||||||
|
this.articleDao = articleDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createArticle(Article article) {
|
||||||
|
articleDao.create(article);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getArticleById(Long id) {
|
||||||
|
articleDao.read(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateArticle(Article article) {
|
||||||
|
articleDao.update(article);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteArticle(Long id) {
|
||||||
|
articleDao.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Article> getAllArticles() {
|
||||||
|
return articleDao.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Article findArticleByAuthor(String author) {
|
||||||
|
return articleDao.findByAuthor(author);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Article findArticleByTitle(String title) {
|
||||||
|
return articleDao.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