From 71815a8f1bbba44eb08d36b80d7e4bedda6881c3 Mon Sep 17 00:00:00 2001 From: Taras Boychuk Date: Wed, 21 Apr 2021 12:29:42 +0300 Subject: [PATCH 01/20] GP-113 fix the typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 85cd0056..2e374985 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Most people don’t know how to learn Enterprise Java efficiently. So we created that helps them to **master strong skills**, learn **world best practices** and build a **successful career**. πŸš€ At Bobocode we have extensive experience in both building Enterprise Java applications and organizing efficient learning. -Therefore, this course was covers what you need in the most efficient way. We believe that +Therefore, this course covers what you need in the most efficient way. We believe that **the key to efficient learning is practice**. πŸ’ͺ And as a software engineer, you should **spend as much time as you can in the IDE writing code**. At the end of the day, this is the only place where you build software... πŸ’» From a76ba6a64f6567393121730ec0f6f1abddc97062 Mon Sep 17 00:00:00 2001 From: Serhii Date: Wed, 5 May 2021 14:27:01 +0300 Subject: [PATCH 02/20] GP-112 update completed --- .../java/com/bobocode/dao/ProductDaoImpl.java | 179 +++++++++++++++++- 1 file changed, 173 insertions(+), 6 deletions(-) diff --git a/2-0-jdbc-api/2-1-1-product-dao/src/main/java/com/bobocode/dao/ProductDaoImpl.java b/2-0-jdbc-api/2-1-1-product-dao/src/main/java/com/bobocode/dao/ProductDaoImpl.java index b1473e62..8dd9a397 100644 --- a/2-0-jdbc-api/2-1-1-product-dao/src/main/java/com/bobocode/dao/ProductDaoImpl.java +++ b/2-0-jdbc-api/2-1-1-product-dao/src/main/java/com/bobocode/dao/ProductDaoImpl.java @@ -1,12 +1,21 @@ package com.bobocode.dao; +import com.bobocode.exception.DaoOperationException; import com.bobocode.model.Product; -import com.bobocode.util.ExerciseNotCompletedException; import javax.sql.DataSource; +import java.sql.*; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; public class ProductDaoImpl implements ProductDao { + private static final String INSERT_SQL = "INSERT INTO products(name, producer, price, expiration_date) VALUES (?, ?, ?, ?);"; + private static final String SELECT_ALL_SQL = "SELECT * FROM products;"; + private static final String SELECT_BY_ID_SQL = "SELECT * FROM products WHERE id = ?;"; + private static final String UPDATE_BY_ID_SLQ = "UPDATE products SET name = ?, producer = ?, price = ?, expiration_date = ? WHERE id = ?;"; + private static final String REMOVE_BY_ID_SQL = "DELETE FROM products WHERE id = ?;"; + private DataSource dataSource; public ProductDaoImpl(DataSource dataSource) { @@ -15,27 +24,185 @@ public ProductDaoImpl(DataSource dataSource) { @Override public void save(Product product) { - throw new ExerciseNotCompletedException();// todo + Objects.requireNonNull(product); + try (Connection connection = dataSource.getConnection()) { // try-with-resources will automatically close connection + saveProduct(product, connection); + } catch (SQLException e) { + throw new DaoOperationException(String.format("Error saving product: %s", product), e); + } + } + + private void saveProduct(Product product, Connection connection) throws SQLException { + PreparedStatement insertStatement = prepareInsertStatement(product, connection); + insertStatement.executeUpdate(); + Long id = fetchGeneratedId(insertStatement); + product.setId(id); + } + + private PreparedStatement prepareInsertStatement(Product product, Connection connection) { + try { + PreparedStatement insertStatement = connection.prepareStatement(INSERT_SQL, + PreparedStatement.RETURN_GENERATED_KEYS); // this parameter will configure query to ask db for a generated keys + fillProductStatement(product, insertStatement); + return insertStatement; + } catch (SQLException e) { + throw new DaoOperationException(String.format("Cannot prepare statement for product: %s", product), e); + } + } + + private void fillProductStatement(Product product, PreparedStatement updateStatement) throws SQLException { + updateStatement.setString(1, product.getName()); + updateStatement.setString(2, product.getProducer()); + updateStatement.setBigDecimal(3, product.getPrice()); + updateStatement.setDate(4, Date.valueOf(product.getExpirationDate())); + } + + private Long fetchGeneratedId(PreparedStatement insertStatement) throws SQLException { + // this method allows to retrieve IDs that were generated by the database during insert statement + ResultSet generatedKeys = insertStatement.getGeneratedKeys(); + if (generatedKeys.next()) { // you need to call next() because cursor is located before the first row + return generatedKeys.getLong(1); + } else { // if next() returned false it means that database didn't return any IDs + throw new DaoOperationException("Can not obtain product ID"); + } } @Override public List findAll() { - throw new ExerciseNotCompletedException();// todo + try (Connection connection = dataSource.getConnection()) { + return findAllProducts(connection); + } catch (SQLException e) { + throw new DaoOperationException("Error finding all products", e); + } + } + + private List findAllProducts(Connection connection) throws SQLException { + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(SELECT_ALL_SQL); + return collectToList(resultSet); + } + + private List collectToList(ResultSet resultSet) throws SQLException { + List products = new ArrayList<>(); + while (resultSet.next()) { + Product product = parseRow(resultSet); + products.add(product); + } + return products; + } + + private Product parseRow(ResultSet resultSet) { + try { + return createFromResultSet(resultSet); + } catch (SQLException e) { + throw new DaoOperationException("Cannot parse row to create product instance", e); + } + } + + private Product createFromResultSet(ResultSet resultSet) throws SQLException { + Product product = new Product(); + product.setId(resultSet.getLong("id")); + product.setName(resultSet.getString("name")); + product.setProducer(resultSet.getString("producer")); + product.setPrice(resultSet.getBigDecimal("price")); + product.setExpirationDate(resultSet.getDate("expiration_date").toLocalDate()); + product.setCreationTime(resultSet.getTimestamp("creation_time").toLocalDateTime()); + return product; } @Override public Product findOne(Long id) { - throw new ExerciseNotCompletedException();// todo + Objects.requireNonNull(id); + try (Connection connection = dataSource.getConnection()) { + return findProductById(id, connection); + } catch (SQLException e) { + throw new DaoOperationException(String.format("Error finding product by id = %d", id), e); + } + } + + private Product findProductById(Long id, Connection connection) throws SQLException { + PreparedStatement selectByIdStatement = prepareSelectByIdStatement(id, connection); + ResultSet resultSet = selectByIdStatement.executeQuery(); + if (resultSet.next()) {// we need to call next() since cursor is located before the first line + return parseRow(resultSet); + } else { // if next() returned false it means that database returned an empty response + throw new DaoOperationException(String.format("Product with id = %d does not exist", id)); + } + } + + private PreparedStatement prepareSelectByIdStatement(Long id, Connection connection) { + try { + PreparedStatement selectByIdStatement = connection.prepareStatement(SELECT_BY_ID_SQL); + selectByIdStatement.setLong(1, id); + return selectByIdStatement; + } catch (SQLException e) { + throw new DaoOperationException(String.format("Cannot prepare select by id statement for id = %d", id), e); + } } @Override public void update(Product product) { - throw new ExerciseNotCompletedException();// todo + Objects.requireNonNull(product); + try (Connection connection = dataSource.getConnection()) { + updateProduct(product, connection); + } catch (SQLException e) { + throw new DaoOperationException(String.format("Error updating product: %s", product), e); + } + } + + private void updateProduct(Product product, Connection connection) throws SQLException { + checkIdIsNotNull(product); + PreparedStatement updateStatement = prepareUpdateStatement(product, connection); + executeUpdateById(updateStatement, product.getId()); + } + + private void executeUpdateById(PreparedStatement insertStatement, Long productId) throws SQLException { + int rowsAffected = insertStatement.executeUpdate(); // returns number of rows that were changed + if (rowsAffected == 0) { + throw new DaoOperationException(String.format("Product with id = %d does not exist", productId)); + } + } + + private PreparedStatement prepareUpdateStatement(Product product, Connection connection) { + try { + PreparedStatement updateStatement = connection.prepareStatement(UPDATE_BY_ID_SLQ); + fillProductStatement(product, updateStatement); + updateStatement.setLong(5, product.getId()); + return updateStatement; + } catch (Exception e) { + throw new DaoOperationException(String.format("Cannot prepare update statement for product: %s", product), e); + } } @Override public void remove(Product product) { - throw new ExerciseNotCompletedException();// todo + Objects.requireNonNull(product); + try (Connection connection = dataSource.getConnection()) { + removeProduct(product, connection); + } catch (SQLException e) { + throw new DaoOperationException(String.format("Error removing product by id = %d", product.getId()), e); + } + } + + private void removeProduct(Product product, Connection connection) throws SQLException { + checkIdIsNotNull(product); + PreparedStatement removeStatement = prepareRemoveStatement(product, connection); + executeUpdateById(removeStatement, product.getId()); + } + + private PreparedStatement prepareRemoveStatement(Product product, Connection connection) { + try { + PreparedStatement removeStatement = connection.prepareStatement(REMOVE_BY_ID_SQL); + removeStatement.setLong(1, product.getId()); + return removeStatement; + } catch (SQLException e) { + throw new DaoOperationException(String.format("Cannot prepare statement for product: %s", product), e); + } } + private void checkIdIsNotNull(Product product) { + if (product.getId() == null) { + throw new DaoOperationException("Product id cannot be null"); + } + } } From e507a2504a56eebaf07b7e05104f2e0128ebc2d4 Mon Sep 17 00:00:00 2001 From: Serhii Date: Wed, 5 May 2021 14:27:01 +0300 Subject: [PATCH 03/20] GP-112 update completed --- .../java/com/bobocode/dao/ProductDaoImpl.java | 179 +++++++++++++++++- 1 file changed, 173 insertions(+), 6 deletions(-) diff --git a/2-0-jdbc-api/2-1-1-product-dao/src/main/java/com/bobocode/dao/ProductDaoImpl.java b/2-0-jdbc-api/2-1-1-product-dao/src/main/java/com/bobocode/dao/ProductDaoImpl.java index b1473e62..8dd9a397 100644 --- a/2-0-jdbc-api/2-1-1-product-dao/src/main/java/com/bobocode/dao/ProductDaoImpl.java +++ b/2-0-jdbc-api/2-1-1-product-dao/src/main/java/com/bobocode/dao/ProductDaoImpl.java @@ -1,12 +1,21 @@ package com.bobocode.dao; +import com.bobocode.exception.DaoOperationException; import com.bobocode.model.Product; -import com.bobocode.util.ExerciseNotCompletedException; import javax.sql.DataSource; +import java.sql.*; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; public class ProductDaoImpl implements ProductDao { + private static final String INSERT_SQL = "INSERT INTO products(name, producer, price, expiration_date) VALUES (?, ?, ?, ?);"; + private static final String SELECT_ALL_SQL = "SELECT * FROM products;"; + private static final String SELECT_BY_ID_SQL = "SELECT * FROM products WHERE id = ?;"; + private static final String UPDATE_BY_ID_SLQ = "UPDATE products SET name = ?, producer = ?, price = ?, expiration_date = ? WHERE id = ?;"; + private static final String REMOVE_BY_ID_SQL = "DELETE FROM products WHERE id = ?;"; + private DataSource dataSource; public ProductDaoImpl(DataSource dataSource) { @@ -15,27 +24,185 @@ public ProductDaoImpl(DataSource dataSource) { @Override public void save(Product product) { - throw new ExerciseNotCompletedException();// todo + Objects.requireNonNull(product); + try (Connection connection = dataSource.getConnection()) { // try-with-resources will automatically close connection + saveProduct(product, connection); + } catch (SQLException e) { + throw new DaoOperationException(String.format("Error saving product: %s", product), e); + } + } + + private void saveProduct(Product product, Connection connection) throws SQLException { + PreparedStatement insertStatement = prepareInsertStatement(product, connection); + insertStatement.executeUpdate(); + Long id = fetchGeneratedId(insertStatement); + product.setId(id); + } + + private PreparedStatement prepareInsertStatement(Product product, Connection connection) { + try { + PreparedStatement insertStatement = connection.prepareStatement(INSERT_SQL, + PreparedStatement.RETURN_GENERATED_KEYS); // this parameter will configure query to ask db for a generated keys + fillProductStatement(product, insertStatement); + return insertStatement; + } catch (SQLException e) { + throw new DaoOperationException(String.format("Cannot prepare statement for product: %s", product), e); + } + } + + private void fillProductStatement(Product product, PreparedStatement updateStatement) throws SQLException { + updateStatement.setString(1, product.getName()); + updateStatement.setString(2, product.getProducer()); + updateStatement.setBigDecimal(3, product.getPrice()); + updateStatement.setDate(4, Date.valueOf(product.getExpirationDate())); + } + + private Long fetchGeneratedId(PreparedStatement insertStatement) throws SQLException { + // this method allows to retrieve IDs that were generated by the database during insert statement + ResultSet generatedKeys = insertStatement.getGeneratedKeys(); + if (generatedKeys.next()) { // you need to call next() because cursor is located before the first row + return generatedKeys.getLong(1); + } else { // if next() returned false it means that database didn't return any IDs + throw new DaoOperationException("Can not obtain product ID"); + } } @Override public List findAll() { - throw new ExerciseNotCompletedException();// todo + try (Connection connection = dataSource.getConnection()) { + return findAllProducts(connection); + } catch (SQLException e) { + throw new DaoOperationException("Error finding all products", e); + } + } + + private List findAllProducts(Connection connection) throws SQLException { + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(SELECT_ALL_SQL); + return collectToList(resultSet); + } + + private List collectToList(ResultSet resultSet) throws SQLException { + List products = new ArrayList<>(); + while (resultSet.next()) { + Product product = parseRow(resultSet); + products.add(product); + } + return products; + } + + private Product parseRow(ResultSet resultSet) { + try { + return createFromResultSet(resultSet); + } catch (SQLException e) { + throw new DaoOperationException("Cannot parse row to create product instance", e); + } + } + + private Product createFromResultSet(ResultSet resultSet) throws SQLException { + Product product = new Product(); + product.setId(resultSet.getLong("id")); + product.setName(resultSet.getString("name")); + product.setProducer(resultSet.getString("producer")); + product.setPrice(resultSet.getBigDecimal("price")); + product.setExpirationDate(resultSet.getDate("expiration_date").toLocalDate()); + product.setCreationTime(resultSet.getTimestamp("creation_time").toLocalDateTime()); + return product; } @Override public Product findOne(Long id) { - throw new ExerciseNotCompletedException();// todo + Objects.requireNonNull(id); + try (Connection connection = dataSource.getConnection()) { + return findProductById(id, connection); + } catch (SQLException e) { + throw new DaoOperationException(String.format("Error finding product by id = %d", id), e); + } + } + + private Product findProductById(Long id, Connection connection) throws SQLException { + PreparedStatement selectByIdStatement = prepareSelectByIdStatement(id, connection); + ResultSet resultSet = selectByIdStatement.executeQuery(); + if (resultSet.next()) {// we need to call next() since cursor is located before the first line + return parseRow(resultSet); + } else { // if next() returned false it means that database returned an empty response + throw new DaoOperationException(String.format("Product with id = %d does not exist", id)); + } + } + + private PreparedStatement prepareSelectByIdStatement(Long id, Connection connection) { + try { + PreparedStatement selectByIdStatement = connection.prepareStatement(SELECT_BY_ID_SQL); + selectByIdStatement.setLong(1, id); + return selectByIdStatement; + } catch (SQLException e) { + throw new DaoOperationException(String.format("Cannot prepare select by id statement for id = %d", id), e); + } } @Override public void update(Product product) { - throw new ExerciseNotCompletedException();// todo + Objects.requireNonNull(product); + try (Connection connection = dataSource.getConnection()) { + updateProduct(product, connection); + } catch (SQLException e) { + throw new DaoOperationException(String.format("Error updating product: %s", product), e); + } + } + + private void updateProduct(Product product, Connection connection) throws SQLException { + checkIdIsNotNull(product); + PreparedStatement updateStatement = prepareUpdateStatement(product, connection); + executeUpdateById(updateStatement, product.getId()); + } + + private void executeUpdateById(PreparedStatement insertStatement, Long productId) throws SQLException { + int rowsAffected = insertStatement.executeUpdate(); // returns number of rows that were changed + if (rowsAffected == 0) { + throw new DaoOperationException(String.format("Product with id = %d does not exist", productId)); + } + } + + private PreparedStatement prepareUpdateStatement(Product product, Connection connection) { + try { + PreparedStatement updateStatement = connection.prepareStatement(UPDATE_BY_ID_SLQ); + fillProductStatement(product, updateStatement); + updateStatement.setLong(5, product.getId()); + return updateStatement; + } catch (Exception e) { + throw new DaoOperationException(String.format("Cannot prepare update statement for product: %s", product), e); + } } @Override public void remove(Product product) { - throw new ExerciseNotCompletedException();// todo + Objects.requireNonNull(product); + try (Connection connection = dataSource.getConnection()) { + removeProduct(product, connection); + } catch (SQLException e) { + throw new DaoOperationException(String.format("Error removing product by id = %d", product.getId()), e); + } + } + + private void removeProduct(Product product, Connection connection) throws SQLException { + checkIdIsNotNull(product); + PreparedStatement removeStatement = prepareRemoveStatement(product, connection); + executeUpdateById(removeStatement, product.getId()); + } + + private PreparedStatement prepareRemoveStatement(Product product, Connection connection) { + try { + PreparedStatement removeStatement = connection.prepareStatement(REMOVE_BY_ID_SQL); + removeStatement.setLong(1, product.getId()); + return removeStatement; + } catch (SQLException e) { + throw new DaoOperationException(String.format("Cannot prepare statement for product: %s", product), e); + } } + private void checkIdIsNotNull(Product product) { + if (product.getId() == null) { + throw new DaoOperationException("Product id cannot be null"); + } + } } From a35a15d50a265a9a047e80ee1bd515b5be9b0c4a Mon Sep 17 00:00:00 2001 From: Serhii Date: Wed, 19 May 2021 13:14:25 +0300 Subject: [PATCH 04/20] GP-116 Update completed --- .../db/migration/table_initialization.sql | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/1-0-rdbms-and-sql/1-2-1-one-to-one-schema/src/main/resources/db/migration/table_initialization.sql b/1-0-rdbms-and-sql/1-2-1-one-to-one-schema/src/main/resources/db/migration/table_initialization.sql index 6754871f..c5f1faf7 100644 --- a/1-0-rdbms-and-sql/1-2-1-one-to-one-schema/src/main/resources/db/migration/table_initialization.sql +++ b/1-0-rdbms-and-sql/1-2-1-one-to-one-schema/src/main/resources/db/migration/table_initialization.sql @@ -22,4 +22,22 @@ should have a column that stores primary key from a parent table, which is a for */ --- TODO: implement the SQL according to the description \ No newline at end of file +CREATE TABLE IF NOT EXISTS users ( + id BIGINT, + first_name VARCHAR(255) NOT NULL, + last_name VARCHAR(255) NOT NULL, + email VARCHAR(255) NOT NULL, + birthday DATE NOT NULL, + CONSTRAINT users_PK PRIMARY KEY (id), + CONSTRAINT users_email_AK UNIQUE (email) + ); + +CREATE TABLE IF NOT EXISTS profiles ( + user_id BIGINT, + city VARCHAR(255), + job_position VARCHAR(255), + company VARCHAR(255), + education VARCHAR(255), + CONSTRAINT profiles_PK PRIMARY KEY (user_id), + CONSTRAINT profiles_users_FK FOREIGN KEY (user_id) REFERENCES users + ); From 4f9574da10000c28b18422d57afd40c88a54f39a Mon Sep 17 00:00:00 2001 From: Taras Boychuk Date: Fri, 21 May 2021 10:29:27 +0300 Subject: [PATCH 05/20] GP-116 update formattiing --- .../db/migration/table_initialization.sql | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/1-0-rdbms-and-sql/1-2-1-one-to-one-schema/src/main/resources/db/migration/table_initialization.sql b/1-0-rdbms-and-sql/1-2-1-one-to-one-schema/src/main/resources/db/migration/table_initialization.sql index c5f1faf7..e52fca07 100644 --- a/1-0-rdbms-and-sql/1-2-1-one-to-one-schema/src/main/resources/db/migration/table_initialization.sql +++ b/1-0-rdbms-and-sql/1-2-1-one-to-one-schema/src/main/resources/db/migration/table_initialization.sql @@ -22,22 +22,24 @@ should have a column that stores primary key from a parent table, which is a for */ -CREATE TABLE IF NOT EXISTS users ( - id BIGINT, - first_name VARCHAR(255) NOT NULL, +CREATE TABLE IF NOT EXISTS users +( + id BIGINT, + first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, birthday DATE NOT NULL, CONSTRAINT users_PK PRIMARY KEY (id), CONSTRAINT users_email_AK UNIQUE (email) - ); +); -CREATE TABLE IF NOT EXISTS profiles ( - user_id BIGINT, - city VARCHAR(255), +CREATE TABLE IF NOT EXISTS profiles +( + user_id BIGINT, + city VARCHAR(255), job_position VARCHAR(255), company VARCHAR(255), education VARCHAR(255), CONSTRAINT profiles_PK PRIMARY KEY (user_id), CONSTRAINT profiles_users_FK FOREIGN KEY (user_id) REFERENCES users - ); +); From f327a7783ae3f6e3c1e14a22422955dff6b0ee69 Mon Sep 17 00:00:00 2001 From: Serhii Date: Tue, 20 Jul 2021 16:05:17 +0300 Subject: [PATCH 06/20] GP-115 add completed solution --- .../db/migration/table_initialization.sql | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/1-0-rdbms-and-sql/1-2-3-many-to-many-schema/src/main/resources/db/migration/table_initialization.sql b/1-0-rdbms-and-sql/1-2-3-many-to-many-schema/src/main/resources/db/migration/table_initialization.sql index 0d89f003..126eb853 100644 --- a/1-0-rdbms-and-sql/1-2-3-many-to-many-schema/src/main/resources/db/migration/table_initialization.sql +++ b/1-0-rdbms-and-sql/1-2-3-many-to-many-schema/src/main/resources/db/migration/table_initialization.sql @@ -1,11 +1,15 @@ - /* + WallStreet database should store information about brokers, sales groups and its relations. + Each broker must have a unique username. First and last names are also mandatory. + A sales group is a special group that has its own restrictions. Sale groups are used to organize the work of brokers. Each group mush have a unique name, transaction type (string), and max transaction amount (a number). All field are mandatory. + A sales group can consists of more than one broker, while each broker can be associated with more than one sale group. + TECH NOTES AND NAMING CONVENTION - All tables, columns and constraints are named using "snake case" naming convention - All table names must be singular (e.g. "user", not "users") @@ -13,9 +17,37 @@ A sales group can consists of more than one broker, while each broker can be ass - Link tables should have composite primary key, that consists of two other foreign key columns - All primary key, foreign key, and unique constraint should be named according to the naming convention. - All link tables should have a composite key that consists of two foreign key columns + - All primary keys should be named according to the following rule "PK_table_name" - All foreign keys should be named according to the following rule "FK_table_name_reference_table_name" - All alternative keys (unique) should be named according to the following rule "UQ_table_name_column_name" + */ --- TODO: write SQL script to create a database tables according to the requirements \ No newline at end of file +create table if not exists broker ( + id bigint, + username varchar(255) not null, + first_name varchar(255) not null, + last_name varchar(255) not null, + constraint pk_broker primary key (id), + constraint uq_broker_username unique (username) + ); + + +create table if not exists sales_group ( + id bigint, + name varchar(255) not null, + transaction_type varchar(255) not null, + max_transaction_amount int not null, + constraint pk_sales_group primary key (id), + constraint uq_sales_group_name unique (name) + ); + + +create table if not exists broker_sales_group ( + broker_id bigint not null, + sales_group_id bigint not null, + constraint pk_broker_sales_group primary key (broker_id, sales_group_id), + constraint fk_broker_sales_group_broker foreign key (broker_id) references broker, + constraint fk_broker_sales_group_sales_group foreign key (sales_group_id) references sales_group + ); \ No newline at end of file From 01e187b59ccca91456d3117e8736d047f9da8b1c Mon Sep 17 00:00:00 2001 From: Serhii Date: Thu, 22 Jul 2021 10:52:52 +0300 Subject: [PATCH 07/20] GP-121 add completed solution --- .../com/bobocode/AccountDbInitializer.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/2-0-jdbc-api/2-0-2-create-table/src/main/java/com/bobocode/AccountDbInitializer.java b/2-0-jdbc-api/2-0-2-create-table/src/main/java/com/bobocode/AccountDbInitializer.java index c97a5429..0d65b9d2 100644 --- a/2-0-jdbc-api/2-0-2-create-table/src/main/java/com/bobocode/AccountDbInitializer.java +++ b/2-0-jdbc-api/2-0-2-create-table/src/main/java/com/bobocode/AccountDbInitializer.java @@ -1,9 +1,9 @@ package com.bobocode; -import com.bobocode.util.ExerciseNotCompletedException; - import javax.sql.DataSource; +import java.sql.Connection; import java.sql.SQLException; +import java.sql.Statement; /** * {@link AccountDbInitializer} provides an API that allow to initialize (create) an Account table in the database @@ -30,6 +30,20 @@ public AccountDbInitializer(DataSource dataSource) { * @throws SQLException */ public void init() throws SQLException { - throw new ExerciseNotCompletedException(); // todo + try (Connection connection = dataSource.getConnection()) { + Statement statement = connection.createStatement(); + statement.execute("CREATE TABLE account(" + + "id BIGINT," + + " email VARCHAR(255) NOT NULL," + + " first_name VARCHAR(255) NOT NULL," + + " last_name VARCHAR(255) NOT NULL," + + " gender VARCHAR(255) NOT NULL," + + " birthday DATE NOT NULL," + + " balance DECIMAL(19,4)," + + " creation_time TIMESTAMP NOT NULL DEFAULT now()," + + " CONSTRAINT account_pk PRIMARY KEY (id)," + + " CONSTRAINT account_email_uq UNIQUE (email)" + + ");"); + } } } \ No newline at end of file From 520cad305df746372d62197da1cccb79c37acb9e Mon Sep 17 00:00:00 2001 From: Serhii Date: Thu, 22 Jul 2021 11:20:54 +0300 Subject: [PATCH 08/20] GP-123 add completed solution --- .../src/main/java/com/bobocode/model/Movie.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/3-0-jpa-and-hibernate/3-0-0-hello-jpa-entity/src/main/java/com/bobocode/model/Movie.java b/3-0-jpa-and-hibernate/3-0-0-hello-jpa-entity/src/main/java/com/bobocode/model/Movie.java index b7db5fa3..9ff140b9 100644 --- a/3-0-jpa-and-hibernate/3-0-0-hello-jpa-entity/src/main/java/com/bobocode/model/Movie.java +++ b/3-0-jpa-and-hibernate/3-0-0-hello-jpa-entity/src/main/java/com/bobocode/model/Movie.java @@ -4,6 +4,8 @@ import lombok.NoArgsConstructor; import lombok.Setter; +import javax.persistence.*; + /** * TODO: you're job is to implement mapping for JPA entity {@link Movie} * - specify id @@ -14,12 +16,19 @@ @NoArgsConstructor @Getter @Setter +@Entity +@Table(name = "movie") public class Movie { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Column(name = "name", nullable = false) private String name; + @Column(name = "director", nullable = false) private String director; + @Column(name = "duration") private Integer durationSeconds; } \ No newline at end of file From 5d3225ca09e395d73525e9c037582cf46474a429 Mon Sep 17 00:00:00 2001 From: Serhii Date: Thu, 22 Jul 2021 11:56:00 +0300 Subject: [PATCH 09/20] GP-124 add completed solution --- .../src/main/resources/META-INF/persistence.xml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/3-0-jpa-and-hibernate/3-0-1-hello-persistence-xml/src/main/resources/META-INF/persistence.xml b/3-0-jpa-and-hibernate/3-0-1-hello-persistence-xml/src/main/resources/META-INF/persistence.xml index c218803f..1cf5c387 100644 --- a/3-0-jpa-and-hibernate/3-0-1-hello-persistence-xml/src/main/resources/META-INF/persistence.xml +++ b/3-0-jpa-and-hibernate/3-0-1-hello-persistence-xml/src/main/resources/META-INF/persistence.xml @@ -11,4 +11,17 @@ + + com.bobocode.model.Song + + + + + + + + + + + From 094416daaa55998b2804c7c35ca9e6a699dc6927 Mon Sep 17 00:00:00 2001 From: Serhii Date: Fri, 23 Jul 2021 10:19:13 +0300 Subject: [PATCH 10/20] GP-131 fix 1-2-3 completed solution --- .../db/migration/table_initialization.sql | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/1-0-rdbms-and-sql/1-2-3-many-to-many-schema/src/main/resources/db/migration/table_initialization.sql b/1-0-rdbms-and-sql/1-2-3-many-to-many-schema/src/main/resources/db/migration/table_initialization.sql index 126eb853..03e1a831 100644 --- a/1-0-rdbms-and-sql/1-2-3-many-to-many-schema/src/main/resources/db/migration/table_initialization.sql +++ b/1-0-rdbms-and-sql/1-2-3-many-to-many-schema/src/main/resources/db/migration/table_initialization.sql @@ -24,30 +24,30 @@ A sales group can consists of more than one broker, while each broker can be ass */ -create table if not exists broker ( - id bigint, - username varchar(255) not null, - first_name varchar(255) not null, - last_name varchar(255) not null, - constraint pk_broker primary key (id), - constraint uq_broker_username unique (username) - ); - - -create table if not exists sales_group ( - id bigint, - name varchar(255) not null, - transaction_type varchar(255) not null, - max_transaction_amount int not null, - constraint pk_sales_group primary key (id), - constraint uq_sales_group_name unique (name) - ); - - -create table if not exists broker_sales_group ( - broker_id bigint not null, - sales_group_id bigint not null, - constraint pk_broker_sales_group primary key (broker_id, sales_group_id), - constraint fk_broker_sales_group_broker foreign key (broker_id) references broker, - constraint fk_broker_sales_group_sales_group foreign key (sales_group_id) references sales_group - ); \ No newline at end of file +CREATE TABLE IF NOT EXISTS broker ( + id BIGINT, + username VARCHAR(255) NOT NULL, + first_name VARCHAR(255) NOT NULL, + last_name VARCHAR(255) NOT NULL, + CONSTRAINT PK_broker PRIMARY KEY (id), + CONSTRAINT UQ_broker_username UNIQUE (username) +); + + +CREATE TABLE IF NOT EXISTS sales_group ( + id BIGINT, + name VARCHAR(255) NOT NULL, + transaction_type VARCHAR(255) NOT NULL, + max_transaction_amount INT NOT NULL, + CONSTRAINT PK_sales_group PRIMARY KEY (id), + CONSTRAINT UQ_sales_group_name UNIQUE (name) +); + + +CREATE TABLE IF NOT EXISTS broker_sales_group ( + broker_id BIGINT NOT NULL, + sales_group_id BIGINT NOT NULL, + CONSTRAINT PK_broker_sales_group PRIMARY KEY (broker_id, sales_group_id), + CONSTRAINT FK_broker_sales_group_broker FOREIGN KEY (broker_id) REFERENCES broker, + CONSTRAINT FK_broker_sales_group_sales_group FOREIGN KEY (sales_group_id) REFERENCES sales_group +); \ No newline at end of file From 3186519a600e4e3912d6a91ae63ab5e935ccc3b2 Mon Sep 17 00:00:00 2001 From: Serhii Date: Mon, 26 Jul 2021 16:00:48 +0300 Subject: [PATCH 11/20] GP-125 add completed solution --- .../src/main/java/com/bobocode/QueryHelper.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/3-0-jpa-and-hibernate/3-0-2-query-helper/src/main/java/com/bobocode/QueryHelper.java b/3-0-jpa-and-hibernate/3-0-2-query-helper/src/main/java/com/bobocode/QueryHelper.java index a0a9c3e9..d2a1e359 100644 --- a/3-0-jpa-and-hibernate/3-0-2-query-helper/src/main/java/com/bobocode/QueryHelper.java +++ b/3-0-jpa-and-hibernate/3-0-2-query-helper/src/main/java/com/bobocode/QueryHelper.java @@ -32,6 +32,18 @@ public QueryHelper(EntityManagerFactory entityManagerFactory) { * @return query result specified by type T */ public T readWithinTx(Function entityManagerConsumer) { - throw new ExerciseNotCompletedException(); // todo: + EntityManager entityManager = entityManagerFactory.createEntityManager(); + entityManager.unwrap(Session.class).setDefaultReadOnly(true); + entityManager.getTransaction().begin(); + try { + T result = entityManagerConsumer.apply(entityManager); + entityManager.getTransaction().commit(); + return result; + } catch (Exception e) { + entityManager.getTransaction().rollback(); + throw new QueryHelperException("Transaction is rolled back.", e); + } finally { + entityManager.close(); + } } } \ No newline at end of file From 91195cffba439683e7dc97354f1b4168c0bda57e Mon Sep 17 00:00:00 2001 From: Serhii Hryhus Date: Tue, 3 Aug 2021 16:54:13 +0300 Subject: [PATCH 12/20] GP-126 add completed solution --- .../java/com/bobocode/dao/AccountDaoImpl.java | 51 ++++++++++++++++--- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/3-0-jpa-and-hibernate/3-0-3-account-dao/src/main/java/com/bobocode/dao/AccountDaoImpl.java b/3-0-jpa-and-hibernate/3-0-3-account-dao/src/main/java/com/bobocode/dao/AccountDaoImpl.java index 8d78f914..f717bd79 100644 --- a/3-0-jpa-and-hibernate/3-0-3-account-dao/src/main/java/com/bobocode/dao/AccountDaoImpl.java +++ b/3-0-jpa-and-hibernate/3-0-3-account-dao/src/main/java/com/bobocode/dao/AccountDaoImpl.java @@ -1,10 +1,14 @@ package com.bobocode.dao; +import com.bobocode.exception.AccountDaoException; import com.bobocode.model.Account; -import com.bobocode.util.ExerciseNotCompletedException; +import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; +import javax.persistence.TypedQuery; import java.util.List; +import java.util.function.Consumer; +import java.util.function.Function; public class AccountDaoImpl implements AccountDao { private EntityManagerFactory emf; @@ -15,32 +19,65 @@ public AccountDaoImpl(EntityManagerFactory emf) { @Override public void save(Account account) { - throw new ExerciseNotCompletedException(); // todo + performWithinPersistenceContext(em -> em.persist(account)); } @Override public Account findById(Long id) { - throw new ExerciseNotCompletedException(); // todo + return performReturningWithinPersistenceContext(entityManager -> entityManager.find(Account.class, id)); } @Override public Account findByEmail(String email) { - throw new ExerciseNotCompletedException(); // todo + return performReturningWithinPersistenceContext(entityManager -> { + TypedQuery findByEmailQuery + = entityManager.createQuery("select a from Account a where a.email = :email", Account.class); + findByEmailQuery.setParameter("email", email); + return findByEmailQuery.getSingleResult(); + }); } @Override public List findAll() { - throw new ExerciseNotCompletedException(); // todo + return performReturningWithinPersistenceContext(entityManager -> + entityManager.createQuery("select a from Account a", Account.class).getResultList()); } @Override public void update(Account account) { - throw new ExerciseNotCompletedException(); // todo + performWithinPersistenceContext(em -> em.merge(account)); } @Override public void remove(Account account) { - throw new ExerciseNotCompletedException(); // todo + performWithinPersistenceContext(entityManager -> { + Account mergedAccount = entityManager.merge(account); + entityManager.remove(mergedAccount); + }); + } + + private void performWithinPersistenceContext(Consumer entityManagerConsumer) { + performReturningWithinPersistenceContext(entityManager -> { + entityManagerConsumer.accept(entityManager); + return null; + }); + } + + private T performReturningWithinPersistenceContext(Function entityManagerFunction) { + EntityManager entityManager = emf.createEntityManager(); + entityManager.getTransaction().begin(); + T result; + try { + result = entityManagerFunction.apply(entityManager); + entityManager.getTransaction().commit(); + } catch (Exception e) { + entityManager.getTransaction().rollback(); + throw new AccountDaoException("Error performing dao operation. Transaction is rolled back!", e); + } finally { + entityManager.close(); + } + return result; } } + From 85f1ea268378ba3a4ff51f2acc7d5ccd99e0c026 Mon Sep 17 00:00:00 2001 From: Serhii Hryhus Date: Fri, 6 Aug 2021 09:54:01 +0300 Subject: [PATCH 13/20] GP-127 add completed solution --- .../src/main/java/com/bobocode/model/Employee.java | 12 ++++++++++++ .../java/com/bobocode/model/EmployeeProfile.java | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/Employee.java b/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/Employee.java index 58085012..914e38d9 100644 --- a/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/Employee.java +++ b/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/Employee.java @@ -4,6 +4,8 @@ import lombok.NoArgsConstructor; import lombok.Setter; +import javax.persistence.*; + /** * todo: * - configure JPA entity @@ -16,9 +18,19 @@ @NoArgsConstructor @Getter @Setter +@Entity +@Table(name = "employee") public class Employee { + @Id + @GeneratedValue private Long id; + + @Column(nullable = false) private String email; + + @Column(nullable = false) private String fistName; + + @Column(nullable = false) private String lastName; } diff --git a/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/EmployeeProfile.java b/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/EmployeeProfile.java index e92d729d..e6309004 100644 --- a/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/EmployeeProfile.java +++ b/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/EmployeeProfile.java @@ -4,6 +4,8 @@ import lombok.NoArgsConstructor; import lombok.Setter; +import javax.persistence.*; + /** * todo: * - configure JPA entity @@ -16,9 +18,20 @@ @NoArgsConstructor @Getter @Setter +@Entity +@Table(name = "employee_profile") public class EmployeeProfile { + @Id private Long id; + + @MapsId + @OneToOne + @JoinColumn(name = "employee_id") private Employee employee; + + @Column(nullable = false) private String position; + + @Column(nullable = false) private String department; } From 8e70715223ebff69a3925a95d53d5633d2f084e9 Mon Sep 17 00:00:00 2001 From: Serhii Hryhus Date: Tue, 17 Aug 2021 12:50:20 +0300 Subject: [PATCH 14/20] GP-128 add completed solution --- .../java/com/bobocode/dao/CompanyDaoImpl.java | 27 ++++++++++++++++++- .../main/java/com/bobocode/model/Company.java | 21 +++++++++++---- .../main/java/com/bobocode/model/Product.java | 13 +++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/dao/CompanyDaoImpl.java b/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/dao/CompanyDaoImpl.java index dbc4fcf6..16f7ccc3 100644 --- a/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/dao/CompanyDaoImpl.java +++ b/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/dao/CompanyDaoImpl.java @@ -1,9 +1,13 @@ package com.bobocode.dao; +import com.bobocode.exception.CompanyDaoException; import com.bobocode.model.Company; import com.bobocode.util.ExerciseNotCompletedException; +import org.hibernate.Session; +import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; +import java.util.function.Function; public class CompanyDaoImpl implements CompanyDao { private EntityManagerFactory entityManagerFactory; @@ -14,6 +18,27 @@ public CompanyDaoImpl(EntityManagerFactory entityManagerFactory) { @Override public Company findByIdFetchProducts(Long id) { - throw new ExerciseNotCompletedException(); // todo + return readWithinTx(entityManager -> + entityManager + .createQuery("select c from Company c join fetch c.products where c.id = :id", Company.class) + .setParameter("id", id) + .getSingleResult() + ); + } + + private T readWithinTx(Function entityManagerFunction) { + EntityManager entityManager = entityManagerFactory.createEntityManager(); + entityManager.unwrap(Session.class).setDefaultReadOnly(true); + entityManager.getTransaction().begin(); + try { + T queryResult = entityManagerFunction.apply(entityManager); + entityManager.getTransaction().commit(); + return queryResult; + } catch (Exception e) { + entityManager.getTransaction().rollback(); + throw new CompanyDaoException("Error performing read operation", e); + } finally { + entityManager.close(); + } } } diff --git a/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Company.java b/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Company.java index 155fab93..efc80b60 100644 --- a/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Company.java +++ b/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Company.java @@ -1,10 +1,9 @@ package com.bobocode.model; import com.bobocode.util.ExerciseNotCompletedException; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.*; +import javax.persistence.*; import java.util.ArrayList; import java.util.List; @@ -25,16 +24,28 @@ @NoArgsConstructor @Getter @Setter +@EqualsAndHashCode(of = "id") +@Entity +@Table(name = "company") public class Company { + @Id + @GeneratedValue private Long id; + + @Column(nullable = false) private String name; + + @Setter(AccessLevel.PRIVATE) + @OneToMany(mappedBy = "company", fetch = FetchType.LAZY) private List products = new ArrayList<>(); public void addProduct(Product product) { - throw new ExerciseNotCompletedException(); + products.add(product); + product.setCompany(this); } public void removeProduct(Product product) { - throw new ExerciseNotCompletedException(); + products.remove(product); + product.setCompany(null); } } \ No newline at end of file diff --git a/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Product.java b/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Product.java index 562d5082..d1c529fb 100644 --- a/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Product.java +++ b/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Product.java @@ -1,9 +1,12 @@ package com.bobocode.model; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import javax.persistence.*; + /** * todo: * - configure JPA entity @@ -17,8 +20,18 @@ @NoArgsConstructor @Getter @Setter +@EqualsAndHashCode(of = "id") +@Entity +@Table(name = "product") public class Product { + @Id + @GeneratedValue private Long id; + + @Column(nullable = false) private String name; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "company_id") private Company company; } From 3daa243da156091cc3225d9c6e350fca3753dd1d Mon Sep 17 00:00:00 2001 From: Serhii Hryhus Date: Tue, 17 Aug 2021 13:24:30 +0300 Subject: [PATCH 15/20] GP-129 add completed solution --- .../main/java/com/bobocode/model/Author.java | 44 ++++++++++++++++--- .../main/java/com/bobocode/model/Book.java | 21 +++++++-- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/3-0-jpa-and-hibernate/3-1-3-author-book/src/main/java/com/bobocode/model/Author.java b/3-0-jpa-and-hibernate/3-1-3-author-book/src/main/java/com/bobocode/model/Author.java index edeebd11..d5c77cb1 100644 --- a/3-0-jpa-and-hibernate/3-1-3-author-book/src/main/java/com/bobocode/model/Author.java +++ b/3-0-jpa-and-hibernate/3-1-3-author-book/src/main/java/com/bobocode/model/Author.java @@ -1,12 +1,14 @@ package com.bobocode.model; import com.bobocode.util.ExerciseNotCompletedException; +import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; -import javax.persistence.CascadeType; +import javax.persistence.*; import java.util.HashSet; +import java.util.Objects; import java.util.Set; /** @@ -32,17 +34,49 @@ @NoArgsConstructor @Getter @Setter +@Entity +@Table(name = "author") public class Author { + @Id + @GeneratedValue private Long id; + + @Column(name = "first_name", nullable = false) private String firstName; + + @Column(name = "last_name", nullable = false) private String lastName; - private Set books; + + @Setter(AccessLevel.PRIVATE) + @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}) + @JoinTable(name = "author_book", + joinColumns = @JoinColumn(name = "author_id"), + inverseJoinColumns = @JoinColumn(name = "book_id") + ) + private Set books = new HashSet<>(); public void addBook(Book book) { - throw new ExerciseNotCompletedException(); + books.add(book); + book.getAuthors().add(this); } public void removeBook(Book book) { - throw new ExerciseNotCompletedException(); + books.remove(book); + book.getAuthors().remove(this); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Author)) return false; + + Author author = (Author) o; + + return Objects.equals(id, author.id); + } + + @Override + public int hashCode() { + return 31; } -} +} \ No newline at end of file diff --git a/3-0-jpa-and-hibernate/3-1-3-author-book/src/main/java/com/bobocode/model/Book.java b/3-0-jpa-and-hibernate/3-1-3-author-book/src/main/java/com/bobocode/model/Book.java index 7975a50b..2335f6dd 100644 --- a/3-0-jpa-and-hibernate/3-1-3-author-book/src/main/java/com/bobocode/model/Book.java +++ b/3-0-jpa-and-hibernate/3-1-3-author-book/src/main/java/com/bobocode/model/Book.java @@ -1,9 +1,9 @@ package com.bobocode.model; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.*; +import org.hibernate.annotations.NaturalId; +import javax.persistence.*; import java.util.HashSet; import java.util.Set; @@ -24,9 +24,22 @@ @NoArgsConstructor @Getter @Setter +@EqualsAndHashCode(of = "isbn") +@Entity +@Table(name = "book") public class Book { + @Id + @GeneratedValue private Long id; + + @Column(nullable = false) private String name; + + @NaturalId + @Column(nullable = false, unique = true) private String isbn; - private Set authors; + + @Setter(AccessLevel.PRIVATE) + @ManyToMany(mappedBy = "books") + private Set authors = new HashSet<>(); } From ec71ecbfb4eecec2e06f614283b41e229a14d622 Mon Sep 17 00:00:00 2001 From: Serhii Hryhus Date: Tue, 17 Aug 2021 14:30:23 +0300 Subject: [PATCH 16/20] GP-130 add completed solution --- .../java/com/bobocode/dao/PhotoDaoImpl.java | 29 ++++++++++++++----- .../main/java/com/bobocode/model/Photo.java | 26 +++++++++++++---- .../java/com/bobocode/model/PhotoComment.java | 22 ++++++++++++++ 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/dao/PhotoDaoImpl.java b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/dao/PhotoDaoImpl.java index c2f06977..7c1760f4 100644 --- a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/dao/PhotoDaoImpl.java +++ b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/dao/PhotoDaoImpl.java @@ -1,43 +1,58 @@ package com.bobocode.dao; import com.bobocode.model.Photo; +import com.bobocode.model.PhotoComment; +import com.bobocode.util.EntityManagerUtil; import com.bobocode.util.ExerciseNotCompletedException; import javax.persistence.EntityManagerFactory; import java.util.List; +import java.util.Objects; /** * Please note that you should not use auto-commit mode for your implementation. */ public class PhotoDaoImpl implements PhotoDao { - private EntityManagerFactory entityManagerFactory; + private EntityManagerUtil emUtil; public PhotoDaoImpl(EntityManagerFactory entityManagerFactory) { - this.entityManagerFactory = entityManagerFactory; + this.emUtil = new EntityManagerUtil(entityManagerFactory); } @Override public void save(Photo photo) { - throw new ExerciseNotCompletedException(); // todo + Objects.requireNonNull(photo); + emUtil.performWithinTx(entityManager -> entityManager.persist(photo)); } @Override public Photo findById(long id) { - throw new ExerciseNotCompletedException(); // todo + return emUtil.performReturningWithinTx(entityManager -> entityManager.find(Photo.class, id)); } @Override + @SuppressWarnings("unchecked") public List findAll() { - throw new ExerciseNotCompletedException(); // todo + return emUtil.performReturningWithinTx( + entityManager -> entityManager.createQuery("select p from Photo p").getResultList() + ); } @Override public void remove(Photo photo) { - throw new ExerciseNotCompletedException(); // todo + Objects.requireNonNull(photo); + emUtil.performWithinTx(entityManager -> { + Photo managedPhoto = entityManager.merge(photo); + entityManager.remove(managedPhoto); + }); } @Override public void addComment(long photoId, String comment) { - throw new ExerciseNotCompletedException(); // todo + emUtil.performWithinTx(entityManager -> { + Photo photoReference = entityManager.getReference(Photo.class, photoId);// does not call database + PhotoComment photoComment = new PhotoComment(comment, photoReference); + entityManager.persist(photoComment); + }); } } diff --git a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/Photo.java b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/Photo.java index cc88e862..ac61a1ec 100644 --- a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/Photo.java +++ b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/Photo.java @@ -1,9 +1,10 @@ package com.bobocode.model; import com.bobocode.util.ExerciseNotCompletedException; -import lombok.Getter; -import lombok.Setter; +import lombok.*; +import javax.persistence.*; +import java.util.ArrayList; import java.util.List; /** @@ -22,19 +23,34 @@ * - enable cascade type {@link javax.persistence.CascadeType#ALL} for field {@link Photo#comments} * - enable orphan removal */ +@NoArgsConstructor @Getter @Setter +@EqualsAndHashCode(of = "id") +@Entity +@Table(name = "photo") public class Photo { + @Id + @GeneratedValue private Long id; + + @Column(nullable = false, unique = true) private String url; + + @Column(nullable = false) private String description; - private List comments; + + @Setter(AccessLevel.PRIVATE) + @OneToMany(mappedBy = "photo", cascade = CascadeType.ALL, orphanRemoval = true) + private List comments = new ArrayList<>(); public void addComment(PhotoComment comment) { - throw new ExerciseNotCompletedException(); + comments.add(comment); + comment.setPhoto(this); } public void removeComment(PhotoComment comment) { - throw new ExerciseNotCompletedException(); + comments.remove(comment); + comment.setPhoto(null); } } diff --git a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/PhotoComment.java b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/PhotoComment.java index 83edf2f7..444e2adf 100644 --- a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/PhotoComment.java +++ b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/PhotoComment.java @@ -1,8 +1,11 @@ package com.bobocode.model; +import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; +import javax.persistence.*; import java.time.LocalDateTime; /** @@ -17,11 +20,30 @@ * - map relation between Photo and PhotoComment using foreign_key column: "photo_id" * - configure relation as mandatory (not optional) */ +@NoArgsConstructor @Getter @Setter +@EqualsAndHashCode(of = "id") +@Entity +@Table(name = "photo_comment") public class PhotoComment { + @Id + @GeneratedValue private Long id; + + @Column(nullable = false) private String text; + + @Column(nullable = false) private LocalDateTime createdOn; + + @ManyToOne(optional = false) + @JoinColumn(name = "photo_id") private Photo photo; + + public PhotoComment(String text, Photo photo) { + this.text = text; + this.photo = photo; + this.createdOn = LocalDateTime.now(); + } } From 1c7afbd5df381763e08744d4d8dbbeb3b00f2f9b Mon Sep 17 00:00:00 2001 From: Taras Boychuk Date: Sat, 13 Aug 2022 00:43:46 +0300 Subject: [PATCH 17/20] Add DemoApp.java --- lesson-demo/src/main/java/com/bobocode/DemoApp.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 lesson-demo/src/main/java/com/bobocode/DemoApp.java diff --git a/lesson-demo/src/main/java/com/bobocode/DemoApp.java b/lesson-demo/src/main/java/com/bobocode/DemoApp.java new file mode 100644 index 00000000..7c4ff968 --- /dev/null +++ b/lesson-demo/src/main/java/com/bobocode/DemoApp.java @@ -0,0 +1,7 @@ +package com.bobocode; + +public class DemoApp { + public static void main(String[] args) { + System.out.println("Hello"); + } +} From 66186b952d4a5b114ca19f0c11a5413619a5d2d6 Mon Sep 17 00:00:00 2001 From: Taras Boychuk Date: Sat, 13 Aug 2022 00:44:15 +0300 Subject: [PATCH 18/20] Revert "Add DemoApp.java" This reverts commit 1c7afbd5df381763e08744d4d8dbbeb3b00f2f9b. --- lesson-demo/src/main/java/com/bobocode/DemoApp.java | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 lesson-demo/src/main/java/com/bobocode/DemoApp.java diff --git a/lesson-demo/src/main/java/com/bobocode/DemoApp.java b/lesson-demo/src/main/java/com/bobocode/DemoApp.java deleted file mode 100644 index 7c4ff968..00000000 --- a/lesson-demo/src/main/java/com/bobocode/DemoApp.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.bobocode; - -public class DemoApp { - public static void main(String[] args) { - System.out.println("Hello"); - } -} From 7f556967b03b51b4ba100fc39785bb403d8f1cdf Mon Sep 17 00:00:00 2001 From: Taras Boychuk Date: Tue, 17 Oct 2023 13:32:55 +0300 Subject: [PATCH 19/20] Migrate to Java 21 * migrate to Jakarta Persistence * update Java version * upgrade Maven dependencies --- 2-0-jdbc-api/pom.xml | 4 ++-- .../java/com/bobocode/JpaEntityMovieTest.java | 12 +++++------ .../main/java/com/bobocode/model/Song.java | 2 +- .../main/java/com/bobocode/QueryHelper.java | 4 ++-- .../java/com/bobocode/QueryHelperTest.java | 4 ++-- .../java/com/bobocode/dao/AccountDaoImpl.java | 2 +- .../java/com/bobocode/dao/AccountDaoTest.java | 6 +++--- .../bobocode/EmployeeProfileMappingTest.java | 2 +- .../java/com/bobocode/dao/CompanyDaoImpl.java | 2 +- .../bobocode/CompanyProductMappingTest.java | 8 ++++---- .../main/java/com/bobocode/model/Author.java | 2 +- .../com/bobocode/AuthorBookMappingTest.java | 2 +- .../java/com/bobocode/dao/PhotoDaoImpl.java | 2 +- .../main/java/com/bobocode/model/Photo.java | 2 +- .../com/bobocode/PhotoCommentMappingTest.java | 18 ++++++++--------- .../test/java/com/bobocode/PhotoDaoTest.java | 4 ++-- 3-0-jpa-and-hibernate/pom.xml | 12 +++++------ java-persistence-util/jdbc-util/pom.xml | 9 +-------- .../main/java/com/bobocode/model/Account.java | 2 +- .../com/bobocode/util/EntityManagerUtil.java | 4 ++-- java-persistence-util/pom.xml | 11 +++++----- lesson-demo/pom.xml | 6 ------ pom.xml | 20 +++++++++---------- 23 files changed, 64 insertions(+), 76 deletions(-) diff --git a/2-0-jdbc-api/pom.xml b/2-0-jdbc-api/pom.xml index 5e20d851..5a1ab39f 100644 --- a/2-0-jdbc-api/pom.xml +++ b/2-0-jdbc-api/pom.xml @@ -21,12 +21,12 @@ org.postgresql postgresql - 9.4-1202-jdbc4 + 42.6.0 com.h2database h2 - 1.4.197 + 2.2.224 com.bobocode diff --git a/3-0-jpa-and-hibernate/3-0-0-hello-jpa-entity/src/test/java/com/bobocode/JpaEntityMovieTest.java b/3-0-jpa-and-hibernate/3-0-0-hello-jpa-entity/src/test/java/com/bobocode/JpaEntityMovieTest.java index cc8fe68f..6cd1cd7d 100644 --- a/3-0-jpa-and-hibernate/3-0-0-hello-jpa-entity/src/test/java/com/bobocode/JpaEntityMovieTest.java +++ b/3-0-jpa-and-hibernate/3-0-0-hello-jpa-entity/src/test/java/com/bobocode/JpaEntityMovieTest.java @@ -4,12 +4,12 @@ import com.bobocode.model.Movie; import java.lang.reflect.Field; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; diff --git a/3-0-jpa-and-hibernate/3-0-1-hello-persistence-xml/src/main/java/com/bobocode/model/Song.java b/3-0-jpa-and-hibernate/3-0-1-hello-persistence-xml/src/main/java/com/bobocode/model/Song.java index 6d02f478..45ae066e 100644 --- a/3-0-jpa-and-hibernate/3-0-1-hello-persistence-xml/src/main/java/com/bobocode/model/Song.java +++ b/3-0-jpa-and-hibernate/3-0-1-hello-persistence-xml/src/main/java/com/bobocode/model/Song.java @@ -4,7 +4,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; -import javax.persistence.*; +import jakarta.persistence.*; @NoArgsConstructor @Getter diff --git a/3-0-jpa-and-hibernate/3-0-2-query-helper/src/main/java/com/bobocode/QueryHelper.java b/3-0-jpa-and-hibernate/3-0-2-query-helper/src/main/java/com/bobocode/QueryHelper.java index 61c4e0d2..a30ca5dc 100644 --- a/3-0-jpa-and-hibernate/3-0-2-query-helper/src/main/java/com/bobocode/QueryHelper.java +++ b/3-0-jpa-and-hibernate/3-0-2-query-helper/src/main/java/com/bobocode/QueryHelper.java @@ -4,8 +4,8 @@ import com.bobocode.util.ExerciseNotCompletedException; import org.hibernate.Session; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; import java.util.Collection; import java.util.function.Function; diff --git a/3-0-jpa-and-hibernate/3-0-2-query-helper/src/test/java/com/bobocode/QueryHelperTest.java b/3-0-jpa-and-hibernate/3-0-2-query-helper/src/test/java/com/bobocode/QueryHelperTest.java index e37fde5f..e5d0f21a 100644 --- a/3-0-jpa-and-hibernate/3-0-2-query-helper/src/test/java/com/bobocode/QueryHelperTest.java +++ b/3-0-jpa-and-hibernate/3-0-2-query-helper/src/test/java/com/bobocode/QueryHelperTest.java @@ -6,8 +6,8 @@ import com.bobocode.util.TestDataGenerator; import org.junit.jupiter.api.*; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.Persistence; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.junit.jupiter.api.Assertions.fail; diff --git a/3-0-jpa-and-hibernate/3-0-3-account-dao/src/main/java/com/bobocode/dao/AccountDaoImpl.java b/3-0-jpa-and-hibernate/3-0-3-account-dao/src/main/java/com/bobocode/dao/AccountDaoImpl.java index 8d78f914..1e409f8f 100644 --- a/3-0-jpa-and-hibernate/3-0-3-account-dao/src/main/java/com/bobocode/dao/AccountDaoImpl.java +++ b/3-0-jpa-and-hibernate/3-0-3-account-dao/src/main/java/com/bobocode/dao/AccountDaoImpl.java @@ -3,7 +3,7 @@ import com.bobocode.model.Account; import com.bobocode.util.ExerciseNotCompletedException; -import javax.persistence.EntityManagerFactory; +import jakarta.persistence.EntityManagerFactory; import java.util.List; public class AccountDaoImpl implements AccountDao { diff --git a/3-0-jpa-and-hibernate/3-0-3-account-dao/src/test/java/com/bobocode/dao/AccountDaoTest.java b/3-0-jpa-and-hibernate/3-0-3-account-dao/src/test/java/com/bobocode/dao/AccountDaoTest.java index 0c710f95..001f0edf 100644 --- a/3-0-jpa-and-hibernate/3-0-3-account-dao/src/test/java/com/bobocode/dao/AccountDaoTest.java +++ b/3-0-jpa-and-hibernate/3-0-3-account-dao/src/test/java/com/bobocode/dao/AccountDaoTest.java @@ -6,9 +6,9 @@ import org.hibernate.Session; import org.junit.jupiter.api.*; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.Persistence; import java.math.BigDecimal; import java.math.RoundingMode; import java.sql.Date; diff --git a/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/test/java/com/bobocode/EmployeeProfileMappingTest.java b/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/test/java/com/bobocode/EmployeeProfileMappingTest.java index 004bbe04..dc9a0e3e 100644 --- a/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/test/java/com/bobocode/EmployeeProfileMappingTest.java +++ b/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/test/java/com/bobocode/EmployeeProfileMappingTest.java @@ -6,7 +6,7 @@ import org.apache.commons.lang3.RandomStringUtils; import org.junit.jupiter.api.*; -import javax.persistence.*; +import jakarta.persistence.*; import java.lang.reflect.Field; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; diff --git a/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/dao/CompanyDaoImpl.java b/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/dao/CompanyDaoImpl.java index dbc4fcf6..c56818c2 100644 --- a/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/dao/CompanyDaoImpl.java +++ b/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/dao/CompanyDaoImpl.java @@ -3,7 +3,7 @@ import com.bobocode.model.Company; import com.bobocode.util.ExerciseNotCompletedException; -import javax.persistence.EntityManagerFactory; +import jakarta.persistence.EntityManagerFactory; public class CompanyDaoImpl implements CompanyDao { private EntityManagerFactory entityManagerFactory; diff --git a/3-0-jpa-and-hibernate/3-1-2-company-products/src/test/java/com/bobocode/CompanyProductMappingTest.java b/3-0-jpa-and-hibernate/3-1-2-company-products/src/test/java/com/bobocode/CompanyProductMappingTest.java index a08a0688..f6c8a4d2 100644 --- a/3-0-jpa-and-hibernate/3-1-2-company-products/src/test/java/com/bobocode/CompanyProductMappingTest.java +++ b/3-0-jpa-and-hibernate/3-1-2-company-products/src/test/java/com/bobocode/CompanyProductMappingTest.java @@ -9,10 +9,10 @@ import org.hibernate.LazyInitializationException; import org.junit.jupiter.api.*; -import javax.persistence.EntityManagerFactory; -import javax.persistence.JoinColumn; -import javax.persistence.Persistence; -import javax.persistence.PersistenceException; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.Persistence; +import jakarta.persistence.PersistenceException; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.List; diff --git a/3-0-jpa-and-hibernate/3-1-3-author-book/src/main/java/com/bobocode/model/Author.java b/3-0-jpa-and-hibernate/3-1-3-author-book/src/main/java/com/bobocode/model/Author.java index edeebd11..28dd228c 100644 --- a/3-0-jpa-and-hibernate/3-1-3-author-book/src/main/java/com/bobocode/model/Author.java +++ b/3-0-jpa-and-hibernate/3-1-3-author-book/src/main/java/com/bobocode/model/Author.java @@ -5,7 +5,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; -import javax.persistence.CascadeType; +import jakarta.persistence.CascadeType; import java.util.HashSet; import java.util.Set; diff --git a/3-0-jpa-and-hibernate/3-1-3-author-book/src/test/java/com/bobocode/AuthorBookMappingTest.java b/3-0-jpa-and-hibernate/3-1-3-author-book/src/test/java/com/bobocode/AuthorBookMappingTest.java index 509bdb92..29e24730 100644 --- a/3-0-jpa-and-hibernate/3-1-3-author-book/src/test/java/com/bobocode/AuthorBookMappingTest.java +++ b/3-0-jpa-and-hibernate/3-1-3-author-book/src/test/java/com/bobocode/AuthorBookMappingTest.java @@ -7,7 +7,7 @@ import org.hibernate.Session; import org.junit.jupiter.api.*; -import javax.persistence.*; +import jakarta.persistence.*; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.List; diff --git a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/dao/PhotoDaoImpl.java b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/dao/PhotoDaoImpl.java index c2f06977..be88196f 100644 --- a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/dao/PhotoDaoImpl.java +++ b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/dao/PhotoDaoImpl.java @@ -3,7 +3,7 @@ import com.bobocode.model.Photo; import com.bobocode.util.ExerciseNotCompletedException; -import javax.persistence.EntityManagerFactory; +import jakarta.persistence.EntityManagerFactory; import java.util.List; /** diff --git a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/Photo.java b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/Photo.java index cc88e862..c81adf3e 100644 --- a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/Photo.java +++ b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/Photo.java @@ -19,7 +19,7 @@ * - initialize field comments * - map relation between Photo and PhotoComment on the child side * - implement helper methods {@link Photo#addComment(PhotoComment)} and {@link Photo#removeComment(PhotoComment)} - * - enable cascade type {@link javax.persistence.CascadeType#ALL} for field {@link Photo#comments} + * - enable cascade type {@link jakarta.persistence.CascadeType#ALL} for field {@link Photo#comments} * - enable orphan removal */ @Getter diff --git a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/test/java/com/bobocode/PhotoCommentMappingTest.java b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/test/java/com/bobocode/PhotoCommentMappingTest.java index db9413af..67245163 100644 --- a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/test/java/com/bobocode/PhotoCommentMappingTest.java +++ b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/test/java/com/bobocode/PhotoCommentMappingTest.java @@ -14,15 +14,15 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.List; -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.EntityManagerFactory; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.OneToMany; -import javax.persistence.Persistence; -import javax.persistence.PersistenceException; -import javax.persistence.Table; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Persistence; +import jakarta.persistence.PersistenceException; +import jakarta.persistence.Table; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; diff --git a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/test/java/com/bobocode/PhotoDaoTest.java b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/test/java/com/bobocode/PhotoDaoTest.java index 079bfe69..cf9ce1d1 100644 --- a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/test/java/com/bobocode/PhotoDaoTest.java +++ b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/test/java/com/bobocode/PhotoDaoTest.java @@ -10,8 +10,8 @@ import com.bobocode.model.Photo; import com.bobocode.util.EntityManagerUtil; import java.util.List; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.Persistence; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; diff --git a/3-0-jpa-and-hibernate/pom.xml b/3-0-jpa-and-hibernate/pom.xml index f7ceba9e..84d982a6 100644 --- a/3-0-jpa-and-hibernate/pom.xml +++ b/3-0-jpa-and-hibernate/pom.xml @@ -27,22 +27,22 @@ org.postgresql postgresql - 42.2.23 + 42.6.0 com.h2database h2 - 1.4.200 + 2.2.224 - org.hibernate.javax.persistence - hibernate-jpa-2.1-api - 1.0.2.Final + jakarta.persistence + jakarta.persistence-api + 3.1.0 org.hibernate hibernate-core - 5.5.4.Final + 6.3.1.Final diff --git a/java-persistence-util/jdbc-util/pom.xml b/java-persistence-util/jdbc-util/pom.xml index 6b53606b..0e822baf 100644 --- a/java-persistence-util/jdbc-util/pom.xml +++ b/java-persistence-util/jdbc-util/pom.xml @@ -11,12 +11,5 @@ jdbc-util jar - - - - org.slf4j - slf4j-simple - 1.7.12 - - + \ No newline at end of file diff --git a/java-persistence-util/jpa-hibernate-model/src/main/java/com/bobocode/model/Account.java b/java-persistence-util/jpa-hibernate-model/src/main/java/com/bobocode/model/Account.java index 0ca7301a..6c1ab536 100644 --- a/java-persistence-util/jpa-hibernate-model/src/main/java/com/bobocode/model/Account.java +++ b/java-persistence-util/jpa-hibernate-model/src/main/java/com/bobocode/model/Account.java @@ -2,7 +2,7 @@ import lombok.*; -import javax.persistence.*; +import jakarta.persistence.*; import java.math.BigDecimal; import java.time.LocalDate; import java.time.LocalDateTime; diff --git a/java-persistence-util/jpa-hibernate-util/src/main/java/com/bobocode/util/EntityManagerUtil.java b/java-persistence-util/jpa-hibernate-util/src/main/java/com/bobocode/util/EntityManagerUtil.java index 5757c834..dbbca122 100644 --- a/java-persistence-util/jpa-hibernate-util/src/main/java/com/bobocode/util/EntityManagerUtil.java +++ b/java-persistence-util/jpa-hibernate-util/src/main/java/com/bobocode/util/EntityManagerUtil.java @@ -1,7 +1,7 @@ package com.bobocode.util; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; import java.util.function.Consumer; import java.util.function.Function; diff --git a/java-persistence-util/pom.xml b/java-persistence-util/pom.xml index 6028e7ad..a28e1f35 100644 --- a/java-persistence-util/pom.xml +++ b/java-persistence-util/pom.xml @@ -24,18 +24,19 @@ org.postgresql postgresql - 42.3.6 + 42.6.0 com.h2database h2 - 1.4.197 + 2.1.214 - org.hibernate.javax.persistence - hibernate-jpa-2.1-api - 1.0.2.Final + jakarta.persistence + jakarta.persistence-api + 3.1.0 + \ No newline at end of file diff --git a/lesson-demo/pom.xml b/lesson-demo/pom.xml index 220809df..80b50add 100644 --- a/lesson-demo/pom.xml +++ b/lesson-demo/pom.xml @@ -11,10 +11,4 @@ lesson-demo - - 17 - 17 - UTF-8 - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index e106bc98..fdfd9592 100644 --- a/pom.xml +++ b/pom.xml @@ -10,8 +10,8 @@ pom - 17 - 17 + 21 + 21 UTF-8 @@ -28,19 +28,19 @@ org.junit.jupiter junit-jupiter-engine - 5.9.0 + 5.10.0 test org.junit.jupiter junit-jupiter-params - 5.9.0 + 5.10.0 test org.assertj assertj-core - 3.23.1 + 3.24.2 test @@ -52,7 +52,7 @@ org.projectlombok lombok - 1.18.24 + 1.18.30 com.google.code.findbugs @@ -62,19 +62,19 @@ org.mockito mockito-core - 4.6.1 + 5.6.0 test org.mockito mockito-inline - 4.6.1 + 5.2.0 test org.slf4j slf4j-simple - 1.7.36 + 2.0.9 org.apache.commons @@ -89,7 +89,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M3 + 3.1.2 From a0c544a1121a50293a12f45abdb6c185b473aac6 Mon Sep 17 00:00:00 2001 From: Taras Boychuk Date: Tue, 17 Oct 2023 14:55:16 +0300 Subject: [PATCH 20/20] Migrate to jakarta packages --- .../src/main/java/com/bobocode/model/Movie.java | 2 +- .../src/main/java/com/bobocode/model/Employee.java | 2 +- .../src/main/java/com/bobocode/model/EmployeeProfile.java | 2 +- .../src/main/java/com/bobocode/model/Company.java | 3 +-- .../src/main/java/com/bobocode/model/Product.java | 2 +- .../src/main/java/com/bobocode/model/Photo.java | 2 +- .../src/main/java/com/bobocode/model/PhotoComment.java | 2 +- 7 files changed, 7 insertions(+), 8 deletions(-) diff --git a/3-0-jpa-and-hibernate/3-0-0-hello-jpa-entity/src/main/java/com/bobocode/model/Movie.java b/3-0-jpa-and-hibernate/3-0-0-hello-jpa-entity/src/main/java/com/bobocode/model/Movie.java index 1ca6d55f..d1bf47c7 100644 --- a/3-0-jpa-and-hibernate/3-0-0-hello-jpa-entity/src/main/java/com/bobocode/model/Movie.java +++ b/3-0-jpa-and-hibernate/3-0-0-hello-jpa-entity/src/main/java/com/bobocode/model/Movie.java @@ -4,7 +4,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; -import javax.persistence.*; +import jakarta.persistence.*; /** * TODO: you're job is to implement mapping for JPA entity {@link Movie} diff --git a/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/Employee.java b/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/Employee.java index 914e38d9..24f7e56c 100644 --- a/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/Employee.java +++ b/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/Employee.java @@ -4,7 +4,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; -import javax.persistence.*; +import jakarta.persistence.*; /** * todo: diff --git a/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/EmployeeProfile.java b/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/EmployeeProfile.java index e6309004..1b9dd20e 100644 --- a/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/EmployeeProfile.java +++ b/3-0-jpa-and-hibernate/3-1-1-employee-profile/src/main/java/com/bobocode/model/EmployeeProfile.java @@ -4,7 +4,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; -import javax.persistence.*; +import jakarta.persistence.*; /** * todo: diff --git a/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Company.java b/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Company.java index 551bf545..0d5076fa 100644 --- a/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Company.java +++ b/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Company.java @@ -1,9 +1,8 @@ package com.bobocode.model; -import com.bobocode.util.ExerciseNotCompletedException; +import jakarta.persistence.*; import lombok.*; -import javax.persistence.*; import java.util.ArrayList; import java.util.List; diff --git a/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Product.java b/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Product.java index 6a554f6e..b3d327cf 100644 --- a/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Product.java +++ b/3-0-jpa-and-hibernate/3-1-2-company-products/src/main/java/com/bobocode/model/Product.java @@ -5,7 +5,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; -import javax.persistence.*; +import jakarta.persistence.*; /** * todo: diff --git a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/Photo.java b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/Photo.java index 45352479..fd24dd0c 100644 --- a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/Photo.java +++ b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/Photo.java @@ -3,7 +3,7 @@ import com.bobocode.util.ExerciseNotCompletedException; import lombok.*; -import javax.persistence.*; +import jakarta.persistence.*; import java.util.ArrayList; import java.util.List; diff --git a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/PhotoComment.java b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/PhotoComment.java index 444e2adf..a322ac7a 100644 --- a/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/PhotoComment.java +++ b/3-0-jpa-and-hibernate/3-2-2-photo-comment-dao/src/main/java/com/bobocode/model/PhotoComment.java @@ -5,7 +5,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; -import javax.persistence.*; +import jakarta.persistence.*; import java.time.LocalDateTime; /**