|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.test.formulajoin; |
| 8 | + |
| 9 | +import org.hibernate.Hibernate; |
| 10 | + |
| 11 | +import org.hibernate.testing.TestForIssue; |
| 12 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Ignore; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | +import static org.junit.Assert.assertFalse; |
| 19 | +import static org.junit.Assert.assertNotNull; |
| 20 | +import static org.junit.Assert.assertNull; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +@TestForIssue(jiraKey = "HHH-9952") |
| 24 | +public class AssociationFormulaTest extends BaseCoreFunctionalTestCase { |
| 25 | + |
| 26 | + public AssociationFormulaTest() { |
| 27 | + super(); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + protected boolean isCleanupTestDataRequired() { |
| 32 | + return true; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + protected boolean rebuildSessionFactoryOnError() { |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String[] getMappings() { |
| 42 | + return new String[] { "formulajoin/Mapping.hbm.xml" }; |
| 43 | + } |
| 44 | + |
| 45 | + @Before |
| 46 | + public void fillDb() { |
| 47 | + |
| 48 | + Entity entity = new Entity(); |
| 49 | + entity.setId( new Id( "test", 1 ) ); |
| 50 | + entity.setOther( new OtherEntity() ); |
| 51 | + entity.getOther().setId( new Id( "test", 2 ) ); |
| 52 | + |
| 53 | + Entity otherNull = new Entity(); |
| 54 | + otherNull.setId( new Id( "null", 3 ) ); |
| 55 | + |
| 56 | + inTransaction( |
| 57 | + session -> { |
| 58 | + session.merge( entity ); |
| 59 | + session.merge( otherNull ); |
| 60 | + } |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testJoin() { |
| 66 | + |
| 67 | + inTransaction( |
| 68 | + session -> { |
| 69 | + Entity loaded = (Entity) session.createQuery( "select e from Entity e inner join e.other o" ) |
| 70 | + .uniqueResult(); |
| 71 | + assertNotNull( "loaded", loaded ); |
| 72 | + assertEquals( 1, loaded.getId().getId() ); |
| 73 | + assertEquals( 2, loaded.getOther().getId().getId() ); |
| 74 | + assertFalse( Hibernate.isInitialized( loaded.getOther() ) ); |
| 75 | + Hibernate.initialize( loaded.getOther() ); |
| 76 | + } |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testJoinFetch() { |
| 82 | + inTransaction( |
| 83 | + session -> { |
| 84 | + Entity loaded = (Entity) session.createQuery( "select e from Entity e inner join fetch e.other o" ) |
| 85 | + .uniqueResult(); |
| 86 | + assertNotNull( "loaded", loaded ); |
| 87 | + assertEquals( 1, loaded.getId().getId() ); |
| 88 | + assertTrue( Hibernate.isInitialized( loaded.getOther() ) ); |
| 89 | + assertEquals( 2, loaded.getOther().getId().getId() ); |
| 90 | + } |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testSelectFullNull() { |
| 96 | + inTransaction( |
| 97 | + session -> { |
| 98 | + Entity loaded = (Entity) session.createQuery( "from Entity e where e.other is null" ) |
| 99 | + .uniqueResult(); |
| 100 | + assertNotNull( "loaded", loaded ); |
| 101 | + assertEquals( 3, loaded.getId().getId() ); |
| 102 | + assertNull( loaded.getOther() ); |
| 103 | + |
| 104 | + } |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testSelectPartialNull() { |
| 110 | + inTransaction( |
| 111 | + session -> { |
| 112 | + Entity loaded = (Entity) session.createQuery( "from Entity e where e.other.id.id is null" ) |
| 113 | + .uniqueResult(); |
| 114 | + assertNotNull( "loaded", loaded ); |
| 115 | + assertEquals( 3, loaded.getId().getId() ); |
| 116 | + assertNull( loaded.getOther() ); |
| 117 | + } |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testSelectFull() { |
| 123 | + inTransaction( |
| 124 | + session -> { |
| 125 | + OtherEntity other = new OtherEntity(); |
| 126 | + other.setId( new Id( "test", 2 ) ); |
| 127 | + Entity loaded = (Entity) session.createQuery( "from Entity e where e.other = :other" ) |
| 128 | + .setParameter( "other", other ) |
| 129 | + .uniqueResult(); |
| 130 | + assertNotNull( "loaded", loaded ); |
| 131 | + assertEquals( 1, loaded.getId().getId() ); |
| 132 | + assertNotNull( loaded.getOther() ); |
| 133 | + assertEquals( 2, loaded.getOther().getId().getId() ); |
| 134 | + } |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testUpdateFromExisting() { |
| 140 | + inTransaction( |
| 141 | + session -> { |
| 142 | + Entity loaded = (Entity) session.createQuery( "from Entity e where e.id.id = 1" ).uniqueResult(); |
| 143 | + assertNotNull( "loaded", loaded ); |
| 144 | + assertNotNull( loaded.getOther() ); |
| 145 | + loaded.setOther( new OtherEntity() ); |
| 146 | + loaded.getOther().setId( new Id( "test", 3 ) ); |
| 147 | + } |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testUpdateFromNull() { |
| 153 | + inTransaction( |
| 154 | + session -> { |
| 155 | + Entity loaded = (Entity) session.createQuery( "from Entity e where e.id.id = 3" ).uniqueResult(); |
| 156 | + assertNotNull( "loaded", loaded ); |
| 157 | + assertNull( loaded.getOther() ); |
| 158 | + loaded.setOther( new OtherEntity() ); |
| 159 | + loaded.getOther().setId( new Id( "test", 3 ) ); |
| 160 | + } |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + @Ignore("multi-column updates don't work!") |
| 166 | + public void testUpdateHql() { |
| 167 | + inTransaction( |
| 168 | + session -> { |
| 169 | + OtherEntity other = new OtherEntity(); |
| 170 | + other.setId( new Id( "null", 4 ) ); |
| 171 | + assertEquals( |
| 172 | + "execute", |
| 173 | + 1, |
| 174 | + session.createQuery( "update Entity e set e.other = :other where e.id.id = 3" ) |
| 175 | + .setParameter( "other", other ) |
| 176 | + .executeUpdate() |
| 177 | + ); |
| 178 | + Entity loaded = (Entity) session.createQuery( "from Entity e where e.id.id = 3" ).uniqueResult(); |
| 179 | + assertNotNull( "loaded", loaded ); |
| 180 | + assertEquals( 4, loaded.getOther().getId().getId() ); |
| 181 | + } |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + @Ignore("multi-column updates don't work!") |
| 187 | + public void testUpdateHqlNull() { |
| 188 | + inTransaction( |
| 189 | + session -> { |
| 190 | + assertEquals( |
| 191 | + "execute", |
| 192 | + 1, |
| 193 | + session.createQuery( "update Entity e set e.other = null where e.id.id = 1" ) |
| 194 | + .executeUpdate() |
| 195 | + ); |
| 196 | + Entity loaded = (Entity) session.createQuery( "from Entity e where e.id.id = 1" ).uniqueResult(); |
| 197 | + assertNotNull( "loaded", loaded ); |
| 198 | + assertEquals( 4, loaded.getOther().getId().getId() ); |
| 199 | + } |
| 200 | + ); |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + public void testDeleteHql() { |
| 205 | + inTransaction( |
| 206 | + session -> { |
| 207 | + OtherEntity other = new OtherEntity(); |
| 208 | + other.setId( new Id( "test", 2 ) ); |
| 209 | + assertEquals( |
| 210 | + "execute", |
| 211 | + 1, |
| 212 | + session.createQuery( "delete Entity e where e.other = :other" ) |
| 213 | + .setParameter( "other", other ) |
| 214 | + .executeUpdate() |
| 215 | + ); |
| 216 | + Entity loaded = (Entity) session.createQuery( "from Entity e where e.id.id = 1" ).uniqueResult(); |
| 217 | + assertNull( "loaded", loaded ); |
| 218 | + } |
| 219 | + ); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + public void testDeleteHqlNull() { |
| 224 | + inTransaction( |
| 225 | + session -> { |
| 226 | + assertEquals( |
| 227 | + "execute", |
| 228 | + 1, |
| 229 | + session.createQuery( "delete Entity e where e.other is null" ).executeUpdate() |
| 230 | + ); |
| 231 | + Entity loaded = (Entity) session.createQuery( "from Entity e where e.id.id = 3" ).uniqueResult(); |
| 232 | + assertNull( "loaded", loaded ); |
| 233 | + } |
| 234 | + ); |
| 235 | + } |
| 236 | + |
| 237 | + @Test |
| 238 | + public void testPersist() { |
| 239 | + inTransaction( |
| 240 | + session -> { |
| 241 | + Entity entity = new Entity(); |
| 242 | + entity.setId( new Id( "new", 5 ) ); |
| 243 | + entity.setOther( new OtherEntity() ); |
| 244 | + entity.getOther().setId( new Id( "new", 6 ) ); |
| 245 | + session.persist( entity ); |
| 246 | + } |
| 247 | + ); |
| 248 | + |
| 249 | + inTransaction( |
| 250 | + session -> { |
| 251 | + Entity loaded = (Entity) session.createQuery( "from Entity e where e.id.id = 5" ).uniqueResult(); |
| 252 | + assertNotNull( "loaded", loaded ); |
| 253 | + assertEquals( 6, loaded.getOther().getId().getId() ); |
| 254 | + } |
| 255 | + ); |
| 256 | + } |
| 257 | + |
| 258 | +} |
0 commit comments