|
| 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.onetoone.flush; |
| 8 | + |
| 9 | +import javax.persistence.Entity; |
| 10 | +import javax.persistence.Id; |
| 11 | +import javax.persistence.OneToOne; |
| 12 | +import javax.persistence.Table; |
| 13 | +import javax.persistence.Version; |
| 14 | + |
| 15 | +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; |
| 16 | + |
| 17 | +import org.hibernate.testing.TestForIssue; |
| 18 | +import org.junit.After; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import static org.hibernate.testing.orm.transaction.TransactionUtil.inTransaction; |
| 23 | +import static org.junit.Assert.assertEquals; |
| 24 | + |
| 25 | +@TestForIssue(jiraKey = "HHH-15045") |
| 26 | +public class DirtyFlushTest extends BaseEntityManagerFunctionalTestCase { |
| 27 | + |
| 28 | + @Override |
| 29 | + protected Class<?>[] getAnnotatedClasses() { |
| 30 | + return new Class[] { User.class, Profile.class }; |
| 31 | + } |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setUp() { |
| 35 | + inTransaction( getOrCreateEntityManager(), em -> { |
| 36 | + final User user = new User(); |
| 37 | + user.id = 1; |
| 38 | + user.version = 1; |
| 39 | + |
| 40 | + final Profile profile = new Profile(); |
| 41 | + profile.id = 1; |
| 42 | + profile.version = 1; |
| 43 | + |
| 44 | + em.persist( user ); |
| 45 | + em.persist( profile ); |
| 46 | + } ); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testDirtyFlushNotHappened() { |
| 51 | + inTransaction( getOrCreateEntityManager(), em -> { |
| 52 | + final User user = em.find( User.class, 1 ); |
| 53 | + assertEquals( 1, user.version ); |
| 54 | + |
| 55 | + final Profile profile = em.find( Profile.class, 1 ); |
| 56 | + assertEquals( 1, profile.version ); |
| 57 | + |
| 58 | + profile.user = user; |
| 59 | + user.profile = profile; |
| 60 | + |
| 61 | + em.persist( profile ); |
| 62 | + em.flush(); |
| 63 | + } ); |
| 64 | + |
| 65 | + inTransaction( getOrCreateEntityManager(), em -> { |
| 66 | + final Profile profile = em.find( Profile.class, 1 ); |
| 67 | + assertEquals( 2, profile.version ); |
| 68 | + |
| 69 | + final User user = em.find( User.class, 1 ); |
| 70 | + assertEquals( |
| 71 | + "without fixing, the version will be bumped due to erroneous dirty flushing", |
| 72 | + 1, |
| 73 | + user.version |
| 74 | + ); |
| 75 | + } ); |
| 76 | + } |
| 77 | + |
| 78 | + @After |
| 79 | + public void tearDown() { |
| 80 | + inTransaction( getOrCreateEntityManager(), em -> { |
| 81 | + em.createQuery( "delete from Profile" ).executeUpdate(); |
| 82 | + em.createQuery( "delete from User" ).executeUpdate(); |
| 83 | + } ); |
| 84 | + } |
| 85 | + |
| 86 | + @Entity(name = "User") |
| 87 | + @Table(name = "USER_TABLE") |
| 88 | + public static class User { |
| 89 | + @Id |
| 90 | + int id; |
| 91 | + |
| 92 | + @Version |
| 93 | + int version; |
| 94 | + |
| 95 | + @OneToOne(mappedBy = "user") |
| 96 | + Profile profile; |
| 97 | + } |
| 98 | + |
| 99 | + @Entity(name = "Profile") |
| 100 | + @Table(name = "PROFILE_TABLE") |
| 101 | + public static class Profile { |
| 102 | + @Id |
| 103 | + int id; |
| 104 | + |
| 105 | + @Version |
| 106 | + int version; |
| 107 | + @OneToOne // internally Hibernate will use `@ManyToOne` for this field |
| 108 | + User user; |
| 109 | + } |
| 110 | +} |
0 commit comments