/** * Method to check if an element is already persistent, or is managed by a different * ExecutionContext. If not persistent, this will persist it. * * @param ec execution context * @param element The element * @param fieldValues any initial field values to use if persisting the element * @return Whether the element was persisted during this call */ protected boolean validateElementForWriting( ExecutionContext ec, Object element, FieldValues fieldValues) { // TODO Pass in cascade flag and if element not present then throw exception // Check the element type for this collection if (!elementIsPersistentInterface && !validateElementType(ec.getClassLoaderResolver(), element)) { throw new ClassCastException( Localiser.msg( "056033", element.getClass().getName(), ownerMemberMetaData.getFullFieldName(), elementType)); } boolean persisted = false; if (elementsAreEmbedded || elementsAreSerialised) { // Element is embedded/serialised so has no id } else { ObjectProvider elementSM = ec.findObjectProvider(element); if (elementSM != null && elementSM.isEmbedded()) { // Element is already with ObjectProvider and is embedded in another field! throw new NucleusUserException( Localiser.msg("056028", ownerMemberMetaData.getFullFieldName(), element)); } persisted = SCOUtils.validateObjectForWriting(ec, element, fieldValues); } return persisted; }