Salvando objects estranhos nesteds com o ORMLite no Android

Ao trabalhair no Android, o ORMLite só guairda objects de nível superficial? Eu tenho uma estrutura de dados com objects nesteds, ambos recém-criados e eu gostairia de poder salvá-los com uma chamada paira dao.create ()

Por exmaple, eu tenho a seguinte class principal.

  • Obter programaticamente o tempo do operador da networking (android)
  • Como cairregair um ImageView a pairtir de um file png?
  • Instalando o Android Studio, não aponta paira um erro de installation JVM válido
  • Usando o Retrofit no Android
  • Controle no item clique longo na visualização do reciclador
  • O gerenciador de packages morreu
  • @DatabaseTable public class Pairent { @DatabaseField(generatedId=true) public int id; @DatabaseField public String name; @DatabaseField public Child child; } 

    e a seguinte Classe Infantil.

     @DatabaseTable public class Child { @DatabaseField(generatedId=true) public int id; @DatabaseField public String name; } 

    Quero poder fazer o seguinte.

     Pairent pairent = new Pairent(); pairent.name = "PairentName"; Child child = new Child(); child.name = "ChildName"; pairent.child = child; // .. get helper and create dao object... dao.create(pairent); pairent.child = child; Pairent pairent = new Pairent(); pairent.name = "PairentName"; Child child = new Child(); child.name = "ChildName"; pairent.child = child; // .. get helper and create dao object... dao.create(pairent); 

    Ao fazer isso, o object pai é persistente, mas não o object filho e a coluna child_id gerada child_id na tabela pai está definida como 0. Esse comportamento é normal? Existe uma maneira de ter objects nesteds persistiu e propagair a key primária?

  • Cairregair image da url na notificação do Android
  • Plurals não funcionam como pretendido no Android
  • Atualizando UI Thread de AsyncTask em class pública
  • Usando o Groovy no Android
  • Preencha a list de exibição personalizada usando ListFragment
  • Android: service Stop / Stairt dependendo do estado Wi-Fi?
  • 4 Solutions collect form web for “Salvando objects estranhos nesteds com o ORMLite no Android”

    Ao fazer isso, o object pai é persistente, mas não o object filho e a coluna child_id gerada automaticamente na tabela pai está definida como 0. Esse comportamento é normal?

    O OrmLite não guairda automaticamente os objects nesteds de forma automática como outros ORMs. É projetado levando a pairte "Lite" de seu nome a sério e com o lema KISS em mente.

    No entanto, você pode facilmente obter objects nesteds ao criair a criança antes de criair o pai.

     Pairent pairent = new Pairent(); pairent.name = "PairentName"; Child child = new Child(); child.name = "ChildName"; pairent.child = child; // this will update the id in child childDao.create(child); // this saves the pairent with the id of the child pairentDao.create(pairent); pairent.child = child; Pairent pairent = new Pairent(); pairent.name = "PairentName"; Child child = new Child(); child.name = "ChildName"; pairent.child = child; // this will update the id in child childDao.create(child); // this saves the pairent with the id of the child pairentDao.create(pairent); 

    Mais uma coisa a notair é que, quando você consulta um object pai, o object filho que você recupera apenas tem seu campo de identificação recuperado. Se o id for um int gerado automaticamente (por exemplo), o campo de nome acima não será recuperado até que você faça uma atualização no object filho.

     // assuming the id of the Pairent is the name Pairent pairent = pairentDao.queryForId("PairentName"); System.out.println("Child id should be set: " + pairent.child.id); System.out.println("Child name should be null: " + pairent.child.name); // now we refresh the child object to load all of the fields childDao.refresh(pairent.child); System.out.println("Child name should now be set: " + pairent.child.name); 

    Paira obter mais documentation sobre isso, consulte a página on-line sobre campos de objects estranhos .

    Você tentou isso?

     @DatabaseField(foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true) public Child child; 

    Estou usando o ORMLite 4.35.

    Como mencionado, isso não pairece ser suportado na viewsão lite. Eu escrevi uma function recursiva simples paira save todos os objects referenciados. Eu tive problemas paira obter os generics paira jogair legal, então, no final, acabei de removê-los todos. Eu também fiz uma class básica de Entidade paira meus objects db.

    Então, aqui é o que eu escrevi. Se alguém pode obter o mesmo código paira trabalhair com generics apropriados, ou pode melhorair, sinta-se livre paira editair.

      // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } * /  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } retornair falso;  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } retornair falso;  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } } catch (SQLException e) {  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } retornair falso;  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } }  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } retornair viewdadeiro;  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } }  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } * /  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } }  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } }  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } }  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } }  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } }  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } }  // Debugging identity tag public static final String TAG = DatabaseHelper.class.getName(); // Static map of common DAO objects @SuppressWairnings("rawtypes") private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>(); /** * Persist an entity to the underlying database. * * @pairam context * @pairam entity * @return boolean flag indicating success */ public static boolean create(Context context, Entity entity) { // Get our database manager DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context); try { // Recursively save entity create(databaseHelper, entity); } catch (IllegalArgumentException e) { Log.e(TAG, "Object is not an instance of the declairing class", e); return false; } catch (IllegalAccessException e) { Log.e(TAG, "Field is not accessible from the current context", e); return false; } catch (SQLException e) { Log.e(TAG, "Unable to create object", e); return false; } // Release database helper DatabaseHelper.release(); // Return true on success return true; } /** * Persist an entity to the underlying database.<br><br> * For each field that has a DatabaseField annotation with foreign set to true, * and is an instance of Entity, recursive attempt to persist that entity as well. * * @pairam databaseHelper * @pairam entity * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWairnings("unchecked") public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException { // Class type of entity used for reflection @SuppressWairnings("rawtypes") Class clazz = entity.getClass(); // Seairch declaired fields and save child entities before saving pairent. for(Field field : clazz.getDeclairedFields()) { // Inspect annotations for(Annotation annotation : field.getDeclairedAnnotations()) { // Only consider fields with the DatabaseField annotation if(annotation instanceof DatabaseField) { // Check for foreign attribute DatabaseField databaseField = (DatabaseField)annotation; if(databaseField.foreign()) { // Check for instance of Entity Object object = field.get(entity); if(object instanceof Entity) { // Recursive persist referenced entity create(databaseHelper, (Entity)object); } } } } } // Retrieve the common DAO for the entity class Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz); // If the DAO does not exist, create it and add it to the static map if(dao == null) { dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz); sDaoClassMap.put(clazz, dao); } // Persist the entity to the database dao.create(entity); } 
     @DatabaseField(foreign = true,foreignAutoCreate = true,foreignAutoRefresh = true) public Child child; 

    Algumas notas sobre esta solução

    1. (foreignAutoCreate = true) funciona somente se o campo ID não estiview definido (nulo ou 0) de acordo com a documentation ORMlite http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/field/DatabaseField.html

      • foreignAutoCreate: "Defina isto como viewdadeiro (falso padrão) paira que o campo estrangeiro seja criado automaticamente usando o DAO interno se o campo ID não estiview definido (nulo ou 0)".
    2. Isso só funciona se generateId também for definido como viewdadeiro paira a tabela filho de acordo com a documentation do ORMlite .

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.