use of org.datanucleus.exceptions.NoPersistenceInformationException in project datanucleus-core by datanucleus.
the class ExecutionContextImpl method newObjectId.
/**
* This method returns an object id instance corresponding to the pcClass and key arguments.
* Operates in 2 modes :-
* <ul>
* <li>The class uses SingleFieldIdentity and the key is the value of the key field</li>
* <li>In all other cases the key is the String form of the object id instance</li>
* </ul>
* @param pcClass Class of the persistable object to create the identity for
* @param key Value of the key for SingleFieldIdentity (or the toString value)
* @return The new object-id instance
*/
public Object newObjectId(Class pcClass, Object key) {
if (pcClass == null) {
throw new NucleusUserException(Localiser.msg("010028"));
}
assertClassPersistable(pcClass);
AbstractClassMetaData cmd = getMetaDataManager().getMetaDataForClass(pcClass, clr);
if (cmd == null) {
throw new NoPersistenceInformationException(pcClass.getName());
}
// If the class is not yet managed, manage it
if (!getStoreManager().managesClass(cmd.getFullClassName())) {
getStoreManager().manageClasses(clr, cmd.getFullClassName());
}
IdentityKeyTranslator translator = getNucleusContext().getIdentityManager().getIdentityKeyTranslator();
if (translator != null) {
// Use the provided translator to convert it
key = translator.getKey(this, pcClass, key);
}
Object id = null;
if (cmd.usesSingleFieldIdentityClass()) {
// Single Field Identity
if (getBooleanProperty(PropertyNames.PROPERTY_FIND_OBJECT_TYPE_CONVERSION) && translator == null && !key.getClass().getName().equals(cmd.getObjectidClass())) {
// key provided is intended to be the type of the PK member, so provide convenience type conversion to the actual type required
AbstractMemberMetaData mmd = cmd.getMetaDataForMember(cmd.getPrimaryKeyMemberNames()[0]);
if (!mmd.getType().isAssignableFrom(key.getClass())) {
Object convKey = TypeConversionHelper.convertTo(key, mmd.getType());
if (convKey != null) {
key = convKey;
}
}
}
id = nucCtx.getIdentityManager().getSingleFieldId(clr.classForName(cmd.getObjectidClass()), pcClass, key);
} else if (key instanceof java.lang.String) {
// String-based PK (datastore identity or application identity)
if (cmd.getIdentityType() == IdentityType.APPLICATION) {
if (Modifier.isAbstract(pcClass.getModifiers()) && cmd.getObjectidClass() != null) {
try {
Constructor c = clr.classForName(cmd.getObjectidClass()).getDeclaredConstructor(new Class[] { java.lang.String.class });
id = c.newInstance(new Object[] { (String) key });
} catch (Exception e) {
String msg = Localiser.msg("010030", cmd.getObjectidClass(), cmd.getFullClassName());
NucleusLogger.PERSISTENCE.error(msg, e);
throw new NucleusUserException(msg);
}
} else {
clr.classForName(pcClass.getName(), true);
id = nucCtx.getIdentityManager().getApplicationId(pcClass, key);
}
} else {
id = nucCtx.getIdentityManager().getDatastoreId((String) key);
}
} else {
// Key is not a string, and is not SingleFieldIdentity
throw new NucleusUserException(Localiser.msg("010029", pcClass.getName(), key.getClass().getName()));
}
return id;
}
Aggregations