-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test access to public field for Hibernate entity lazy-loaded from pol…
…ymorphic toOne association
- Loading branch information
Showing
1 changed file
with
122 additions
and
0 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
...rkus/hibernate/orm/publicfields/PublicFieldWithProxyAndLazyLoadingAndInheritanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package io.quarkus.hibernate.orm.publicfields; | ||
|
||
import static io.quarkus.hibernate.orm.TransactionTestUtils.inTransaction; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import javax.inject.Inject; | ||
import javax.persistence.Entity; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.ManyToOne; | ||
import javax.transaction.UserTransaction; | ||
|
||
import org.hibernate.Hibernate; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.TransactionTestUtils; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class PublicFieldWithProxyAndLazyLoadingAndInheritanceTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClass(TransactionTestUtils.class) | ||
.addClasses(Containing.class, Contained.class, ContainedExtended.class) | ||
.addClass(FieldAccessEnhancedDelegate.class)) | ||
.withConfigurationResource("application.properties"); | ||
|
||
@Inject | ||
EntityManager em; | ||
|
||
@Inject | ||
UserTransaction transaction; | ||
|
||
private Long containingID; | ||
|
||
@Test | ||
public void test() { | ||
FieldAccessEnhancedDelegate delegate = new FieldAccessEnhancedDelegate(); | ||
inTransaction(transaction, () -> { | ||
containingID = delegate.createEntities(em, "George"); | ||
}); | ||
inTransaction(transaction, () -> { | ||
delegate.testLazyLoading(em, containingID); | ||
}); | ||
} | ||
|
||
@Entity(name = "Containing") | ||
public static class Containing { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
public Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY, optional = false) | ||
public Contained contained; | ||
} | ||
|
||
@Entity(name = "Contained") | ||
public static class Contained { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
public Long id; | ||
|
||
public String name; | ||
|
||
Contained() { | ||
} | ||
|
||
Contained(String name) { | ||
this.name = name; | ||
} | ||
} | ||
|
||
@Entity(name = "ContainedExtended") | ||
public static class ContainedExtended extends Contained { | ||
|
||
ContainedExtended() { | ||
} | ||
|
||
ContainedExtended(String name) { | ||
this.name = name; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* A class whose bytecode is transformed by Quarkus to replace public field access with getter/setter access. | ||
* <p> | ||
* (Test bytecode was not transformed by Quarkus when using QuarkusUnitTest last time I checked). | ||
*/ | ||
private static class FieldAccessEnhancedDelegate { | ||
|
||
public long createEntities(EntityManager entityManager, String name) { | ||
Containing containing = new Containing(); | ||
ContainedExtended contained = new ContainedExtended(name); | ||
containing.contained = contained; | ||
entityManager.persist(contained); | ||
entityManager.persist(containing); | ||
return containing.id; | ||
} | ||
|
||
public void testLazyLoading(EntityManager entityManager, Long containingID) { | ||
Containing containing = entityManager.find(Containing.class, containingID); | ||
Contained contained = containing.contained; | ||
assertThat(contained).isNotNull(); | ||
assertThat(Hibernate.isPropertyInitialized(contained, "name")).isFalse(); | ||
assertThat(contained.name).isNotNull(); | ||
} | ||
} | ||
} |