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