Use JPA @Enumerated to convert enum to string (#466)

This commit is contained in:
Shicong Huang 2020-02-04 10:50:03 -05:00 committed by GitHub
parent ed38da628c
commit 79b7e9f023
7 changed files with 9 additions and 159 deletions

View file

@ -89,9 +89,10 @@ import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides; import javax.persistence.AttributeOverrides;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Embedded; import javax.persistence.Embedded;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Transient; import javax.persistence.Transient;
import org.hibernate.annotations.Type;
import org.joda.money.CurrencyUnit; import org.joda.money.CurrencyUnit;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@ -253,9 +254,11 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
/** The type of this registrar. */ /** The type of this registrar. */
@Column(nullable = false) @Column(nullable = false)
@Enumerated(EnumType.STRING)
Type type; Type type;
/** The state of this registrar. */ /** The state of this registrar. */
@Enumerated(EnumType.STRING)
State state; State state;
/** The set of TLDs which this registrar is allowed to access. */ /** The set of TLDs which this registrar is allowed to access. */

View file

@ -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<T extends Enum<T>> implements AttributeConverter<T, String> {
@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<T>(getClass()) {}.getExactType(), dbData);
}
}

View file

@ -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<Registrar.State> {}

View file

@ -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<Registrar.Type> {}

View file

@ -41,8 +41,6 @@
<class>google.registry.persistence.CreateAutoTimestampConverter</class> <class>google.registry.persistence.CreateAutoTimestampConverter</class>
<class>google.registry.persistence.CurrencyUnitConverter</class> <class>google.registry.persistence.CurrencyUnitConverter</class>
<class>google.registry.persistence.DateTimeConverter</class> <class>google.registry.persistence.DateTimeConverter</class>
<class>google.registry.persistence.RegistrarStateConverter</class>
<class>google.registry.persistence.RegistrarTypeConverter</class>
<class>google.registry.persistence.UpdateAutoTimestampConverter</class> <class>google.registry.persistence.UpdateAutoTimestampConverter</class>
<class>google.registry.persistence.ZonedDateTimeConverter</class> <class>google.registry.persistence.ZonedDateTimeConverter</class>

View file

@ -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;
}
}
}

View file

@ -22,15 +22,17 @@ import google.registry.model.registrar.Registrar.State;
import google.registry.persistence.transaction.JpaTestRules; import google.registry.persistence.transaction.JpaTestRules;
import google.registry.persistence.transaction.JpaTestRules.JpaUnitTestRule; import google.registry.persistence.transaction.JpaTestRules.JpaUnitTestRule;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id; import javax.persistence.Id;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
/** Unit tests for {@link RegistrarStateConverter}. */ /** Unit tests for {@link Enumerated} annotation. */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class RegistrarStateConverterTest { public class StringValueEnumeratedTest {
@Rule @Rule
public final JpaUnitTestRule jpaRule = public final JpaUnitTestRule jpaRule =
@ -66,6 +68,7 @@ public class RegistrarStateConverterTest {
@Id String name = "id"; @Id String name = "id";
@Enumerated(EnumType.STRING)
State state; State state;
private TestEntity() {} private TestEntity() {}