I use the identification scheme described in PWO
AbstractUser entity (abstract class) -> SIteUser entity extends AbstractUser (implements the password control) // I use SiteUser as MySQL doesn't like User !) -> Administrator entity extends SiteUser (type=9) I also have a Language entity in my DB (one language , english, already written) I defined a many-to-many relationship 'languages' (JOIN table) between SiteUser and Language entities
When trying to insert my first Administrator, I got an error...
An exception occurred while trying to save administrator language com.webobjects.eoaccess.EOObjectNotAvailableException: objectMatchingValueForKeyEntityNamed: No AbstractUser found with key login matching anon ("Entity Administrator has a reference to class EOGenericRecord which can not be loaded.")
remarks : "No AbstractUser found with key login matching anon" : strange ! Administrator is a subclass of AbstractUser and the login data is set... "reference to class EOGenericRecord which can not be loaded ": strange ! cannot be loaded as it's not yet written into the DB
------- here is my code, in AdminPage ( extends ValidatingPage)
public void createAdministrator(){ EOEditingContext ec = editingContext(); ec.lock(); try { Administrator.authenticatedUser(ec, "anon", "emouse"); } catch (EOObjectNotAvailableException e) { Administrator anAdministrator = new Administrator(); ec.insertObject(anAdministrator); anAdministrator.setLogin("anon"); anAdministrator.setPassword("emouse");
Language englishLanguage = null; try { englishLanguage = (Language) EOUtilities.objectMatchingKeyAndValue(ec, "Language", "languageCode", "en"); } catch(Exception ex) {} if (englishLanguage != null) { anAdministrator.setMotherTongue(englishLanguage); anAdministrator.addObjectToBothSidesOfRelationshipWithKey(englishLanguage, "languages"); this.attemptSave(); if (errors().count()>0) { System.out.println("An exception occurred while trying to save administrator data " ); System.out.println(errors().toString()); } }
} finally { ec.unlock(); } }
|