diff --git a/core/src/main/java/google/registry/model/registrar/Registrar.java b/core/src/main/java/google/registry/model/registrar/Registrar.java index 6825e80c3..6b4e828fb 100644 --- a/core/src/main/java/google/registry/model/registrar/Registrar.java +++ b/core/src/main/java/google/registry/model/registrar/Registrar.java @@ -89,9 +89,10 @@ import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.Embedded; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; import javax.persistence.Table; import javax.persistence.Transient; -import org.hibernate.annotations.Type; import org.joda.money.CurrencyUnit; import org.joda.time.DateTime; @@ -253,9 +254,11 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable /** The type of this registrar. */ @Column(nullable = false) + @Enumerated(EnumType.STRING) Type type; /** The state of this registrar. */ + @Enumerated(EnumType.STRING) State state; /** The set of TLDs which this registrar is allowed to access. */ diff --git a/core/src/main/java/google/registry/persistence/GenericEnumConverter.java b/core/src/main/java/google/registry/persistence/GenericEnumConverter.java deleted file mode 100644 index 3fd9ee2c2..000000000 --- a/core/src/main/java/google/registry/persistence/GenericEnumConverter.java +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2020 The Nomulus Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package google.registry.persistence; - -import google.registry.util.TypeUtils.TypeInstantiator; -import javax.persistence.AttributeConverter; - -/** Generic converter for storing/retrieving {@link Enum} objects. */ -public class GenericEnumConverter> implements AttributeConverter { - - @Override - public String convertToDatabaseColumn(T attribute) { - return attribute == null ? null : attribute.toString(); - } - - @Override - public T convertToEntityAttribute(String dbData) { - return dbData == null - ? null - : Enum.valueOf(new TypeInstantiator(getClass()) {}.getExactType(), dbData); - } -} diff --git a/core/src/main/java/google/registry/persistence/RegistrarStateConverter.java b/core/src/main/java/google/registry/persistence/RegistrarStateConverter.java deleted file mode 100644 index 24646dc28..000000000 --- a/core/src/main/java/google/registry/persistence/RegistrarStateConverter.java +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2020 The Nomulus Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package google.registry.persistence; - -import google.registry.model.registrar.Registrar; -import javax.persistence.Converter; - -/** JPA converter for storing/retrieving {@link Registrar.State} objects. */ -@Converter(autoApply = true) -public class RegistrarStateConverter extends GenericEnumConverter {} diff --git a/core/src/main/java/google/registry/persistence/RegistrarTypeConverter.java b/core/src/main/java/google/registry/persistence/RegistrarTypeConverter.java deleted file mode 100644 index a9a1f9dd7..000000000 --- a/core/src/main/java/google/registry/persistence/RegistrarTypeConverter.java +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2020 The Nomulus Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package google.registry.persistence; - -import google.registry.model.registrar.Registrar; -import javax.persistence.Converter; - -/** JPA converter for storing/retrieving {@link Registrar.Type} objects. */ -@Converter(autoApply = true) -public class RegistrarTypeConverter extends GenericEnumConverter {} diff --git a/core/src/main/resources/META-INF/persistence.xml b/core/src/main/resources/META-INF/persistence.xml index 403c310ab..27d763139 100644 --- a/core/src/main/resources/META-INF/persistence.xml +++ b/core/src/main/resources/META-INF/persistence.xml @@ -41,8 +41,6 @@ google.registry.persistence.CreateAutoTimestampConverter google.registry.persistence.CurrencyUnitConverter google.registry.persistence.DateTimeConverter - google.registry.persistence.RegistrarStateConverter - google.registry.persistence.RegistrarTypeConverter google.registry.persistence.UpdateAutoTimestampConverter google.registry.persistence.ZonedDateTimeConverter diff --git a/core/src/test/java/google/registry/persistence/RegistrarTypeConverterTest.java b/core/src/test/java/google/registry/persistence/RegistrarTypeConverterTest.java deleted file mode 100644 index c309c74ff..000000000 --- a/core/src/test/java/google/registry/persistence/RegistrarTypeConverterTest.java +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2020 The Nomulus Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package google.registry.persistence; - -import static com.google.common.truth.Truth.assertThat; -import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; - -import google.registry.model.ImmutableObject; -import google.registry.model.registrar.Registrar.Type; -import google.registry.persistence.transaction.JpaTestRules; -import google.registry.persistence.transaction.JpaTestRules.JpaUnitTestRule; -import javax.persistence.Entity; -import javax.persistence.Id; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit tests for {@link RegistrarTypeConverter}. */ -@RunWith(JUnit4.class) -public class RegistrarTypeConverterTest { - @Rule - public final JpaUnitTestRule jpaRule = - new JpaTestRules.Builder().withEntityClass(TestEntity.class).buildUnitTestRule(); - - @Test - public void roundTripConversion_returnsSameEnum() { - TestEntity testEntity = new TestEntity(Type.MONITORING); - jpaTm().transact(() -> jpaTm().getEntityManager().persist(testEntity)); - TestEntity persisted = - jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id")); - assertThat(persisted.type).isEqualTo(Type.MONITORING); - } - - @Test - public void testNativeQuery_succeeds() { - TestEntity testEntity = new TestEntity(Type.MONITORING); - jpaTm().transact(() -> jpaTm().getEntityManager().persist(testEntity)); - - assertThat( - jpaTm() - .transact( - () -> - jpaTm() - .getEntityManager() - .createNativeQuery("SELECT type FROM \"TestEntity\" WHERE name = 'id'") - .getSingleResult())) - .isEqualTo("MONITORING"); - } - - @Entity(name = "TestEntity") // Override entity name to avoid the nested class reference. - private static class TestEntity extends ImmutableObject { - - @Id String name = "id"; - - Type type; - - private TestEntity() {} - - private TestEntity(Type type) { - this.type = type; - } - } -} diff --git a/core/src/test/java/google/registry/persistence/RegistrarStateConverterTest.java b/core/src/test/java/google/registry/persistence/StringValueEnumeratedTest.java similarity index 92% rename from core/src/test/java/google/registry/persistence/RegistrarStateConverterTest.java rename to core/src/test/java/google/registry/persistence/StringValueEnumeratedTest.java index 2a2ff8eaa..6a59375f3 100644 --- a/core/src/test/java/google/registry/persistence/RegistrarStateConverterTest.java +++ b/core/src/test/java/google/registry/persistence/StringValueEnumeratedTest.java @@ -22,15 +22,17 @@ import google.registry.model.registrar.Registrar.State; import google.registry.persistence.transaction.JpaTestRules; import google.registry.persistence.transaction.JpaTestRules.JpaUnitTestRule; import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; import javax.persistence.Id; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** Unit tests for {@link RegistrarStateConverter}. */ +/** Unit tests for {@link Enumerated} annotation. */ @RunWith(JUnit4.class) -public class RegistrarStateConverterTest { +public class StringValueEnumeratedTest { @Rule public final JpaUnitTestRule jpaRule = @@ -66,6 +68,7 @@ public class RegistrarStateConverterTest { @Id String name = "id"; + @Enumerated(EnumType.STRING) State state; private TestEntity() {}