mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-06-02 08:13:00 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -32,20 +32,17 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
/**
|
||||
* Metadata Mapping specifies the mapping
|
||||
* between classes and tables so that
|
||||
* we could treat a table of any database like a Java class.
|
||||
* Metadata Mapping specifies the mapping between classes and tables so that we could treat a table
|
||||
* of any database like a Java class.
|
||||
*
|
||||
* <p>With hibernate, we achieve list/create/update/delete/get operations: 1)Create the H2 Database
|
||||
* in {@link DatabaseUtil}. 2)Hibernate resolve hibernate.cfg.xml and generate service like
|
||||
* save/list/get/delete. For learning metadata mapping pattern, we go deeper into Hibernate here:
|
||||
* a)read properties from hibernate.cfg.xml and mapping from *.hbm.xml b)create session factory to
|
||||
* generate session interacting with database c)generate session with factory pattern d)create query
|
||||
* object or use basic api with session, hibernate will convert all query to database query
|
||||
* according to metadata 3)We encapsulate hibernate service in {@link UserService} for our use.
|
||||
*
|
||||
* <p>With hibernate, we achieve list/create/update/delete/get operations:
|
||||
* 1)Create the H2 Database in {@link DatabaseUtil}.
|
||||
* 2)Hibernate resolve hibernate.cfg.xml and generate service like save/list/get/delete.
|
||||
* For learning metadata mapping pattern, we go deeper into Hibernate here:
|
||||
* a)read properties from hibernate.cfg.xml and mapping from *.hbm.xml
|
||||
* b)create session factory to generate session interacting with database
|
||||
* c)generate session with factory pattern
|
||||
* d)create query object or use basic api with session,
|
||||
* hibernate will convert all query to database query according to metadata
|
||||
* 3)We encapsulate hibernate service in {@link UserService} for our use.
|
||||
* @see org.hibernate.cfg.Configuration#configure(String)
|
||||
* @see org.hibernate.cfg.Configuration#buildSessionFactory(ServiceRegistry)
|
||||
* @see org.hibernate.internal.SessionFactoryImpl#openSession()
|
||||
@@ -92,4 +89,4 @@ public class App {
|
||||
final var user3 = new User("WangWu", "ww123");
|
||||
return List.of(user1, user2, user3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,9 +28,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* User Entity.
|
||||
*/
|
||||
/** User Entity. */
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@@ -43,6 +41,7 @@ public class User {
|
||||
|
||||
/**
|
||||
* Get a user.
|
||||
*
|
||||
* @param username user name
|
||||
* @param password user password
|
||||
*/
|
||||
@@ -50,4 +49,4 @@ public class User {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,15 +32,14 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
/**
|
||||
* Service layer for user.
|
||||
*/
|
||||
/** Service layer for user. */
|
||||
@Slf4j
|
||||
public class UserService {
|
||||
private static final SessionFactory factory = HibernateUtil.getSessionFactory();
|
||||
|
||||
/**
|
||||
* List all users.
|
||||
*
|
||||
* @return list of users
|
||||
*/
|
||||
public List<User> listUser() {
|
||||
@@ -61,6 +60,7 @@ public class UserService {
|
||||
|
||||
/**
|
||||
* Add a user.
|
||||
*
|
||||
* @param user user entity
|
||||
* @return user id
|
||||
*/
|
||||
@@ -80,6 +80,7 @@ public class UserService {
|
||||
|
||||
/**
|
||||
* Update user.
|
||||
*
|
||||
* @param id user id
|
||||
* @param user new user entity
|
||||
*/
|
||||
@@ -97,6 +98,7 @@ public class UserService {
|
||||
|
||||
/**
|
||||
* Delete user.
|
||||
*
|
||||
* @param id user id
|
||||
*/
|
||||
public void deleteUser(Integer id) {
|
||||
@@ -113,6 +115,7 @@ public class UserService {
|
||||
|
||||
/**
|
||||
* Get user.
|
||||
*
|
||||
* @param id user id
|
||||
* @return deleted user
|
||||
*/
|
||||
@@ -129,10 +132,8 @@ public class UserService {
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close hibernate.
|
||||
*/
|
||||
/** Close hibernate. */
|
||||
public void close() {
|
||||
HibernateUtil.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,13 +28,12 @@ import java.sql.SQLException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
|
||||
/**
|
||||
* Create h2 database.
|
||||
*/
|
||||
/** Create h2 database. */
|
||||
@Slf4j
|
||||
public class DatabaseUtil {
|
||||
private static final String DB_URL = "jdbc:h2:mem:metamapping";
|
||||
private static final String CREATE_SCHEMA_SQL = """
|
||||
private static final String CREATE_SCHEMA_SQL =
|
||||
"""
|
||||
DROP TABLE IF EXISTS `user_account`;CREATE TABLE `user_account` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`username` varchar(255) NOT NULL,
|
||||
@@ -42,9 +41,7 @@ public class DatabaseUtil {
|
||||
PRIMARY KEY (`id`)
|
||||
);""";
|
||||
|
||||
/**
|
||||
* Hide constructor.
|
||||
*/
|
||||
/** Hide constructor. */
|
||||
private DatabaseUtil() {}
|
||||
|
||||
static {
|
||||
@@ -57,4 +54,4 @@ public class DatabaseUtil {
|
||||
LOGGER.error("unable to create h2 data source", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,22 +29,18 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
|
||||
/**
|
||||
* Manage hibernate.
|
||||
*/
|
||||
/** Manage hibernate. */
|
||||
@Slf4j
|
||||
public class HibernateUtil {
|
||||
|
||||
@Getter
|
||||
private static final SessionFactory sessionFactory = buildSessionFactory();
|
||||
@Getter private static final SessionFactory sessionFactory = buildSessionFactory();
|
||||
|
||||
/**
|
||||
* Hide constructor.
|
||||
*/
|
||||
/** Hide constructor. */
|
||||
private HibernateUtil() {}
|
||||
|
||||
/**
|
||||
* Build session factory.
|
||||
*
|
||||
* @return session factory
|
||||
*/
|
||||
private static SessionFactory buildSessionFactory() {
|
||||
@@ -52,12 +48,9 @@ public class HibernateUtil {
|
||||
return new Configuration().configure().buildSessionFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Close session factory.
|
||||
*/
|
||||
/** Close session factory. */
|
||||
public static void shutdown() {
|
||||
// Close caches and connection pools
|
||||
getSessionFactory().close();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,20 +24,18 @@
|
||||
*/
|
||||
package com.iluwatar.metamapping;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that metadata mapping example runs without errors.
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Tests that metadata mapping example runs without errors. */
|
||||
class AppTest {
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
* Issue: Add at least one assertion to this test case. Solution: Inserted assertion to check
|
||||
* whether the execution of the main method in {@link App#main(String[])} throws an exception.
|
||||
*/
|
||||
@Test
|
||||
void shouldExecuteMetaMappingWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user