From c31d5fffe1267101042891dab0582cb7deb65043 Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Sat, 3 Jul 2021 03:47:54 +0200 Subject: [PATCH] JPA Entities and the Serializable Interface (#10971) * JPA Entities and the Serializable Interface * JPA Entities and the Serializable Interface - edit after review * JPA Entities and the Serializable Interface - formatting * JPA Entities and the Serializable Interface - indentation --- .../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 | 77 +++++++++++++++++++ 6 files changed, 238 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..696bc23ab0 --- /dev/null +++ b/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/serializable/JPASerializableIntegrationTest.java @@ -0,0 +1,77 @@ +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("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); + + 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()); + } + + @After + public void tearDown() { + entityManager.close(); + } + +}