|
| 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.orm.test.mapping.identifier; |
| 8 | + |
| 9 | +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; |
| 10 | +import static org.junit.Assert.assertNull; |
| 11 | +import static org.junit.Assert.assertSame; |
| 12 | + |
| 13 | +import org.hibernate.Session; |
| 14 | +import org.hibernate.annotations.NaturalId; |
| 15 | +import org.hibernate.internal.SessionImpl; |
| 16 | +import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +import jakarta.persistence.Entity; |
| 20 | +import jakarta.persistence.Id; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Gary Gregory |
| 24 | + */ |
| 25 | +public class MutableNaturalIdsTest extends BaseEntityManagerFunctionalTestCase { |
| 26 | + |
| 27 | + private static final String FIELD_1 = "email1"; |
| 28 | + private static final String FIELD_2 = "email2"; |
| 29 | + private static final String FIELD_3 = "email3"; |
| 30 | + |
| 31 | + private static final String OLD_VALUE_1 = "[email protected]"; |
| 32 | + private static final String OLD_VALUE_2 = "[email protected]"; |
| 33 | + private static final String OLD_VALUE_3 = "[email protected]"; |
| 34 | + |
| 35 | + private static final String NEW_VALUE_1 = "[email protected]"; |
| 36 | + private static final String NEW_VALUE_2 = "[email protected]"; |
| 37 | + |
| 38 | + @Override |
| 39 | + protected Class<?>[] getAnnotatedClasses() { |
| 40 | + return new Class<?>[] { |
| 41 | + Author.class |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void test() { |
| 47 | + doInJPA(this::entityManagerFactory, entityManager -> { |
| 48 | + Author author = new Author(); |
| 49 | + author.setId(1L); |
| 50 | + author.setName("John Doe"); |
| 51 | + author.setEmail1(OLD_VALUE_1); |
| 52 | + author.setEmail2(OLD_VALUE_2); |
| 53 | + author.setEmail3(OLD_VALUE_3); |
| 54 | + |
| 55 | + entityManager.persist(author); |
| 56 | + }); |
| 57 | + doInJPA(this::entityManagerFactory, entityManager -> { |
| 58 | + Author author = entityManager |
| 59 | + .unwrap(Session.class) |
| 60 | + .byNaturalId(Author.class) |
| 61 | + .using(FIELD_1, OLD_VALUE_1) |
| 62 | + .using(FIELD_2, OLD_VALUE_2) |
| 63 | + .using(FIELD_3, OLD_VALUE_3) |
| 64 | + .load(); |
| 65 | + |
| 66 | + author.setEmail1(NEW_VALUE_1); |
| 67 | + author.setEmail2(NEW_VALUE_2); |
| 68 | + |
| 69 | + assertNull( |
| 70 | + entityManager |
| 71 | + .unwrap(Session.class) |
| 72 | + .byNaturalId(Author.class) |
| 73 | + .setSynchronizationEnabled(false) |
| 74 | + .using(FIELD_1, NEW_VALUE_1) |
| 75 | + .using(FIELD_2, NEW_VALUE_2) |
| 76 | + .using(FIELD_3, OLD_VALUE_3) |
| 77 | + .load() |
| 78 | + ); |
| 79 | + |
| 80 | + assertSame(author, |
| 81 | + entityManager |
| 82 | + .unwrap(Session.class) |
| 83 | + .byNaturalId(Author.class) |
| 84 | + .setSynchronizationEnabled(true) |
| 85 | + .using(FIELD_1, NEW_VALUE_1) |
| 86 | + .using(FIELD_2, NEW_VALUE_2) |
| 87 | + .using(FIELD_3, OLD_VALUE_3) |
| 88 | + .load() |
| 89 | + ); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + @Entity(name = "Author") |
| 94 | + public static class Author { |
| 95 | + |
| 96 | + @Id |
| 97 | + private Long id; |
| 98 | + |
| 99 | + private String name; |
| 100 | + |
| 101 | + @NaturalId(mutable = true) |
| 102 | + private String email1; |
| 103 | + |
| 104 | + @NaturalId(mutable = true) |
| 105 | + private String email2; |
| 106 | + |
| 107 | + @NaturalId |
| 108 | + private String email3; |
| 109 | + |
| 110 | + public Long getId() { |
| 111 | + return id; |
| 112 | + } |
| 113 | + |
| 114 | + public void setId(Long id) { |
| 115 | + this.id = id; |
| 116 | + } |
| 117 | + |
| 118 | + public String getName() { |
| 119 | + return name; |
| 120 | + } |
| 121 | + |
| 122 | + public void setName(String name) { |
| 123 | + this.name = name; |
| 124 | + } |
| 125 | + |
| 126 | + public String getEmail1() { |
| 127 | + return email1; |
| 128 | + } |
| 129 | + |
| 130 | + public String getEmail2() { |
| 131 | + return email2; |
| 132 | + } |
| 133 | + |
| 134 | + public String getEmail3() { |
| 135 | + return email3; |
| 136 | + } |
| 137 | + |
| 138 | + public void setEmail1(String email) { |
| 139 | + this.email1 = email; |
| 140 | + } |
| 141 | + |
| 142 | + public void setEmail2(String email2) { |
| 143 | + this.email2 = email2; |
| 144 | + } |
| 145 | + |
| 146 | + public void setEmail3(String email3) { |
| 147 | + this.email3 = email3; |
| 148 | + } |
| 149 | + |
| 150 | + } |
| 151 | +} |
0 commit comments