BAEL-4007 Add jpa data equality unit test specs

This commit is contained in:
andrebrowne
2020-07-15 20:53:23 -04:00
parent 994e909a18
commit 9e03b89e69
8 changed files with 402 additions and 0 deletions
@@ -0,0 +1,54 @@
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() {
final int prime = 31;
int result = 1;
result = prime * result + ((email == null) ? 0 : email.hashCode());
return result;
}
@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;
}
}
@@ -0,0 +1,54 @@
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() == getId();
return false;
}
}
@@ -0,0 +1,38 @@
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();
}
}
@@ -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>
@@ -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>