[BAEL-4601] How to convert a Hibernate proxy to a real entity object

* Adds Domain Models
* Adds Integration Tests
This commit is contained in:
Emmanuel Yasa
2021-01-05 01:32:23 +08:00
parent f63058acb6
commit 6daa38007c
6 changed files with 257 additions and 0 deletions
@@ -0,0 +1,20 @@
package com.baeldung.jpa.hibernateunproxy;
import javax.persistence.Entity;
import java.math.BigDecimal;
@Entity
public class CreditCardPayment extends Payment {
private String cardNumber;
CreditCardPayment(BigDecimal amount, WebUser webUser, String cardNumber) {
this.amount = amount;
this.webUser = webUser;
this.cardNumber = cardNumber;
}
protected CreditCardPayment() {
}
}
@@ -0,0 +1,47 @@
package com.baeldung.jpa.hibernateunproxy;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import java.math.BigDecimal;
import java.util.Objects;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Payment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
protected WebUser webUser;
protected BigDecimal amount;
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Payment payment = (Payment) o;
return Objects.equals(id, payment.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
protected Payment() {
}
}
@@ -0,0 +1,53 @@
package com.baeldung.jpa.hibernateunproxy;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import java.util.Objects;
import java.util.UUID;
@Entity
public class PaymentReceipt {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(fetch = FetchType.LAZY)
private Payment payment;
private String transactionNumber;
PaymentReceipt(Payment payment) {
this.payment = payment;
this.transactionNumber = UUID.randomUUID().toString();
}
public Payment getPayment() {
return payment;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
PaymentReceipt that = (PaymentReceipt) o;
return Objects.equals(id, that.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
protected PaymentReceipt() {
}
}
@@ -0,0 +1,42 @@
package com.baeldung.jpa.hibernateunproxy;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Objects;
@Entity
public class WebUser {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
WebUser(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
WebUser webUser = (WebUser) o;
return Objects.equals(id, webUser.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
protected WebUser() {
}
}