Add JPA annotations to RegistrarContact (#432)

This commit is contained in:
Shicong Huang 2020-02-06 16:58:15 -05:00 committed by GitHub
parent 8b9139bc4c
commit d6f49f5c08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 142 additions and 16 deletions

View file

@ -167,10 +167,5 @@ public enum StatusValue implements EppEnum {
}
/** Hibernate type for sets of {@link StatusValue}. */
public static class StatusValueSetType extends EnumSetUserType<StatusValue> {
@Override
protected StatusValue convertToElem(String value) {
return StatusValue.valueOf(value);
}
}
public static class StatusValueSetType extends EnumSetUserType<StatusValue> {}
}

View file

@ -42,9 +42,14 @@ import google.registry.model.ImmutableObject;
import google.registry.model.JsonMapBuilder;
import google.registry.model.Jsonifiable;
import google.registry.model.annotations.ReportedOn;
import google.registry.persistence.EnumSetUserType;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.annotations.Type;
/**
* A contact for a Registrar. Note, equality, hashCode and comparable have been overridden to only
@ -56,10 +61,16 @@ import java.util.Set;
*/
@ReportedOn
@Entity
@javax.persistence.Entity
@Table(
name = "RegistrarPoc",
indexes = {
@javax.persistence.Index(columnList = "gaeUserId", name = "registrarpoc_gae_user_id_idx")
})
// TODO(shicong): Rename the class name to RegistrarPoc after database migration
public class RegistrarContact extends ImmutableObject implements Jsonifiable {
@Parent
Key<Registrar> parent;
@Parent @Transient Key<Registrar> parent;
/**
* Registrar contacts types for partner communication tracking.
@ -92,6 +103,9 @@ public class RegistrarContact extends ImmutableObject implements Jsonifiable {
this.displayName = display;
this.required = required;
}
/** Hibernate type for sets of {@link Type}. */
public static class RegistrarPocType extends EnumSetUserType<Type> {}
}
/** The name of the contact. */
@ -99,6 +113,8 @@ public class RegistrarContact extends ImmutableObject implements Jsonifiable {
/** The email address of the contact. */
@Id
@javax.persistence.Id
@Column(nullable = false)
String emailAddress;
/** The voice number of the contact. */
@ -108,9 +124,11 @@ public class RegistrarContact extends ImmutableObject implements Jsonifiable {
String faxNumber;
/**
* Multiple types are used to associate the registrar contact with
* various mailing groups. This data is internal to the registry.
* Multiple types are used to associate the registrar contact with various mailing groups. This
* data is internal to the registry.
*/
@org.hibernate.annotations.Type(
type = "google.registry.model.registrar.RegistrarContact$Type$RegistrarPocType")
Set<Type> types;
/**

View file

@ -14,6 +14,7 @@
package google.registry.persistence;
import google.registry.util.TypeUtils.TypeInstantiator;
import java.util.HashSet;
import java.util.Set;
@ -35,4 +36,16 @@ public class EnumSetUserType<E extends Enum<E>>
public Class returnedClass() {
return Set.class;
}
@Override
protected E convertToElem(String columnValue) {
return columnValue == null
? null
: Enum.valueOf(new TypeInstantiator<E>(getClass()) {}.getExactType(), columnValue);
}
@Override
protected String convertToColumn(E elementValue) {
return elementValue == null ? null : elementValue.toString();
}
}

View file

@ -21,6 +21,7 @@
-->
<class>google.registry.model.domain.DomainBase</class>
<class>google.registry.model.registrar.Registrar</class>
<class>google.registry.model.registrar.RegistrarContact</class>
<class>google.registry.schema.domain.RegistryLock</class>
<class>google.registry.schema.tmch.ClaimsList</class>
<class>google.registry.schema.cursor.Cursor</class>

View file

@ -49,17 +49,33 @@ public class EnumSetUserTypeTest {
assertThat(persisted.data).isEqualTo(enums);
}
@Test
public void testNativeQuery_succeeds() {
Set<TestEnum> enums = ImmutableSet.of(TestEnum.BAR, TestEnum.FOO);
TestEntity obj = new TestEntity("foo", enums);
jpaTm().transact(() -> jpaTm().getEntityManager().persist(obj));
assertThat(
ImmutableSet.of(
getSingleResultFromNativeQuery(
"SELECT data[1] FROM \"TestEntity\" WHERE name = 'foo'"),
getSingleResultFromNativeQuery(
"SELECT data[2] FROM \"TestEntity\" WHERE name = 'foo'")))
.containsExactly("BAR", "FOO");
}
private static Object getSingleResultFromNativeQuery(String sql) {
return jpaTm()
.transact(() -> jpaTm().getEntityManager().createNativeQuery(sql).getSingleResult());
}
enum TestEnum {
FOO,
BAR,
BAZ;
public static class TestEnumType extends EnumSetUserType<TestEnum> {
@Override
protected TestEnum convertToElem(String value) {
return TestEnum.valueOf(value);
}
}
public static class TestEnumType extends EnumSetUserType<TestEnum> {}
}
@Entity(name = "TestEntity")