Author: mbo
Date: Sat Jun 18 10:41:18 2005
New Revision: 191305
URL: http://svn.apache.org/viewcvs?rev=191305&view=rev
Log:
Support for single field identity in the JDO model. The ObjectId class defaults
to a subclass of SingleFieldIdentity if the pc class does not declare a ObjectId
class, but a single primary key field.
Modified:
incubator/jdo/trunk/core20/src/java/org/apache/jdo/impl/model/jdo/JDOClassImplDynamic.java
incubator/jdo/trunk/core20/src/java/org/apache/jdo/impl/model/jdo/util/TypeSupport.java
Modified: incubator/jdo/trunk/core20/src/java/org/apache/jdo/impl/model/jdo/JDOClassImplDynamic.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/core20/src/java/org/apache/jdo/impl/model/jdo/JDOClassImplDynamic.java?rev=191305&r1=191304&r2=191305&view=diff
==============================================================================
--- incubator/jdo/trunk/core20/src/java/org/apache/jdo/impl/model/jdo/JDOClassImplDynamic.java
(original)
+++ incubator/jdo/trunk/core20/src/java/org/apache/jdo/impl/model/jdo/JDOClassImplDynamic.java
Sat Jun 18 10:41:18 2005
@@ -251,6 +251,19 @@
this.declaredObjectIdClassName = type.getName();
}
}
+ else {
+ // not declared, check for single field ObjectId class
+ JDOField[] declaredPKFields = getDeclaredPrimaryKeyFields();
+ if ((declaredPKFields != null) && (declaredPKFields.length == 1)) {
+ // there is one pk field declared by this class =>
+ // check the type
+ JavaType fieldType = declaredPKFields[0].getType();
+ if (fieldType != null) {
+ return TypeSupport.getSingleFieldObjectIdClassName(
+ fieldType.getName());
+ }
+ }
+ }
return declaredObjectIdClassName;
}
@@ -1206,6 +1219,31 @@
return null;
}
+ /**
+ * Returns the collection of identifying declared fields of this JDOClass
+ * in the form of an array. The method returns the JDOField instances
+ * declared by this JDOClass defined as primary key fields (see {@link
+ * JDOField#isPrimaryKey}).
+ * @return the identifying fields of this JDOClass
+ */
+ protected JDOField[] getDeclaredPrimaryKeyFields() {
+ JDOField[] fields = getDeclaredFields();
+ JDOField[] tmp = new JDOField[fields.length];
+ int length = 0;
+ for (int i = 0; i < fields.length; i++) {
+ JDOField field = fields[i];
+ if (field.isManaged() && field.isPrimaryKey()) {
+ tmp[length++] = field;
+ }
+ }
+ // now fill the returned array
+ // the array should have the correct length
+ JDOField[] result = new JDOField[length];
+ System.arraycopy(tmp, 0, result, 0, length);
+
+ return result;
+ }
+
/**
* Returns a new instance of the JDOClass implementation class.
*/
Modified: incubator/jdo/trunk/core20/src/java/org/apache/jdo/impl/model/jdo/util/TypeSupport.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/core20/src/java/org/apache/jdo/impl/model/jdo/util/TypeSupport.java?rev=191305&r1=191304&r2=191305&view=diff
==============================================================================
--- incubator/jdo/trunk/core20/src/java/org/apache/jdo/impl/model/jdo/util/TypeSupport.java
(original)
+++ incubator/jdo/trunk/core20/src/java/org/apache/jdo/impl/model/jdo/util/TypeSupport.java
Sat Jun 18 10:41:18 2005
@@ -21,35 +21,73 @@
package org.apache.jdo.impl.model.jdo.util;
+import java.util.Map;
import java.util.Set;
+import java.util.HashMap;
import java.util.HashSet;
import org.apache.jdo.model.java.JavaModel;
import org.apache.jdo.model.java.JavaType;
import org.apache.jdo.model.jdo.JDOModel;
-
/**
*
*/
public class TypeSupport
{
/** */
- private static Set primitiveTypeNames = new HashSet();
+ private final static Set primitiveTypeNames = new HashSet();
+
+ /** */
+ private final static Map singleFieldObjectIdClassNames = new HashMap();
static
{
// initialize set of names of primitive types
- primitiveTypeNames.add("byte");
- primitiveTypeNames.add("short");
- primitiveTypeNames.add("int");
- primitiveTypeNames.add("long");
- primitiveTypeNames.add("char");
- primitiveTypeNames.add("float");
- primitiveTypeNames.add("double");
- primitiveTypeNames.add("boolean");
+ primitiveTypeNames.add("byte"); // NOI18N
+ primitiveTypeNames.add("short"); // NOI18N
+ primitiveTypeNames.add("int"); // NOI18N
+ primitiveTypeNames.add("long"); // NOI18N
+ primitiveTypeNames.add("char"); // NOI18N
+ primitiveTypeNames.add("float"); // NOI18N
+ primitiveTypeNames.add("double"); // NOI18N
+ primitiveTypeNames.add("boolean"); // NOI18N
+
+ // initialize map of singleFieldObjectIdClassNames
+ singleFieldObjectIdClassNames.put(
+ "byte", //NOI18N
+ "com.sun.persistence.support.identity.ByteIdentity"); //NOI18N
+ singleFieldObjectIdClassNames.put(
+ "java.lang.Byte", //NOI18N
+ "com.sun.persistence.support.identity.ByteIdentity"); //NOI18N
+ singleFieldObjectIdClassNames.put(
+ "char", //NOI18N
+ "com.sun.persistence.support.identity.CharIdentity"); //NOI18N
+ singleFieldObjectIdClassNames.put(
+ "java.lang.Char", //NOI18N
+ "com.sun.persistence.support.identity.CharIdentity"); //NOI18N
+ singleFieldObjectIdClassNames.put(
+ "int", //NOI18N
+ "com.sun.persistence.support.identity.IntIdentity"); //NOI18N
+ singleFieldObjectIdClassNames.put(
+ "java.lang.Integer", //NOI18N
+ "com.sun.persistence.support.identity.IntIdentity"); //NOI18N
+ singleFieldObjectIdClassNames.put(
+ "long", //NOI18N
+ "com.sun.persistence.support.identity.LongIdentity"); //NOI18N
+ singleFieldObjectIdClassNames.put(
+ "java.lang.Long", //NOI18N
+ "com.sun.persistence.support.identity.LongIdentity"); //NOI18N
+ singleFieldObjectIdClassNames.put(
+ "short", //NOI18N
+ "com.sun.persistence.support.identity.ShortIdentity"); //NOI18N
+ singleFieldObjectIdClassNames.put(
+ "java.lang.Short", //NOI18N
+ "com.sun.persistence.support.identity.ShortIdentity"); //NOI18N
+ singleFieldObjectIdClassNames.put(
+ "java.lang.String", //NOI18N
+ "com.sun.persistence.support.identity.StringIdentity"); //NOI18N
}
-
/**
* Returns <code>true</code> if the persistence-modifier of a field
@@ -147,7 +185,17 @@
return false;
}
-
+ /**
+ * Returns the name of a single field ObjectId class, if the specified
+ * type name denotes a type that is suitable for single fiedl identity.
+ * @param typeName the type to be checked
+ */
+ public static String getSingleFieldObjectIdClassName(String typeName) {
+ if (typeName == null)
+ return null;
+ return (String) singleFieldObjectIdClassNames.get(typeName);
+ }
+
//========= Internal helper methods ==========
/**
|