|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as |
| 5 | + * indicated by the @author tags or express copyright attribution |
| 6 | + * statements applied by the authors. All third-party contributions are |
| 7 | + * distributed under license by Red Hat Inc. |
| 8 | + * |
| 9 | + * This copyrighted material is made available to anyone wishing to use, modify, |
| 10 | + * copy, or redistribute it subject to the terms and conditions of the GNU |
| 11 | + * Lesser General Public License, as published by the Free Software Foundation. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 16 | + * for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public License |
| 19 | + * along with this distribution; if not, write to: |
| 20 | + * Free Software Foundation, Inc. |
| 21 | + * 51 Franklin Street, Fifth Floor |
| 22 | + * Boston, MA 02110-1301 USA |
| 23 | + */ |
| 24 | +package org.hibernate.jpa.test.graphs.find; |
| 25 | + |
| 26 | +import org.hibernate.Hibernate; |
| 27 | +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +import javax.persistence.*; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +import static org.junit.Assert.*; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Christian Bauer |
| 38 | + */ |
| 39 | +public class FindEntityGraphTests extends BaseEntityManagerFunctionalTestCase { |
| 40 | + |
| 41 | + @Override |
| 42 | + protected Class<?>[] getAnnotatedClasses() { |
| 43 | + return new Class[]{Foo.class, Bar.class, Baz.class}; |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void loadParallelManyToOne() { |
| 48 | + EntityManager em = getOrCreateEntityManager(); |
| 49 | + em.getTransaction().begin(); |
| 50 | + |
| 51 | + Bar bar = new Bar(); |
| 52 | + bar.id = 1; |
| 53 | + bar.name = "bar"; |
| 54 | + em.persist(bar); |
| 55 | + |
| 56 | + Baz baz = new Baz(); |
| 57 | + baz.id = 2; |
| 58 | + baz.name = "baz"; |
| 59 | + em.persist(baz); |
| 60 | + |
| 61 | + Foo foo = new Foo(); |
| 62 | + foo.id = 3; |
| 63 | + foo.name = "foo"; |
| 64 | + foo.bar = bar; |
| 65 | + foo.baz = baz; |
| 66 | + em.persist(foo); |
| 67 | + |
| 68 | + em.getTransaction().commit(); |
| 69 | + em.close(); |
| 70 | + |
| 71 | + em = getOrCreateEntityManager(); |
| 72 | + em.getTransaction().begin(); |
| 73 | + |
| 74 | + EntityGraph<Foo> fooGraph = em.createEntityGraph(Foo.class); |
| 75 | + fooGraph.addAttributeNodes("bar", "baz"); |
| 76 | + |
| 77 | + Map<String, Object> properties = new HashMap<String, Object>(); |
| 78 | + properties.put("javax.persistence.loadgraph", fooGraph); |
| 79 | + |
| 80 | + Foo result = em.find(Foo.class, foo.id, properties); |
| 81 | + |
| 82 | + assertTrue(Hibernate.isInitialized(result)); |
| 83 | + assertTrue(Hibernate.isInitialized(result.bar)); |
| 84 | + assertTrue(Hibernate.isInitialized(result.baz)); |
| 85 | + |
| 86 | + em.getTransaction().commit(); |
| 87 | + em.close(); |
| 88 | + } |
| 89 | + |
| 90 | + @Entity |
| 91 | + public static class Foo { |
| 92 | + |
| 93 | + @Id |
| 94 | + public Integer id; |
| 95 | + |
| 96 | + public String name; |
| 97 | + |
| 98 | + @ManyToOne(fetch = FetchType.LAZY) |
| 99 | + public Bar bar; |
| 100 | + |
| 101 | + @ManyToOne(fetch = FetchType.LAZY) |
| 102 | + public Baz baz; |
| 103 | + } |
| 104 | + |
| 105 | + @Entity |
| 106 | + public static class Bar { |
| 107 | + |
| 108 | + @Id |
| 109 | + public Integer id; |
| 110 | + |
| 111 | + public String name; |
| 112 | + } |
| 113 | + |
| 114 | + @Entity |
| 115 | + public static class Baz { |
| 116 | + |
| 117 | + @Id |
| 118 | + public Integer id; |
| 119 | + |
| 120 | + public String name; |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments