JPA Entities and the Serializable Interface

This commit is contained in:
Mladen Savic 2021-06-27 18:51:48 +02:00
parent f808407f8f
commit d5c2b68a44
6 changed files with 237 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -100,4 +100,23 @@
</properties> </properties>
</persistence-unit> </persistence-unit>
<persistence-unit name="com.baeldung.hibernate.serializable.h2_persistence_unit">
<description>EntityManager serializable persistence unit</description>
<class>com.baeldung.hibernate.serializable.Email</class>
<class>com.baeldung.hibernate.serializable.Account</class>
<class>com.baeldung.hibernate.serializable.User</class>
<class>com.baeldung.hibernate.serializable.UserId</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.generate_statistics" value="false"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
</properties>
</persistence-unit>
</persistence> </persistence>

View File

@ -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();
}
}