Advertisement
Guest User

Untitled

a guest
Jul 5th, 2012
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. Saving nested foreign objects with ORMLite on Android
  2. @DatabaseTable
  3. public class Parent {
  4.  
  5. @DatabaseField(generatedId=true)
  6. public int id;
  7.  
  8. @DatabaseField
  9. public String name;
  10.  
  11. @DatabaseField
  12. public Child child;
  13. }
  14.  
  15. @DatabaseTable
  16. public class Child {
  17.  
  18. @DatabaseField(generatedId=true)
  19. public int id;
  20.  
  21. @DatabaseField
  22. public String name;
  23. }
  24.  
  25. Parent parent = new Parent();
  26. parent.name = "ParentName";
  27.  
  28. Child child = new Child();
  29. child.name = "ChildName";
  30.  
  31. parent.child = child;
  32.  
  33. // .. get helper and create dao object...
  34. dao.create(parent);
  35.  
  36. Parent parent = new Parent();
  37. parent.name = "ParentName";
  38.  
  39. Child child = new Child();
  40. child.name = "ChildName";
  41.  
  42. parent.child = child;
  43.  
  44. // this will update the id in child
  45. childDao.create(child);
  46.  
  47. // this saves the parent with the id of the child
  48. parentDao.create(parent);
  49.  
  50. // assuming the id of the Parent is the name
  51. Parent parent = parentDao.queryForId("ParentName");
  52. System.out.println("Child id should be set: " + parent.child.id);
  53. System.out.println("Child name should be null: " + parent.child.name);
  54.  
  55. // now we refresh the child object to load all of the fields
  56. childDao.refresh(parent.child);
  57. System.out.println("Child name should now be set: " + parent.child.name);
  58.  
  59. // Debugging identity tag
  60. public static final String TAG = DatabaseHelper.class.getName();
  61.  
  62. // Static map of common DAO objects
  63. @SuppressWarnings("rawtypes")
  64. private static final Map<Class, Dao<?, Integer>> sDaoClassMap = new HashMap<Class, Dao<?,Integer>>();
  65.  
  66. /**
  67. * Persist an entity to the underlying database.
  68. *
  69. * @param context
  70. * @param entity
  71. * @return boolean flag indicating success
  72. */
  73. public static boolean create(Context context, Entity entity) {
  74. // Get our database manager
  75. DatabaseHelper databaseHelper = DatabaseHelper.getHelper(context);
  76.  
  77. try {
  78. // Recursively save entity
  79. create(databaseHelper, entity);
  80.  
  81. } catch (IllegalArgumentException e) {
  82. Log.e(TAG, "Object is not an instance of the declaring class", e);
  83. return false;
  84. } catch (IllegalAccessException e) {
  85. Log.e(TAG, "Field is not accessible from the current context", e);
  86. return false;
  87. } catch (SQLException e) {
  88. Log.e(TAG, "Unable to create object", e);
  89. return false;
  90. }
  91.  
  92. // Release database helper
  93. DatabaseHelper.release();
  94.  
  95. // Return true on success
  96. return true;
  97. }
  98.  
  99. /**
  100. * Persist an entity to the underlying database.<br><br>
  101. * For each field that has a DatabaseField annotation with foreign set to true,
  102. * and is an instance of Entity, recursive attempt to persist that entity as well.
  103. *
  104. * @param databaseHelper
  105. * @param entity
  106. * @throws IllegalArgumentException
  107. * @throws IllegalAccessException
  108. * @throws SQLException
  109. */
  110. @SuppressWarnings("unchecked")
  111. public static void create(DatabaseHelper databaseHelper, Entity entity) throws IllegalArgumentException, IllegalAccessException, SQLException {
  112. // Class type of entity used for reflection
  113. @SuppressWarnings("rawtypes")
  114. Class clazz = entity.getClass();
  115.  
  116. // Search declared fields and save child entities before saving parent.
  117. for(Field field : clazz.getDeclaredFields()) {
  118. // Inspect annotations
  119. for(Annotation annotation : field.getDeclaredAnnotations()) {
  120. // Only consider fields with the DatabaseField annotation
  121. if(annotation instanceof DatabaseField) {
  122. // Check for foreign attribute
  123. DatabaseField databaseField = (DatabaseField)annotation;
  124. if(databaseField.foreign()) {
  125. // Check for instance of Entity
  126. Object object = field.get(entity);
  127. if(object instanceof Entity) {
  128. // Recursive persist referenced entity
  129. create(databaseHelper, (Entity)object);
  130. }
  131. }
  132. }
  133. }
  134. }
  135.  
  136. // Retrieve the common DAO for the entity class
  137. Dao<Entity, Integer> dao = (Dao<Entity, Integer>) sDaoClassMap.get(clazz);
  138. // If the DAO does not exist, create it and add it to the static map
  139. if(dao == null) {
  140. dao = BaseDaoImpl.createDao(databaseHelper.getConnectionSource(), clazz);
  141. sDaoClassMap.put(clazz, dao);
  142. }
  143.  
  144. // Persist the entity to the database
  145. dao.create(entity);
  146. }
  147.  
  148. @DatabaseField(foreign = true,foreignAutoCreate = true,foreignAutoRefresh = true)
  149. public Child child;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement