Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.codejig.builder;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertTrue;
- import java.util.Collection;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.OneToOne;
- import org.hibernate.Hibernate;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import org.hibernate.graph.RootGraph;
- import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- public class FetchWithRootGraphTest extends BaseCoreFunctionalTestCase {
- @Entity
- class SimpleEntity {
- int id;
- String text;
- SimpleEntity(int id, String text) {
- this.id = id;
- this.text = text;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getText() {
- return text;
- }
- public void setText(String text) {
- this.text = text;
- }
- }
- @Entity
- class EntityWithReference {
- int id;
- @OneToOne(fetch=FetchType.LAZY)
- SimpleEntity reference;
- public EntityWithReference(int id, SimpleEntity ref) {
- this.id = id;
- this.reference = ref;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public SimpleEntity getReference() {
- return reference;
- }
- public void setReference(SimpleEntity reference) {
- this.reference = reference;
- }
- }
- @Override
- protected Class[] getAnnotatedClasses() {
- return new Class[] {
- SimpleEntity.class,
- EntityWithReference.class
- };
- }
- @Before
- public void before() {
- Session s = openSession();
- Transaction tx = s.beginTransaction();
- for(int i = 0; i < 10; ++i) {
- SimpleEntity sim = new SimpleEntity(i, "Entity #" + i);
- EntityWithReference ref = new EntityWithReference(i, sim);
- s.save(sim);
- s.save(ref);
- }
- tx.commit();
- s.close();
- }
- @After
- public void after() {
- Session session = sessionFactory().openSession();
- session.getTransaction().begin();
- session.createQuery( "delete simpleentity" ).executeUpdate();
- session.createQuery( "delete entitywithreference" ).executeUpdate();
- session.getTransaction().commit();
- session.close();
- }
- @Test
- public void hhh13312Test() throws Exception {
- Session s = openSession();
- Transaction tx = s.beginTransaction();
- RootGraph<EntityWithReference> g = s.createEntityGraph(EntityWithReference.class);
- g.addAttributeNode("reference");
- EntityWithReference single = s.byId(EntityWithReference.class)
- .with(g)
- .load(3L);
- assertEquals((long)single.getId(), 3L);
- assertTrue(Hibernate.isInitialized(single.getReference()));
- Collection<EntityWithReference> multiple = s.byMultipleIds(EntityWithReference.class)
- .with(g)
- .multiLoad(1L, 2L, 3L);
- assertEquals(multiple.size(), 3);
- multiple.stream()
- .map(EntityWithReference::getReference)
- .map(Hibernate::isInitialized)
- .forEach(b -> assertTrue(b));
- tx.commit();
- s.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement