mirror of
https://github.com/google/nomulus.git
synced 2025-06-01 02:04:04 +02:00
Automatically apply JPA type converters (#305)
* Automatically apply JPA type converters * Include converters in tests and schema generation too
This commit is contained in:
parent
0fd7cf29b5
commit
5dc058ec99
11 changed files with 25 additions and 20 deletions
|
@ -27,7 +27,7 @@ import javax.persistence.Converter;
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
|
|
||||||
/** JPA converter to for storing/retrieving CreateAutoTimestamp objects. */
|
/** JPA converter to for storing/retrieving CreateAutoTimestamp objects. */
|
||||||
@Converter
|
@Converter(autoApply = true)
|
||||||
public class CreateAutoTimestampConverter
|
public class CreateAutoTimestampConverter
|
||||||
implements AttributeConverter<CreateAutoTimestamp, Timestamp> {
|
implements AttributeConverter<CreateAutoTimestamp, Timestamp> {
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ import javax.persistence.AttributeConverter;
|
||||||
import javax.persistence.Converter;
|
import javax.persistence.Converter;
|
||||||
|
|
||||||
/** JPA converter for storing/retrieving UpdateAutoTimestamp objects. */
|
/** JPA converter for storing/retrieving UpdateAutoTimestamp objects. */
|
||||||
@Converter
|
@Converter(autoApply = true)
|
||||||
public class UpdateAutoTimestampConverter
|
public class UpdateAutoTimestampConverter
|
||||||
implements AttributeConverter<UpdateAutoTimestamp, Timestamp> {
|
implements AttributeConverter<UpdateAutoTimestamp, Timestamp> {
|
||||||
|
|
||||||
|
|
|
@ -21,12 +21,10 @@ import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
|
||||||
import google.registry.model.Buildable;
|
import google.registry.model.Buildable;
|
||||||
import google.registry.model.CreateAutoTimestamp;
|
import google.registry.model.CreateAutoTimestamp;
|
||||||
import google.registry.model.ImmutableObject;
|
import google.registry.model.ImmutableObject;
|
||||||
import google.registry.persistence.CreateAutoTimestampConverter;
|
|
||||||
import google.registry.util.DateTimeUtils;
|
import google.registry.util.DateTimeUtils;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Convert;
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.EnumType;
|
import javax.persistence.EnumType;
|
||||||
import javax.persistence.Enumerated;
|
import javax.persistence.Enumerated;
|
||||||
|
@ -109,7 +107,6 @@ public final class RegistryLock extends ImmutableObject implements Buildable {
|
||||||
|
|
||||||
/** Creation timestamp is when the lock/unlock is first requested. */
|
/** Creation timestamp is when the lock/unlock is first requested. */
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@Convert(converter = CreateAutoTimestampConverter.class)
|
|
||||||
private CreateAutoTimestamp creationTimestamp = CreateAutoTimestamp.create(null);
|
private CreateAutoTimestamp creationTimestamp = CreateAutoTimestamp.create(null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -17,12 +17,10 @@ package google.registry.schema.tld;
|
||||||
import static com.google.common.base.Preconditions.checkState;
|
import static com.google.common.base.Preconditions.checkState;
|
||||||
|
|
||||||
import google.registry.model.CreateAutoTimestamp;
|
import google.registry.model.CreateAutoTimestamp;
|
||||||
import google.registry.persistence.CreateAutoTimestampConverter;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.persistence.CollectionTable;
|
import javax.persistence.CollectionTable;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Convert;
|
|
||||||
import javax.persistence.ElementCollection;
|
import javax.persistence.ElementCollection;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
|
@ -56,7 +54,6 @@ public class PremiumList {
|
||||||
private Long revisionId;
|
private Long revisionId;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@Convert(converter = CreateAutoTimestampConverter.class)
|
|
||||||
private CreateAutoTimestamp creationTimestamp = CreateAutoTimestamp.create(null);
|
private CreateAutoTimestamp creationTimestamp = CreateAutoTimestamp.create(null);
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
|
|
|
@ -27,8 +27,10 @@ import google.registry.model.domain.secdns.DelegationSignerData;
|
||||||
import google.registry.model.eppcommon.Trid;
|
import google.registry.model.eppcommon.Trid;
|
||||||
import google.registry.model.transfer.BaseTransferObject;
|
import google.registry.model.transfer.BaseTransferObject;
|
||||||
import google.registry.model.transfer.TransferData;
|
import google.registry.model.transfer.TransferData;
|
||||||
|
import google.registry.persistence.CreateAutoTimestampConverter;
|
||||||
import google.registry.persistence.NomulusNamingStrategy;
|
import google.registry.persistence.NomulusNamingStrategy;
|
||||||
import google.registry.persistence.NomulusPostgreSQLDialect;
|
import google.registry.persistence.NomulusPostgreSQLDialect;
|
||||||
|
import google.registry.persistence.UpdateAutoTimestampConverter;
|
||||||
import google.registry.schema.domain.RegistryLock;
|
import google.registry.schema.domain.RegistryLock;
|
||||||
import google.registry.schema.tld.PremiumList;
|
import google.registry.schema.tld.PremiumList;
|
||||||
import google.registry.schema.tmch.ClaimsList;
|
import google.registry.schema.tmch.ClaimsList;
|
||||||
|
@ -62,6 +64,7 @@ public class GenerateSqlSchemaCommand implements Command {
|
||||||
ImmutableSet.of(
|
ImmutableSet.of(
|
||||||
BaseTransferObject.class,
|
BaseTransferObject.class,
|
||||||
ClaimsList.class,
|
ClaimsList.class,
|
||||||
|
CreateAutoTimestampConverter.class,
|
||||||
DelegationSignerData.class,
|
DelegationSignerData.class,
|
||||||
DesignatedContact.class,
|
DesignatedContact.class,
|
||||||
DomainBase.class,
|
DomainBase.class,
|
||||||
|
@ -70,7 +73,8 @@ public class GenerateSqlSchemaCommand implements Command {
|
||||||
PremiumList.class,
|
PremiumList.class,
|
||||||
RegistryLock.class,
|
RegistryLock.class,
|
||||||
TransferData.class,
|
TransferData.class,
|
||||||
Trid.class);
|
Trid.class,
|
||||||
|
UpdateAutoTimestampConverter.class);
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
public static final String DB_OPTIONS_CLASH =
|
public static final String DB_OPTIONS_CLASH =
|
||||||
|
|
|
@ -32,6 +32,10 @@
|
||||||
<class>google.registry.model.transfer.TransferData</class>
|
<class>google.registry.model.transfer.TransferData</class>
|
||||||
<class>google.registry.model.eppcommon.Trid</class>
|
<class>google.registry.model.eppcommon.Trid</class>
|
||||||
|
|
||||||
|
<!-- Customized type converters -->
|
||||||
|
<class>google.registry.persistence.CreateAutoTimestampConverter</class>
|
||||||
|
<class>google.registry.persistence.UpdateAutoTimestampConverter</class>
|
||||||
|
|
||||||
<!-- TODO(weiminyu): check out application-layer validation. -->
|
<!-- TODO(weiminyu): check out application-layer validation. -->
|
||||||
<validation-mode>NONE</validation-mode>
|
<validation-mode>NONE</validation-mode>
|
||||||
</persistence-unit>
|
</persistence-unit>
|
||||||
|
|
|
@ -19,6 +19,7 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
|
||||||
import static google.registry.testing.JUnitBackports.assertThrows;
|
import static google.registry.testing.JUnitBackports.assertThrows;
|
||||||
|
|
||||||
import google.registry.model.transaction.JpaTransactionManagerRule;
|
import google.registry.model.transaction.JpaTransactionManagerRule;
|
||||||
|
import google.registry.persistence.CreateAutoTimestampConverter;
|
||||||
import google.registry.schema.domain.RegistryLock;
|
import google.registry.schema.domain.RegistryLock;
|
||||||
import google.registry.schema.domain.RegistryLock.Action;
|
import google.registry.schema.domain.RegistryLock.Action;
|
||||||
import google.registry.testing.AppEngineRule;
|
import google.registry.testing.AppEngineRule;
|
||||||
|
@ -37,7 +38,9 @@ public final class RegistryLockDaoTest {
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public final JpaTransactionManagerRule jpaTmRule =
|
public final JpaTransactionManagerRule jpaTmRule =
|
||||||
new JpaTransactionManagerRule.Builder().withEntityClass(RegistryLock.class).build();
|
new JpaTransactionManagerRule.Builder()
|
||||||
|
.withEntityClass(RegistryLock.class, CreateAutoTimestampConverter.class)
|
||||||
|
.build();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSaveAndLoad_success() {
|
public void testSaveAndLoad_success() {
|
||||||
|
|
|
@ -18,6 +18,7 @@ import static org.joda.time.DateTimeZone.UTC;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import google.registry.persistence.PersistenceModule;
|
import google.registry.persistence.PersistenceModule;
|
||||||
import google.registry.testing.FakeClock;
|
import google.registry.testing.FakeClock;
|
||||||
|
@ -145,9 +146,9 @@ public class JpaTransactionManagerRule extends ExternalResource {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Adds an annotated class to the known entities for the database. */
|
/** Adds annotated class(es) to the known entities for the database. */
|
||||||
public Builder withEntityClass(Class clazz) {
|
public Builder withEntityClass(Class... classes) {
|
||||||
this.extraEntityClasses.add(clazz);
|
this.extraEntityClasses.addAll(ImmutableSet.copyOf(classes));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
|
||||||
import google.registry.model.CreateAutoTimestamp;
|
import google.registry.model.CreateAutoTimestamp;
|
||||||
import google.registry.model.ImmutableObject;
|
import google.registry.model.ImmutableObject;
|
||||||
import google.registry.model.transaction.JpaTransactionManagerRule;
|
import google.registry.model.transaction.JpaTransactionManagerRule;
|
||||||
import javax.persistence.Convert;
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import org.hibernate.cfg.Environment;
|
import org.hibernate.cfg.Environment;
|
||||||
|
@ -44,7 +43,7 @@ public class CreateAutoTimestampConverterTest {
|
||||||
@Rule
|
@Rule
|
||||||
public final JpaTransactionManagerRule jpaTmRule =
|
public final JpaTransactionManagerRule jpaTmRule =
|
||||||
new JpaTransactionManagerRule.Builder()
|
new JpaTransactionManagerRule.Builder()
|
||||||
.withEntityClass(TestEntity.class)
|
.withEntityClass(TestEntity.class, CreateAutoTimestampConverter.class)
|
||||||
.withProperty(Environment.HBM2DDL_AUTO, "update")
|
.withProperty(Environment.HBM2DDL_AUTO, "update")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
@ -78,7 +77,6 @@ public class CreateAutoTimestampConverterTest {
|
||||||
|
|
||||||
@Id String name;
|
@Id String name;
|
||||||
|
|
||||||
@Convert(converter = CreateAutoTimestampConverter.class)
|
|
||||||
CreateAutoTimestamp cat;
|
CreateAutoTimestamp cat;
|
||||||
|
|
||||||
public TestEntity() {}
|
public TestEntity() {}
|
||||||
|
|
|
@ -19,7 +19,6 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
|
||||||
import google.registry.model.ImmutableObject;
|
import google.registry.model.ImmutableObject;
|
||||||
import google.registry.model.UpdateAutoTimestamp;
|
import google.registry.model.UpdateAutoTimestamp;
|
||||||
import google.registry.model.transaction.JpaTransactionManagerRule;
|
import google.registry.model.transaction.JpaTransactionManagerRule;
|
||||||
import javax.persistence.Convert;
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import org.hibernate.cfg.Environment;
|
import org.hibernate.cfg.Environment;
|
||||||
|
@ -43,7 +42,7 @@ public class UpdateAutoTimestampConverterTest {
|
||||||
@Rule
|
@Rule
|
||||||
public final JpaTransactionManagerRule jpaTmRule =
|
public final JpaTransactionManagerRule jpaTmRule =
|
||||||
new JpaTransactionManagerRule.Builder()
|
new JpaTransactionManagerRule.Builder()
|
||||||
.withEntityClass(TestEntity.class)
|
.withEntityClass(TestEntity.class, UpdateAutoTimestampConverter.class)
|
||||||
.withProperty(Environment.HBM2DDL_AUTO, "update")
|
.withProperty(Environment.HBM2DDL_AUTO, "update")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
@ -89,7 +88,6 @@ public class UpdateAutoTimestampConverterTest {
|
||||||
|
|
||||||
@Id String name;
|
@Id String name;
|
||||||
|
|
||||||
@Convert(converter = UpdateAutoTimestampConverter.class)
|
|
||||||
UpdateAutoTimestamp uat;
|
UpdateAutoTimestamp uat;
|
||||||
|
|
||||||
public TestEntity() {}
|
public TestEntity() {}
|
||||||
|
|
|
@ -20,6 +20,7 @@ import static google.registry.testing.JUnitBackports.assertThrows;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import google.registry.model.transaction.JpaTransactionManagerRule;
|
import google.registry.model.transaction.JpaTransactionManagerRule;
|
||||||
|
import google.registry.persistence.CreateAutoTimestampConverter;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import javax.persistence.PersistenceException;
|
import javax.persistence.PersistenceException;
|
||||||
import org.joda.money.CurrencyUnit;
|
import org.joda.money.CurrencyUnit;
|
||||||
|
@ -34,7 +35,9 @@ public class PremiumListDaoTest {
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public final JpaTransactionManagerRule jpaTmRule =
|
public final JpaTransactionManagerRule jpaTmRule =
|
||||||
new JpaTransactionManagerRule.Builder().withEntityClass(PremiumList.class).build();
|
new JpaTransactionManagerRule.Builder()
|
||||||
|
.withEntityClass(PremiumList.class, CreateAutoTimestampConverter.class)
|
||||||
|
.build();
|
||||||
|
|
||||||
private static final ImmutableMap<String, BigDecimal> TEST_PRICES =
|
private static final ImmutableMap<String, BigDecimal> TEST_PRICES =
|
||||||
ImmutableMap.of(
|
ImmutableMap.of(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue