From d5c2b68a44474c87894923e06a6dea50beee601f Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Sun, 27 Jun 2021 18:51:48 +0200 Subject: [PATCH 1/8] JPA Entities and the Serializable Interface --- .../hibernate/serializable/Account.java | 41 ++++++++++ .../hibernate/serializable/Email.java | 38 ++++++++++ .../baeldung/hibernate/serializable/User.java | 36 +++++++++ .../hibernate/serializable/UserId.java | 27 +++++++ .../main/resources/META-INF/persistence.xml | 19 +++++ .../JPASerializableIntegrationTest.java | 76 +++++++++++++++++++ 6 files changed, 237 insertions(+) create mode 100644 persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/Account.java create mode 100644 persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/Email.java create mode 100644 persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/User.java create mode 100644 persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/UserId.java create mode 100644 persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java diff --git a/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/Account.java b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/Account.java new file mode 100644 index 0000000000..b051809ee5 --- /dev/null +++ b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/Account.java @@ -0,0 +1,41 @@ +package com.baeldung.hibernate.serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +@Entity +public class Account { + + @Id + private long id; + private String type; + @ManyToOne + @JoinColumn(referencedColumnName = "email") + private User user; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } +} diff --git a/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/Email.java b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/Email.java new file mode 100644 index 0000000000..11e7c6f159 --- /dev/null +++ b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/Email.java @@ -0,0 +1,38 @@ +package com.baeldung.hibernate.serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; +import java.io.Serializable; + +@Entity +public class Email implements Serializable { + + @Id + private long id; + private String name; + private String domain; + + 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 getDomain() { + return domain; + } + + public void setDomain(String domain) { + this.domain = domain; + } +} diff --git a/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/User.java b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/User.java new file mode 100644 index 0000000000..e7820fe52f --- /dev/null +++ b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/User.java @@ -0,0 +1,36 @@ +package com.baeldung.hibernate.serializable; + +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; + +@Entity +public class User { + + @EmbeddedId private UserId userId; + private Email email; + + + public User() { + } + + public User(UserId userId, Email email) { + this.userId = userId; + this.email = email; + } + + public UserId getUserId() { + return userId; + } + + public void setUserId(UserId userId) { + this.userId = userId; + } + + public Email getEmail() { + return email; + } + + public void setEmail(Email email) { + this.email = email; + } +} diff --git a/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/UserId.java b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/UserId.java new file mode 100644 index 0000000000..7d3d382f67 --- /dev/null +++ b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/serializable/UserId.java @@ -0,0 +1,27 @@ +package com.baeldung.hibernate.serializable; + +import javax.persistence.Embeddable; +import java.io.Serializable; + +@Embeddable +public class UserId implements Serializable { + + private String name; + private String lastName; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml b/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml index c2d5bf59ab..3482414b9d 100644 --- a/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml +++ b/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml @@ -100,4 +100,23 @@ + + + EntityManager serializable persistence unit + com.baeldung.hibernate.serializable.Email + com.baeldung.hibernate.serializable.Account + com.baeldung.hibernate.serializable.User + com.baeldung.hibernate.serializable.UserId + true + + + + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java b/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java new file mode 100644 index 0000000000..d3b5a6ae7a --- /dev/null +++ b/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java @@ -0,0 +1,76 @@ +package com.baeldung.hibernate.serializable; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import java.io.IOException; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class JPASerializableIntegrationTest { + + private static EntityManager entityManager; + + @Before + public void setUp() throws IOException { + EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("com.baeldung.hibernate.serializable.h2_persistence_unit"); + entityManager = entityManagerFactory.createEntityManager(); + entityManager.getTransaction().begin(); + } + + @Test + public void givenUser_whenPersisted_thenUserWillBeFoundById() { + UserId userId = new UserId(); + userId.setName("John"); + userId.setLastName("Doe"); + Email email = new Email(); + email.setId(1); + email.setName("johndoe"); + email.setDomain("gmail.com"); + User user = new User(userId, email); + + entityManager.persist(user); + + User userDb = entityManager.find(User.class, userId); + assertEquals(userDb.getEmail().getName(), "johndoe"); + } + +@Test +public void givenAssociation_whenPersisted_thenMultipleAccountsWillBeFoundByEmail() { + UserId userId = new UserId(); + userId.setName("John"); + userId.setLastName("Doe"); + Email email = new Email(); + email.setId(1); + email.setName("johndoe"); + email.setDomain("gmail.com"); + User user = new User(userId, email); + Account account = new Account(); + account.setType("test"); + account.setId(10); + account.setUser(user); + Account account2 = new Account(); + account2.setType("main"); + account2.setId(11); + account2.setUser(user); + + entityManager.persist(user); + entityManager.persist(account); + entityManager.persist(account2); + + List userAccounts = entityManager.createQuery("select a from Account a join fetch a.user where a.user.email = :email") + .setParameter("email", email).getResultList(); + assertEquals(userAccounts.size(), 2); +} + + @After + public void tearDown() { + entityManager.close(); + } + +} From 860605b3e9395806440a1cd07204767527419b0b Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Wed, 30 Jun 2021 17:50:17 +0200 Subject: [PATCH 2/8] JPA Entities and the Serializable Interface - edit after review --- .../serializable/JPASerializableIntegrationTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java b/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java index d3b5a6ae7a..5bbb7495d7 100644 --- a/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java +++ b/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java @@ -37,7 +37,7 @@ public class JPASerializableIntegrationTest { entityManager.persist(user); User userDb = entityManager.find(User.class, userId); - assertEquals(userDb.getEmail().getName(), "johndoe"); + assertEquals("johndoe", userDb.getEmail().getName()); } @Test @@ -65,7 +65,7 @@ public void givenAssociation_whenPersisted_thenMultipleAccountsWillBeFoundByEmai List userAccounts = entityManager.createQuery("select a from Account a join fetch a.user where a.user.email = :email") .setParameter("email", email).getResultList(); - assertEquals(userAccounts.size(), 2); + assertEquals(2, userAccounts.size()); } @After From ec09e1293f86984a935156425814a2c5cf184c4e Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Thu, 1 Jul 2021 19:43:04 +0200 Subject: [PATCH 3/8] JPA Entities and the Serializable Interface - formatting --- .../JPASerializableIntegrationTest.java | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java b/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java index 5bbb7495d7..7dc315082b 100644 --- a/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java +++ b/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java @@ -40,33 +40,33 @@ public class JPASerializableIntegrationTest { assertEquals("johndoe", userDb.getEmail().getName()); } -@Test -public void givenAssociation_whenPersisted_thenMultipleAccountsWillBeFoundByEmail() { - UserId userId = new UserId(); - userId.setName("John"); - userId.setLastName("Doe"); - Email email = new Email(); - email.setId(1); - email.setName("johndoe"); - email.setDomain("gmail.com"); - User user = new User(userId, email); - Account account = new Account(); - account.setType("test"); - account.setId(10); - account.setUser(user); - Account account2 = new Account(); - account2.setType("main"); - account2.setId(11); - account2.setUser(user); + @Test + public void givenAssociation_whenPersisted_thenMultipleAccountsWillBeFoundByEmail() { + UserId userId = new UserId(); + userId.setName("John"); + userId.setLastName("Doe"); + Email email = new Email(); + email.setId(1); + email.setName("johndoe"); + email.setDomain("gmail.com"); + User user = new User(userId, email); + Account account = new Account(); + account.setType("test"); + account.setId(10); + account.setUser(user); + Account account2 = new Account(); + account2.setType("main"); + account2.setId(11); + account2.setUser(user); - entityManager.persist(user); - entityManager.persist(account); - entityManager.persist(account2); + entityManager.persist(user); + entityManager.persist(account); + entityManager.persist(account2); - List userAccounts = entityManager.createQuery("select a from Account a join fetch a.user where a.user.email = :email") - .setParameter("email", email).getResultList(); - assertEquals(2, userAccounts.size()); -} + List userAccounts = entityManager.createQuery("select a from Account a join fetch a.user where a.user.email = :email") + .setParameter("email", email).getResultList(); + assertEquals(2, userAccounts.size()); + } @After public void tearDown() { From fe5ea96f7605dca511a49b4cbbafd8a41fe00ae1 Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Fri, 2 Jul 2021 09:15:43 +0200 Subject: [PATCH 4/8] JPA Entities and the Serializable Interface - indentation --- .../hibernate/serializable/JPASerializableIntegrationTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java b/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java index 7dc315082b..696bc23ab0 100644 --- a/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java +++ b/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java @@ -64,7 +64,8 @@ public class JPASerializableIntegrationTest { entityManager.persist(account2); List userAccounts = entityManager.createQuery("select a from Account a join fetch a.user where a.user.email = :email") - .setParameter("email", email).getResultList(); + .setParameter("email", email) + .getResultList(); assertEquals(2, userAccounts.size()); } From 69be42810394a67831c0fae190fd541ae5633863 Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Thu, 29 Jul 2021 15:41:01 +0200 Subject: [PATCH 5/8] EntityNotFoundException in Hibernate --- .../entitynotfoundexception/Category.java | 42 +++++++++++++++++ .../entitynotfoundexception/Item.java | 44 ++++++++++++++++++ .../entitynotfoundexception/User.java | 28 ++++++++++++ .../main/resources/META-INF/persistence.xml | 18 ++++++++ ...ntityNotFoundExceptionIntegrationTest.java | 45 +++++++++++++++++++ 5 files changed, 177 insertions(+) create mode 100644 persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Category.java create mode 100644 persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Item.java create mode 100644 persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/User.java create mode 100644 persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/entitynotfoundexception/EntityNotFoundExceptionIntegrationTest.java diff --git a/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Category.java b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Category.java new file mode 100644 index 0000000000..25d31d50c7 --- /dev/null +++ b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Category.java @@ -0,0 +1,42 @@ +package com.baeldung.hibernate.entitynotfoundexception; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +@Entity +public class Category implements Serializable { + + @Id + @Column(unique = true, nullable = false) + private long id; + private String name; + @OneToMany + @JoinColumn(name = "category_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) + private List items = new ArrayList<>(); + + 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 List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Item.java b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Item.java new file mode 100644 index 0000000000..ee6f677d7d --- /dev/null +++ b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Item.java @@ -0,0 +1,44 @@ +package com.baeldung.hibernate.entitynotfoundexception; + +import org.hibernate.annotations.NotFound; +import org.hibernate.annotations.NotFoundAction; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity +public class Item implements Serializable { + + @Id + @Column(unique = true, nullable = false) + private long id; + private String name; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "category_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) + @NotFound(action = NotFoundAction.IGNORE) + private Category category; + + 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 Category getCategory() { + return category; + } + + public void setCategory(Category category) { + this.category = category; + } +} diff --git a/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/User.java b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/User.java new file mode 100644 index 0000000000..d89047195c --- /dev/null +++ b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/User.java @@ -0,0 +1,28 @@ +package com.baeldung.hibernate.entitynotfoundexception; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class User { + + @Id + private long id; + private String name; + + 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; + } +} diff --git a/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml b/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml index 3482414b9d..29cd4faf05 100644 --- a/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml +++ b/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml @@ -119,4 +119,22 @@ + + + EntityManager EntityNotFoundException persistence unit + com.baeldung.hibernate.entitynotfoundexception.Category + com.baeldung.hibernate.entitynotfoundexception.Item + com.baeldung.hibernate.entitynotfoundexception.User + true + + + + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/entitynotfoundexception/EntityNotFoundExceptionIntegrationTest.java b/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/entitynotfoundexception/EntityNotFoundExceptionIntegrationTest.java new file mode 100644 index 0000000000..bcb4e3eb95 --- /dev/null +++ b/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/entitynotfoundexception/EntityNotFoundExceptionIntegrationTest.java @@ -0,0 +1,45 @@ +package com.baeldung.hibernate.entitynotfoundexception; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityNotFoundException; +import javax.persistence.Persistence; +import java.io.IOException; + +public class EntityNotFoundExceptionIntegrationTest { + + private static EntityManager entityManager; + + @Before + public void setUp() throws IOException { + EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("com.baeldung.hibernate.entitynotfoundexception.h2_persistence_unit"); + entityManager = entityManagerFactory.createEntityManager(); + entityManager.getTransaction().begin(); + + } + + + @Test(expected = EntityNotFoundException.class) + public void givenNonExistingUserId_whenGetReferenceIsUsed_thenExceptionIsThrown() { + User user = entityManager.getReference(User.class, 1L); + user.getName(); + } + + @Test(expected = EntityNotFoundException.class) + public void givenItem_whenManyToOneEntityIsMissing_thenExceptionIsThrown() { + entityManager.createNativeQuery("Insert into Item (category_id, name, id) values (1, 'test', 1)").executeUpdate(); + entityManager.flush(); + Item item = entityManager.find(Item.class, 1L); + item.getCategory().getName(); + } + + @After + public void tearDown() { + entityManager.close(); + } + +} From 0372e0fba525a3fed2c73e1a319870c06a326a5f Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Mon, 2 Aug 2021 10:52:05 +0200 Subject: [PATCH 6/8] EntityNotFoundException in Hibernate - fix persistence unit config to allow multiple test to run in conjunction. --- .../com/baeldung/hibernate/entitynotfoundexception/Item.java | 1 - .../hibernate-jpa/src/main/resources/META-INF/persistence.xml | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Item.java b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Item.java index ee6f677d7d..3abed00eb8 100644 --- a/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Item.java +++ b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Item.java @@ -15,7 +15,6 @@ public class Item implements Serializable { private String name; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "category_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) - @NotFound(action = NotFoundAction.IGNORE) private Category category; public long getId() { diff --git a/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml b/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml index 29cd4faf05..e895ac6ba9 100644 --- a/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml +++ b/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml @@ -114,7 +114,7 @@ - + @@ -132,7 +132,7 @@ - + From f30118469ce192f9c4ba8514b42d5adfe9acf84b Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Tue, 31 Aug 2021 20:44:43 +0200 Subject: [PATCH 7/8] Connecting to a Specific Schema in JDBC --- persistence-modules/java-jpa-3/pom.xml | 6 ++ .../jpa/postgresql_schema/Product.java | 30 +++++++ .../main/resources/META-INF/persistence.xml | 11 +++ .../PostgresqlSchemaLiveTest.java | 85 +++++++++++++++++++ .../PostgresqlTestContainer.java | 22 +++++ 5 files changed, 154 insertions(+) create mode 100644 persistence-modules/java-jpa-3/src/main/java/com/baeldung/jpa/postgresql_schema/Product.java create mode 100644 persistence-modules/java-jpa-3/src/test/java/com/baeldung/jpa/postgresql_schema/PostgresqlSchemaLiveTest.java create mode 100644 persistence-modules/java-jpa-3/src/test/java/com/baeldung/jpa/postgresql_schema/PostgresqlTestContainer.java diff --git a/persistence-modules/java-jpa-3/pom.xml b/persistence-modules/java-jpa-3/pom.xml index cecabc10cc..aef35b0547 100644 --- a/persistence-modules/java-jpa-3/pom.xml +++ b/persistence-modules/java-jpa-3/pom.xml @@ -74,6 +74,12 @@ ${junit.version} test + + org.testcontainers + postgresql + 1.16.0 + test + diff --git a/persistence-modules/java-jpa-3/src/main/java/com/baeldung/jpa/postgresql_schema/Product.java b/persistence-modules/java-jpa-3/src/main/java/com/baeldung/jpa/postgresql_schema/Product.java new file mode 100644 index 0000000000..cf74e8bb6d --- /dev/null +++ b/persistence-modules/java-jpa-3/src/main/java/com/baeldung/jpa/postgresql_schema/Product.java @@ -0,0 +1,30 @@ +package com.baeldung.jpa.postgresql_schema; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "product", schema = "store") +public class Product { + + @Id + private int id; + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/persistence-modules/java-jpa-3/src/main/resources/META-INF/persistence.xml b/persistence-modules/java-jpa-3/src/main/resources/META-INF/persistence.xml index 1166aaca71..ef1c4fb3d3 100644 --- a/persistence-modules/java-jpa-3/src/main/resources/META-INF/persistence.xml +++ b/persistence-modules/java-jpa-3/src/main/resources/META-INF/persistence.xml @@ -149,4 +149,15 @@ + + org.hibernate.jpa.HibernatePersistenceProvider + com.baeldung.jpa.postgresql_schema.Product + true + + + + + + + diff --git a/persistence-modules/java-jpa-3/src/test/java/com/baeldung/jpa/postgresql_schema/PostgresqlSchemaLiveTest.java b/persistence-modules/java-jpa-3/src/test/java/com/baeldung/jpa/postgresql_schema/PostgresqlSchemaLiveTest.java new file mode 100644 index 0000000000..e46ea2892c --- /dev/null +++ b/persistence-modules/java-jpa-3/src/test/java/com/baeldung/jpa/postgresql_schema/PostgresqlSchemaLiveTest.java @@ -0,0 +1,85 @@ +package com.baeldung.jpa.postgresql_schema; + +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.postgresql.ds.PGSimpleDataSource; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +public class PostgresqlSchemaLiveTest { + //This tests require running Docker application with working internet connection to fetch + //Postgres image if the image is not available locally. + + @ClassRule + public static PostgresqlTestContainer container = PostgresqlTestContainer.getInstance(); + + + @BeforeClass + public static void setup() throws Exception { + Properties properties = new Properties(); + properties.setProperty("user", container.getUsername()); + properties.setProperty("password", container.getPassword()); + Connection connection = DriverManager.getConnection(container.getJdbcUrl(), properties); + connection.createStatement().execute("CREATE SCHEMA store"); + connection.createStatement().execute("CREATE TABLE store.product(id SERIAL PRIMARY KEY, name VARCHAR(20))"); + connection.createStatement().execute("INSERT INTO store.product VALUES(1, 'test product')"); + } + + @Test + public void settingUpSchemaUsingJdbcURL() throws Exception { + Properties properties = new Properties(); + properties.setProperty("user", container.getUsername()); + properties.setProperty("password", container.getPassword()); + Connection connection = DriverManager.getConnection(container.getJdbcUrl().concat("¤tSchema=store"), properties); + + ResultSet resultSet = connection.createStatement().executeQuery("SELECT * FROM product"); + resultSet.next(); + + assertThat(resultSet.getInt(1), equalTo(1)); + assertThat(resultSet.getString(2), equalTo("test product")); + } + + @Test + public void settingUpSchemaUsingPGSimpleDataSource() throws Exception { + int port = Integer.parseInt(container.getJdbcUrl().substring(container.getJdbcUrl().lastIndexOf(":") + 1, container.getJdbcUrl().lastIndexOf("/"))); + PGSimpleDataSource ds = new PGSimpleDataSource(); + ds.setServerNames(new String[]{container.getHost()}); + ds.setPortNumbers(new int[]{port}); + ds.setUser(container.getUsername()); + ds.setPassword(container.getPassword()); + ds.setDatabaseName("test"); + ds.setCurrentSchema("store"); + + ResultSet resultSet = ds.getConnection().createStatement().executeQuery("SELECT * FROM product"); + resultSet.next(); + + assertThat(resultSet.getInt(1), equalTo(1)); + assertThat(resultSet.getString(2), equalTo("test product")); + } + + @Test + public void settingUpSchemaUsingTableAnnotation() { + Map props = new HashMap<>(); + props.put("hibernate.connection.url", container.getJdbcUrl()); + props.put("hibernate.connection.user", container.getUsername()); + props.put("hibernate.connection.password", container.getPassword()); + EntityManagerFactory emf = Persistence.createEntityManagerFactory("postgresql_schema_unit", props); + EntityManager entityManager = emf.createEntityManager(); + + Product product = entityManager.find(Product.class, 1); + + assertThat(product.getName(), equalTo("test product")); + } +} diff --git a/persistence-modules/java-jpa-3/src/test/java/com/baeldung/jpa/postgresql_schema/PostgresqlTestContainer.java b/persistence-modules/java-jpa-3/src/test/java/com/baeldung/jpa/postgresql_schema/PostgresqlTestContainer.java new file mode 100644 index 0000000000..edc84845c6 --- /dev/null +++ b/persistence-modules/java-jpa-3/src/test/java/com/baeldung/jpa/postgresql_schema/PostgresqlTestContainer.java @@ -0,0 +1,22 @@ +package com.baeldung.jpa.postgresql_schema; + +import org.testcontainers.containers.PostgreSQLContainer; + +public class PostgresqlTestContainer extends PostgreSQLContainer { + + private static final String IMAGE_VERSION = "postgres"; + + private static PostgresqlTestContainer container; + + + private PostgresqlTestContainer() { + super(IMAGE_VERSION); + } + + public static PostgresqlTestContainer getInstance() { + if (container == null) { + container = new PostgresqlTestContainer(); + } + return container; + } +} From 8854bcc5b0aa148404baa338e3afcbc05cb884cb Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Fri, 1 Oct 2021 12:42:21 +0200 Subject: [PATCH 8/8] Parallel Test Execution for JUnit 5 --- junit5/README.md | 6 +++ junit5/pom.xml | 36 +++++++++++++++ .../java/com/baeldung/junit5/A_UnitTest.java | 21 +++++++++ .../java/com/baeldung/junit5/B_UnitTest.java | 23 ++++++++++ .../java/com/baeldung/junit5/C_UnitTest.java | 45 +++++++++++++++++++ .../test/resources/junit-platform.properties | 4 ++ pom.xml | 1 + 7 files changed, 136 insertions(+) create mode 100644 junit5/README.md create mode 100644 junit5/pom.xml create mode 100644 junit5/src/test/java/com/baeldung/junit5/A_UnitTest.java create mode 100644 junit5/src/test/java/com/baeldung/junit5/B_UnitTest.java create mode 100644 junit5/src/test/java/com/baeldung/junit5/C_UnitTest.java create mode 100644 junit5/src/test/resources/junit-platform.properties diff --git a/junit5/README.md b/junit5/README.md new file mode 100644 index 0000000000..ad16ad164d --- /dev/null +++ b/junit5/README.md @@ -0,0 +1,6 @@ +## JUnit5 + +This module contains articles about the JUnit 5 + +### Relevant Articles: + diff --git a/junit5/pom.xml b/junit5/pom.xml new file mode 100644 index 0000000000..b9804408a2 --- /dev/null +++ b/junit5/pom.xml @@ -0,0 +1,36 @@ + + + + 4.0.0 + junit5 + junit5 + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + + + 8 + 8 + + + + + org.junit.jupiter + junit-jupiter-api + 5.8.1 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.8.1 + test + + + + \ No newline at end of file diff --git a/junit5/src/test/java/com/baeldung/junit5/A_UnitTest.java b/junit5/src/test/java/com/baeldung/junit5/A_UnitTest.java new file mode 100644 index 0000000000..e4ba59b22d --- /dev/null +++ b/junit5/src/test/java/com/baeldung/junit5/A_UnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.junit5; + +import org.junit.jupiter.api.Test; + +public class A_UnitTest { + + @Test + public void first() throws Exception{ + System.out.println("Test A first() start => " + Thread.currentThread().getName()); + Thread.sleep(500); + System.out.println("Test A first() end => " + Thread.currentThread().getName()); + } + + @Test + public void second() throws Exception{ + System.out.println("Test A second() start => " + Thread.currentThread().getName()); + Thread.sleep(500); + System.out.println("Test A second() end => " + Thread.currentThread().getName()); + } + +} diff --git a/junit5/src/test/java/com/baeldung/junit5/B_UnitTest.java b/junit5/src/test/java/com/baeldung/junit5/B_UnitTest.java new file mode 100644 index 0000000000..2b195d2551 --- /dev/null +++ b/junit5/src/test/java/com/baeldung/junit5/B_UnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.junit5; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; + +public class B_UnitTest { + + @Test + public void first() throws Exception{ + System.out.println("Test B first() start => " + Thread.currentThread().getName()); + Thread.sleep(500); + System.out.println("Test B first() end => " + Thread.currentThread().getName()); + } + + @Test + public void second() throws Exception{ + System.out.println("Test B second() start => " + Thread.currentThread().getName()); + Thread.sleep(500); + System.out.println("Test B second() end => " + Thread.currentThread().getName()); + } + +} diff --git a/junit5/src/test/java/com/baeldung/junit5/C_UnitTest.java b/junit5/src/test/java/com/baeldung/junit5/C_UnitTest.java new file mode 100644 index 0000000000..ce545f6bee --- /dev/null +++ b/junit5/src/test/java/com/baeldung/junit5/C_UnitTest.java @@ -0,0 +1,45 @@ +package com.baeldung.junit5; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.ResourceLock; + +import java.util.ArrayList; +import java.util.List; + +public class C_UnitTest { + + private List resources; + + @BeforeEach + void before() { + resources = new ArrayList<>(); + resources.add("test"); + } + + @AfterEach + void after() { + resources.clear(); + } + + @Test + @ResourceLock(value = "resources") + public void first() throws Exception { + System.out.println("Test C first() start => " + Thread.currentThread().getName()); + resources.add("first"); + System.out.println(resources); + Thread.sleep(500); + System.out.println("Test C first() end => " + Thread.currentThread().getName()); + } + + @Test + @ResourceLock(value = "resources") + public void second() throws Exception { + System.out.println("Test C second() start => " + Thread.currentThread().getName()); + resources.add("second"); + System.out.println(resources); + Thread.sleep(500); + System.out.println("Test C second() end => " + Thread.currentThread().getName()); + } +} diff --git a/junit5/src/test/resources/junit-platform.properties b/junit5/src/test/resources/junit-platform.properties new file mode 100644 index 0000000000..42100f85da --- /dev/null +++ b/junit5/src/test/resources/junit-platform.properties @@ -0,0 +1,4 @@ +junit.jupiter.execution.parallel.enabled = true +junit.jupiter.execution.parallel.config.strategy=dynamic +junit.jupiter.execution.parallel.mode.default = concurrent +junit.jupiter.execution.parallel.mode.classes.default = concurrent diff --git a/pom.xml b/pom.xml index f5ac14a009..8d28669313 100644 --- a/pom.xml +++ b/pom.xml @@ -472,6 +472,7 @@ json-path jsoup jta + junit5 kubernetes ksqldb