|
| 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.test.collection.basic; |
| 25 | + |
| 26 | +import java.util.HashSet; |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +import org.hibernate.Session; |
| 30 | + |
| 31 | +import org.junit.Assert; |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +import org.hibernate.testing.FailureExpected; |
| 35 | +import org.hibernate.testing.TestForIssue; |
| 36 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 37 | + |
| 38 | +public class JoinFetchElementCollectionTest extends BaseCoreFunctionalTestCase { |
| 39 | + @Override |
| 40 | + protected Class<?>[] getAnnotatedClasses() { |
| 41 | + return new Class[] {Contact.class, EmailAddress.class, User.class}; |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + @TestForIssue(jiraKey = "HHH-8206") |
| 46 | + @FailureExpected(jiraKey = "HHH-8206", message = "This is not explicitly supported, however should arguably throw an exception") |
| 47 | + public void testJoinFetchesByPath() { |
| 48 | + Set<EmailAddress> emailAddresses = new HashSet<EmailAddress>(); |
| 49 | + emailAddresses.add( new EmailAddress( "test1@test.com" ) ); |
| 50 | + emailAddresses.add( new EmailAddress( "test2@test.com" ) ); |
| 51 | + emailAddresses.add( new EmailAddress( "test3@test.com" ) ); |
| 52 | + |
| 53 | + { |
| 54 | + // Session 1: Insert a user with email addresses but no emailAddresses2 |
| 55 | + Session session = openSession(); |
| 56 | + session.beginTransaction(); |
| 57 | + |
| 58 | + User user = new User(); |
| 59 | + user.setName( "john" ); |
| 60 | + Contact contact = new Contact(); |
| 61 | + contact.setName( "John Doe" ); |
| 62 | + contact.setEmailAddresses( emailAddresses ); |
| 63 | + contact = (Contact) session.merge( contact ); |
| 64 | + user.setContact( contact ); |
| 65 | + user = (User) session.merge( user ); |
| 66 | + |
| 67 | + session.getTransaction().commit(); |
| 68 | + session.close(); |
| 69 | + } |
| 70 | + { |
| 71 | + // Session 2: Retrieve the user object and check if the sets have the expected values |
| 72 | + Session session = openSession(); |
| 73 | + session.beginTransaction(); |
| 74 | + final String qry = "SELECT user " |
| 75 | + + "FROM User user " |
| 76 | + + "LEFT OUTER JOIN FETCH user.contact " |
| 77 | + + "LEFT OUTER JOIN FETCH user.contact.emailAddresses2 " |
| 78 | + + "LEFT OUTER JOIN FETCH user.contact.emailAddresses"; |
| 79 | + User user = (User) session.createQuery( qry ).uniqueResult(); |
| 80 | + session.getTransaction().commit(); |
| 81 | + session.close(); |
| 82 | + |
| 83 | + Assert.assertEquals( emailAddresses, user.getContact().getEmailAddresses() ); |
| 84 | + Assert.assertTrue( user.getContact().getEmailAddresses2().isEmpty() ); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + @TestForIssue(jiraKey = "HHH-5465") |
| 91 | + public void testJoinFetchElementCollection() { |
| 92 | + Set<EmailAddress> emailAddresses = new HashSet<EmailAddress>(); |
| 93 | + emailAddresses.add( new EmailAddress( "test1@test.com" ) ); |
| 94 | + emailAddresses.add( new EmailAddress( "test2@test.com" ) ); |
| 95 | + emailAddresses.add( new EmailAddress( "test3@test.com" ) ); |
| 96 | + |
| 97 | + { |
| 98 | + // Session 1: Insert a user with email addresses but no emailAddresses2 |
| 99 | + Session session = openSession(); |
| 100 | + session.beginTransaction(); |
| 101 | + |
| 102 | + User user = new User(); |
| 103 | + user.setName( "john" ); |
| 104 | + Contact contact = new Contact(); |
| 105 | + contact.setName( "John Doe" ); |
| 106 | + contact.setEmailAddresses( emailAddresses ); |
| 107 | + contact = (Contact) session.merge( contact ); |
| 108 | + user.setContact( contact ); |
| 109 | + user = (User) session.merge( user ); |
| 110 | + |
| 111 | + session.getTransaction().commit(); |
| 112 | + session.close(); |
| 113 | + } |
| 114 | + { |
| 115 | + // Session 2: Retrieve the user object and check if the sets have the expected values |
| 116 | + Session session = openSession(); |
| 117 | + session.beginTransaction(); |
| 118 | + final String qry = "SELECT user " |
| 119 | + + "FROM User user " |
| 120 | + + "LEFT OUTER JOIN FETCH user.contact c " |
| 121 | + + "LEFT OUTER JOIN FETCH c.emailAddresses2 " |
| 122 | + + "LEFT OUTER JOIN FETCH c.emailAddresses"; |
| 123 | + User user = (User) session.createQuery( qry ).uniqueResult(); |
| 124 | + session.getTransaction().commit(); |
| 125 | + session.close(); |
| 126 | + |
| 127 | + Assert.assertEquals( emailAddresses, user.getContact().getEmailAddresses() ); |
| 128 | + Assert.assertTrue( user.getContact().getEmailAddresses2().isEmpty() ); |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments