Merge pull request #9715 from andrebrowne/master

BAEL-4077 JPA Entity Equality
This commit is contained in:
Greg 2020-08-12 11:40:40 -04:00 committed by GitHub
commit 660b20c569
8 changed files with 344 additions and 0 deletions

View File

@ -0,0 +1,3 @@
## JPA in Java
This module contains articles about the Java Persistence API (JPA) in Java.

View File

@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>java-jpa-3</artifactId>
<name>java-jpa-3</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>persistence-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<!--Compile time JPA API -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>${javax.persistence-api.version}</version>
</dependency>
<!--Runtime JPA implementation -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>${eclipselink.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgres.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<hibernate.version>5.4.14.Final</hibernate.version>
<eclipselink.version>2.7.4</eclipselink.version>
<postgres.version>42.2.5</postgres.version>
<javax.persistence-api.version>2.2</javax.persistence-api.version>
<assertj.version>3.11.1</assertj.version>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-processor-plugin.version>3.3.3</maven-processor-plugin.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
</properties>
</project>

View File

@ -0,0 +1,53 @@
package com.baeldung.jpa.equality;
import javax.persistence.*;
@Entity
public class EqualByBusinessKey {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String email;
public EqualByBusinessKey() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public int hashCode() {
return java.util.Objects.hashCode(email);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (obj instanceof EqualByBusinessKey) {
if (((EqualByBusinessKey) obj).getEmail() == getEmail()) {
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.jpa.equality;
import javax.persistence.*;
@Entity
public class EqualById {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String email;
public EqualById() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (obj instanceof EqualById) {
return ((EqualById) obj).getId().equals(getId());
}
return false;
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.jpa.equality;
import javax.persistence.*;
@Entity
public class EqualByJavaDefault implements Cloneable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String email;
public EqualByJavaDefault() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
version="2.2">
<persistence-unit name="jpa-h2-equality">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.equality.EqualByJavaDefault</class>
<class>com.baeldung.jpa.equality.EqualById</class>
<class>com.baeldung.jpa.equality.EqualByBusinessKey</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="false" />
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
</properties>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
%msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,74 @@
package com.baeldung.jpa.equality;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class EqualityUnitTest {
private static EntityManagerFactory factory;
private static EntityManager entityManager;
@BeforeClass
public static void setup() {
factory = Persistence.createEntityManagerFactory("jpa-h2-equality");
entityManager = factory.createEntityManager();
}
@Test
public void givenObjectBasedEquality_whenUsingEquals_thenEqualIsBasedOnInstance() throws CloneNotSupportedException {
EqualByJavaDefault object1 = new EqualByJavaDefault();
EqualByJavaDefault object2 = new EqualByJavaDefault();
object1.setEmail("test.user@domain.com");
entityManager.getTransaction().begin();
entityManager.persist(object1);
entityManager.getTransaction().commit();
object2 = (EqualByJavaDefault) object1.clone();
Assert.assertNotEquals(object1, object2);
Assert.assertEquals(object1.getId(), object2.getId());
Assert.assertEquals(object1.getEmail(), object2.getEmail());
}
@Test
public void givenIdBasedEquality_whenUsingEquals_thenEqualIsBasedOnId() {
EqualById object1 = new EqualById();
EqualById object2 = new EqualById();
object1.setEmail("test.user.1@domain.com");
object2.setEmail("test.user.2@domain.com");
entityManager.getTransaction().begin();
entityManager.persist(object1);
entityManager.getTransaction().commit();
object2.setId(object1.getId());
Assert.assertEquals(object1, object2);
Assert.assertEquals(object1.getId(), object2.getId());
Assert.assertNotEquals(object1.getEmail(), object2.getEmail());
}
@Test
public void givenBusinessKeyBasedEquality_whenUsingEquals_thenEqualIsBasedOnBusinessKey() {
EqualByBusinessKey object1 = new EqualByBusinessKey();
EqualByBusinessKey object2 = new EqualByBusinessKey();
object1.setEmail("test.user@test-domain.com");
object2.setEmail("test.user@test-domain.com");
entityManager.getTransaction().begin();
entityManager.persist(object1);
entityManager.getTransaction().commit();
Assert.assertEquals(object1, object2);
Assert.assertNotEquals(object1.getId(), object2.getId());
}
}