Remove support for @SecondaryKey annotation
Multiple primary keys for a single entity isn't something
that can be scaled on top of a database like BigTable or
Cassandra. Remove support for it, forcing the application
to implement its own secondary key approach that fits its
database model.
Change-Id: I3dcf08336ff3563321367516cd72c04c3bb5f37a
Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/src/main/java/com/google/gwtorm/client/SecondaryKey.java b/src/main/java/com/google/gwtorm/client/SecondaryKey.java
deleted file mode 100644
index 773bc1a..0000000
--- a/src/main/java/com/google/gwtorm/client/SecondaryKey.java
+++ /dev/null
@@ -1,36 +0,0 @@
-// Copyright 2008 Google Inc.
-//
-// 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 com.google.gwtorm.client;
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-/**
- * Annotation marking a query function in a {@link Access} interface as a key.
- * <p>
- * SecondaryKey is gwtorm's concept of SQL UNIQUE.
- *
- * @see Access
- */
-@Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.METHOD)
-public @interface SecondaryKey {
- /**
- * @return name of the field in the entity which contains a secondary key.
- */
- String value();
-}
diff --git a/src/main/java/com/google/gwtorm/jdbc/gen/AccessGen.java b/src/main/java/com/google/gwtorm/jdbc/gen/AccessGen.java
index b719847..ee5ccde 100644
--- a/src/main/java/com/google/gwtorm/jdbc/gen/AccessGen.java
+++ b/src/main/java/com/google/gwtorm/jdbc/gen/AccessGen.java
@@ -115,9 +115,6 @@
overrideGetMany();
}
}
- for (final KeyModel key : model.getSecondaryKeys()) {
- implementKeyQuery(key);
- }
for (final QueryModel q : model.getQueries()) {
implementQuery(q);
diff --git a/src/main/java/com/google/gwtorm/schema/RelationModel.java b/src/main/java/com/google/gwtorm/schema/RelationModel.java
index d656ae4..85c440f 100644
--- a/src/main/java/com/google/gwtorm/schema/RelationModel.java
+++ b/src/main/java/com/google/gwtorm/schema/RelationModel.java
@@ -17,7 +17,6 @@
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.PrimaryKey;
import com.google.gwtorm.client.Relation;
-import com.google.gwtorm.client.SecondaryKey;
import com.google.gwtorm.schema.sql.SqlDialect;
import java.util.ArrayList;
@@ -37,14 +36,12 @@
protected final LinkedHashMap<String, ColumnModel> columnsByColumnName;
protected final SortedMap<Integer, ColumnModel> columnsById;
protected KeyModel primaryKey;
- protected Collection<KeyModel> secondaryKeys;
protected Collection<QueryModel> queries;
protected RelationModel() {
fieldsByFieldName = new LinkedHashMap<String, ColumnModel>();
columnsByColumnName = new LinkedHashMap<String, ColumnModel>();
columnsById = new TreeMap<Integer, ColumnModel>();
- secondaryKeys = new ArrayList<KeyModel>();
queries = new ArrayList<QueryModel>();
}
@@ -107,17 +104,6 @@
}
}
- protected void addSecondaryKey(final String name,
- final SecondaryKey annotation) throws OrmException {
- final ColumnModel field = getField(annotation.value());
- if (field == null) {
- throw new OrmException("Field " + annotation.value() + " not in "
- + getEntityTypeClassName());
- }
-
- secondaryKeys.add(new KeyModel(name, field));
- }
-
protected void addQuery(final QueryModel q) {
queries.add(q);
}
@@ -182,10 +168,6 @@
return Collections.<ColumnModel> emptyList();
}
- public Collection<KeyModel> getSecondaryKeys() {
- return secondaryKeys;
- }
-
public Collection<QueryModel> getQueries() {
return queries;
}
@@ -243,19 +225,6 @@
r.append(")\n");
}
- for (final KeyModel key : secondaryKeys) {
- r.append(",UNIQUE(");
- for (final Iterator<ColumnModel> i = key.getAllLeafColumns().iterator(); i
- .hasNext();) {
- final ColumnModel col = i.next();
- r.append(col.getColumnName());
- if (i.hasNext()) {
- r.append(",");
- }
- }
- r.append(")\n");
- }
-
r.append(")");
dialect.appendCreateTableStorage(r, this);
return r.toString();
diff --git a/src/main/java/com/google/gwtorm/schema/java/JavaRelationModel.java b/src/main/java/com/google/gwtorm/schema/java/JavaRelationModel.java
index 8442d63..f447095 100644
--- a/src/main/java/com/google/gwtorm/schema/java/JavaRelationModel.java
+++ b/src/main/java/com/google/gwtorm/schema/java/JavaRelationModel.java
@@ -21,7 +21,6 @@
import com.google.gwtorm.client.Query;
import com.google.gwtorm.client.Relation;
import com.google.gwtorm.client.ResultSet;
-import com.google.gwtorm.client.SecondaryKey;
import com.google.gwtorm.schema.QueryModel;
import com.google.gwtorm.schema.RelationModel;
@@ -85,13 +84,6 @@
}
initPrimaryKey(m.getName(), m.getAnnotation(PrimaryKey.class));
- } else if (m.getAnnotation(SecondaryKey.class) != null) {
- if (m.getReturnType() != entityType) {
- throw new OrmException("SecondaryKey " + m.getName() + " must return "
- + entityType.getName());
- }
- addSecondaryKey(m.getName(), m.getAnnotation(SecondaryKey.class));
-
} else if (m.getAnnotation(Query.class) != null) {
if (!ResultSet.class.isAssignableFrom(m.getReturnType())
|| !(m.getGenericReturnType() instanceof ParameterizedType)
diff --git a/src/test/java/com/google/gwtorm/data/PersonAccess.java b/src/test/java/com/google/gwtorm/data/PersonAccess.java
index 14384e1..7df107e 100644
--- a/src/test/java/com/google/gwtorm/data/PersonAccess.java
+++ b/src/test/java/com/google/gwtorm/data/PersonAccess.java
@@ -19,15 +19,11 @@
import com.google.gwtorm.client.PrimaryKey;
import com.google.gwtorm.client.Query;
import com.google.gwtorm.client.ResultSet;
-import com.google.gwtorm.client.SecondaryKey;
public interface PersonAccess extends Access<TestPerson, TestPerson.Key> {
@PrimaryKey("name")
TestPerson get(TestPerson.Key key) throws OrmException;
- @SecondaryKey("age")
- TestPerson byAge(int age) throws OrmException;
-
@Query
ResultSet<TestPerson> all() throws OrmException;
diff --git a/src/test/java/com/google/gwtorm/server/PhoneBookDbTestCase.java b/src/test/java/com/google/gwtorm/server/PhoneBookDbTestCase.java
index 5e1ecac..2ffdebd 100644
--- a/src/test/java/com/google/gwtorm/server/PhoneBookDbTestCase.java
+++ b/src/test/java/com/google/gwtorm/server/PhoneBookDbTestCase.java
@@ -159,18 +159,6 @@
assertEquals(sp.primaryKey(p1), sp.primaryKey(p2));
}
- public void testGetByAge() throws Exception {
- final PhoneBookDb schema = openAndCreate();
- final PersonAccess sp = schema.people();
- final TestPerson p1 = new TestPerson(new TestPerson.Key("Bob"), 18);
- sp.insert(Collections.singleton(p1));
-
- final TestPerson p2 = sp.byAge(p1.age());
- assertNotNull(p2);
- assertNotSame(p1, p2);
- assertEquals(sp.primaryKey(p1), sp.primaryKey(p2));
- }
-
public void testGetOnePersonIterator() throws Exception {
final PhoneBookDb schema = openAndCreate();
final PersonAccess sp = schema.people();