Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. package com.codejig.builder;
  2.  
  3.  
  4. import static org.junit.Assert.assertEquals;
  5. import static org.junit.Assert.assertTrue;
  6.  
  7. import java.util.Collection;
  8.  
  9. import javax.persistence.Entity;
  10. import javax.persistence.FetchType;
  11. import javax.persistence.OneToOne;
  12.  
  13. import org.hibernate.Hibernate;
  14. import org.hibernate.Session;
  15. import org.hibernate.Transaction;
  16. import org.hibernate.graph.RootGraph;
  17. import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
  18. import org.junit.After;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21.  
  22. public class FetchWithRootGraphTest extends BaseCoreFunctionalTestCase {
  23.  
  24.     @Entity
  25.     class SimpleEntity {
  26.         int id;
  27.         String text;
  28.         SimpleEntity(int id, String text) {
  29.             this.id = id;
  30.             this.text = text;
  31.         }
  32.         public int getId() {
  33.             return id;
  34.         }
  35.         public void setId(int id) {
  36.             this.id = id;
  37.         }
  38.         public String getText() {
  39.             return text;
  40.         }
  41.         public void setText(String text) {
  42.             this.text = text;
  43.         }
  44.     }
  45.    
  46.     @Entity
  47.     class EntityWithReference {
  48.         int id;
  49.        
  50.         @OneToOne(fetch=FetchType.LAZY)
  51.         SimpleEntity reference;
  52.        
  53.         public EntityWithReference(int id, SimpleEntity ref) {
  54.             this.id = id;
  55.             this.reference = ref;
  56.         }
  57.         public int getId() {
  58.             return id;
  59.         }
  60.         public void setId(int id) {
  61.             this.id = id;
  62.         }
  63.         public SimpleEntity getReference() {
  64.             return reference;
  65.         }
  66.         public void setReference(SimpleEntity reference) {
  67.             this.reference = reference;
  68.         }
  69.        
  70.     }
  71.    
  72.    
  73.     @Override
  74.     protected Class[] getAnnotatedClasses() {
  75.         return new Class[] {
  76.             SimpleEntity.class,
  77.             EntityWithReference.class
  78.         };
  79.     }
  80.    
  81.     @Before
  82.     public void before() {
  83.         Session s = openSession();
  84.         Transaction tx = s.beginTransaction();
  85.        
  86.         for(int i = 0; i < 10; ++i) {
  87.             SimpleEntity sim = new SimpleEntity(i, "Entity #" + i);
  88.             EntityWithReference ref = new EntityWithReference(i, sim);
  89.             s.save(sim);
  90.             s.save(ref);
  91.         }
  92.         tx.commit();
  93.         s.close();
  94.    
  95.    
  96.     }
  97.    
  98.    
  99.     @After
  100.     public void after() {
  101.         Session session = sessionFactory().openSession();
  102.         session.getTransaction().begin();
  103.         session.createQuery( "delete simpleentity" ).executeUpdate();
  104.         session.createQuery( "delete entitywithreference" ).executeUpdate();
  105.         session.getTransaction().commit();
  106.         session.close();
  107.     }
  108.    
  109.     @Test
  110.     public void hhh13312Test() throws Exception {
  111.         Session s = openSession();
  112.         Transaction tx = s.beginTransaction();
  113.        
  114.         RootGraph<EntityWithReference> g = s.createEntityGraph(EntityWithReference.class);
  115.         g.addAttributeNode("reference");
  116.        
  117.         EntityWithReference single  = s.byId(EntityWithReference.class)
  118.                 .with(g)
  119.                 .load(3L);
  120.        
  121.         assertEquals((long)single.getId(), 3L);
  122.         assertTrue(Hibernate.isInitialized(single.getReference()));
  123.        
  124.        
  125.         Collection<EntityWithReference> multiple = s.byMultipleIds(EntityWithReference.class)
  126.                 .with(g)
  127.                 .multiLoad(1L, 2L, 3L);
  128.         assertEquals(multiple.size(), 3);
  129.         multiple.stream()
  130.             .map(EntityWithReference::getReference)
  131.             .map(Hibernate::isInitialized)
  132.             .forEach(b -> assertTrue(b));
  133.        
  134.         tx.commit();
  135.         s.close();
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement