You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlcontainer-referencing.asciidoc 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ---
  2. title: Referencing Another SQLContainer
  3. order: 6
  4. layout: page
  5. ---
  6. [[sqlcontainer.referencing]]
  7. = Referencing Another SQLContainer
  8. When developing a database-connected application, there is usually a need to
  9. retrieve data related to one table from one or more other tables. In most cases,
  10. this relation is achieved with a foreign key reference, where a column of the
  11. first table contains a primary key or candidate key of a row in another table.
  12. SQLContainer offers limited support for this kind of referencing relation,
  13. although all referencing is currently done on the Java side so no constraints
  14. need to be made in the database. A new reference can be created by calling the
  15. following method:
  16. ----
  17. public void addReference(SQLContainer refdCont,
  18. String refingCol, String refdCol);
  19. ----
  20. This method should be called on the source container of the reference. The
  21. target container should be given as the first parameter. The
  22. [parameter]#refingCol# is the name of the 'foreign key' column in the source
  23. container, and the [parameter]#refdCol# is the name of the referenced key column
  24. in the target container.
  25. __Note: For any [classname]#SQLContainer#, all the referenced target containers
  26. must be different. You can not reference the same container from the same source
  27. twice.__
  28. Handling the referenced item can be done through the three provided set/get
  29. methods, and the reference can be completely removed with the
  30. [methodname]#removeReference()# method. Signatures of these methods are listed
  31. below:
  32. ----
  33. public boolean setReferencedItem(Object itemId,
  34. Object refdItemId, SQLContainer refdCont)
  35. public Object getReferencedItemId(Object itemId,
  36. SQLContainer refdCont)
  37. public Item getReferencedItem(Object itemId,
  38. SQLContainer refdCont)
  39. public boolean removeReference(SQLContainer refdCont)
  40. ----
  41. The setter method should be given three parameters: [parameter]#itemId# is the
  42. ID of the referencing item (from the source container), [parameter]#refdItemId#
  43. is the referenced [parameter]#itemID# (from the target container) and
  44. [parameter]#refdCont# is a reference to the target container that identifies the
  45. reference. This method returns true if the setting of the referenced item was
  46. successful. After setting the referenced item you must normally call
  47. [methodname]#commit()# on the source container to persist the changes to the
  48. database.
  49. The [methodname]#getReferencedItemId()# method will return the item ID of the
  50. referenced item. As parameters this method needs the item ID of the referencing
  51. item and a reference to the target container as an identifier.
  52. [classname]#SQLContainer# also provides a convenience method
  53. [methodname]#getReferencedItem()#, which directly returns the referenced item
  54. from the target container.
  55. Finally, the referencing can be removed from the source container by calling the
  56. [methodname]#removeReference()# method with the target container as parameter.
  57. Note that this does not actually change anything in the database; it merely
  58. removes the logical relation that exists only on the Java-side.