How To Convert A Hibernate Proxy To A Real Entity Object - Baeldung
How To Convert A Hibernate Proxy To A Real Entity Object - Baeldung
(/)
by baeldung (https://www.baeldung.com/author/baeldung/)
Persistence (https://www.baeldung.com/category/persistence/)
Spring (https://www.baeldung.com/category/spring/) +
Hibernate (https://www.baeldung.com/tag/hibernate/)
1. Overview
In this tutorial, we'll learn how to convert a Hibernate proxy (/hibernate-proxy-
load-method) to a real entity object. Before that, we'll understand when
Hibernate creates a proxy object. Then, we'll talk about why Hibernate proxy is
useful. And nally, we'll simulate a scenario where there's a need to un proxy
an object.
https://www.baeldung.com/hibernate-proxy-to-real-entity-object 1/9
10/3/2021 How to Convert a Hibernate Proxy to a Real Entity Object | Baeldung
@Entity
public class PaymentReceipt {
...
@OneToOne(fetch = FetchType.LAZY)
private Payment payment;
...
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Payment {
...
@ManyToOne(fetch = FetchType.LAZY)
protected WebUser webUser;
...
}
@Test
public void givenPaymentReceipt_whenAccessingPayment_thenVerifyType() {
PaymentReceipt paymentReceipt =
entityManager.find(PaymentReceipt.class, 3L);
Assert.assertTrue(paymentReceipt.getPayment() instanceof
HibernateProxy);
}
From the test, we've loaded a PaymentReceipt and veri ed that the payment
object isn't an instance of CreditCardPayment — it's a HibernateProxy
object.
In contrast, without lazy loading, the previous test would fail as the returned
payment object would be an instance of CreditCardPayment.
https://www.baeldung.com/hibernate-proxy-to-real-entity-object 2/9
10/3/2021 How to Convert a Hibernate Proxy to a Real Entity Object | Baeldung
paymentReceipt = {PaymentReceipt@5042}
payment = {Payment$HibernateProxy$CZIczfae@5047}
"com.baeldung.jpa.hibernateunproxy.CreditCardPayment@2"
$$_hibernate_interceptor = {ByteBuddyInterceptor@5053}
From the debugger, we can see that Hibernate is using Byte Buddy (/byte-
buddy), which is a library for generating Java classes dynamically at run-time.
Now, let's quickly retrieve a PaymentReceipt and check the generated SQL
from the logs:
https://www.baeldung.com/hibernate-proxy-to-real-entity-object 3/9
10/3/2021 How to Convert a Hibernate Proxy to a Real Entity Object | Baeldung
select
paymentrec0_.id as id1_2_0_,
paymentrec0_.payment_id as payment_3_2_0_,
paymentrec0_.transactionNumber as transact2_2_0_,
payment1_.id as id1_1_1_,
payment1_.amount as amount2_1_1_,
payment1_.webUser_id as webuser_3_1_1_,
payment1_.cardNumber as cardnumb1_0_1_,
payment1_.clazz_ as clazz_1_,
webuser2_.id as id1_3_2_,
webuser2_.name as name2_3_2_
from
PaymentReceipt paymentrec0_
left outer join
(
select
id,
amount,
webUser_id,
cardNumber,
1 as clazz_
from
CreditCardPayment
) payment1_
on paymentrec0_.payment_id=payment1_.id
left outer join
WebUser webuser2_
on payment1_.webUser_id=webuser2_.id
where
paymentrec0_.id=?
select
paymentrec0_.id as id1_2_0_,
paymentrec0_.payment_id as payment_3_2_0_,
paymentrec0_.transactionNumber as transact2_2_0_
from
PaymentReceipt paymentrec0_
where
paymentrec0_.id=?
Clearly, the generated SQL is simpli ed by omitting all the unnecessary join
statements.
https://www.baeldung.com/hibernate-proxy-to-real-entity-object 4/9
10/3/2021 How to Convert a Hibernate Proxy to a Real Entity Object | Baeldung
@Test
public void
givenWebUserProxy_whenCreatingPayment_thenExecuteSingleStatement() {
entityManager.getTransaction().begin();
entityManager.getTransaction().commit();
Assert.assertTrue(webUser instanceof HibernateProxy);
}
insert
into
CreditCardPayment
(amount, webUser_id, cardNumber, id)
values
(?, ?, ?, ?)
Here, we can see that, when using the proxy, Hibernate only executed a
single statement: an INSERT statement for Payment creation.
https://www.baeldung.com/hibernate-proxy-to-real-entity-object 5/9
10/3/2021 How to Convert a Hibernate Proxy to a Real Entity Object | Baeldung
@Entity
public class CreditCardPayment extends Payment {
@Test
public void
givenPaymentReceipt_whenCastingPaymentToConcreteClass_thenThrowClassCastEx
ception() {
PaymentReceipt paymentReceipt =
entityManager.find(PaymentReceipt.class, 3L);
assertThrows(ClassCastException.class, () -> {
CreditCardPayment creditCardPayment = (CreditCardPayment)
paymentReceipt.getPayment();
});
}
https://www.baeldung.com/hibernate-proxy-to-real-entity-object 6/9
10/3/2021 How to Convert a Hibernate Proxy to a Real Entity Object | Baeldung
Since Hibernate 5.2.10, we can use the built-in static method for unproxying
Hibernate entities:
Hibernate.unproxy(paymentReceipt.getPayment());
@Test
public void
givenPaymentReceipt_whenPaymentIsUnproxied_thenReturnRealEntityObject() {
PaymentReceipt paymentReceipt =
entityManager.find(PaymentReceipt.class, 3L);
Assert.assertTrue(Hibernate.unproxy(paymentReceipt.getPayment())
instanceof CreditCardPayment);
}
From the test, we can see that we've successfully converted a Hibernate
proxy to a real entity object.
On the other hand, here's a solution before Hibernate 5.2.10:
6. Conclusion
In this tutorial, we've learned how to convert a Hibernate proxy to a real entity
object. In addition to that, we've discussed how the Hibernate proxy works and
why it's useful. Then, we simulated a situation where there's a need to un
proxy an object.
Lastly, we ran several integration tests to demonstrate our examples and
verify our solution.
As always, the full source code of the article is available over on GitHub
(https://github.com/eugenp/tutorials/tree/master/persistence-
modules/java-jpa-3).
https://www.baeldung.com/hibernate-proxy-to-real-entity-object 7/9
10/3/2021 How to Convert a Hibernate Proxy to a Real Entity Object | Baeldung
CATEGORIES
SPRING (HTTPS://WWW.BAELDUNG.COM/CATEGORY/SPRING/)
REST (HTTPS://WWW.BAELDUNG.COM/CATEGORY/REST/)
JAVA (HTTPS://WWW.BAELDUNG.COM/CATEGORY/JAVA/)
SECURITY (HTTPS://WWW.BAELDUNG.COM/CATEGORY/SECURITY-2/)
PERSISTENCE (HTTPS://WWW.BAELDUNG.COM/CATEGORY/PERSISTENCE/)
JACKSON (HTTPS://WWW.BAELDUNG.COM/CATEGORY/JSON/JACKSON/)
HTTP CLIENT-SIDE (HTTPS://WWW.BAELDUNG.COM/CATEGORY/HTTP/)
SERIES
JAVA “BACK TO BASICS” TUTORIAL (/JAVA-TUTORIAL)
JACKSON JSON TUTORIAL (/JACKSON)
HTTPCLIENT 4 TUTORIAL (/HTTPCLIENT-GUIDE)
REST WITH SPRING TUTORIAL (/REST-WITH-SPRING-SERIES)
SPRING PERSISTENCE TUTORIAL (/PERSISTENCE-WITH-SPRING-SERIES)
SECURITY WITH SPRING (/SECURITY-SPRING)
ABOUT
ABOUT BAELDUNG (/ABOUT)
https://www.baeldung.com/hibernate-proxy-to-real-entity-object 8/9
10/3/2021 How to Convert a Hibernate Proxy to a Real Entity Object | Baeldung
https://www.baeldung.com/hibernate-proxy-to-real-entity-object 9/9