(elephant:open-store '(:BDB "/tmp/db"))

(elephant:defpclass A ()
 ((ic :accessor ic
      :initarg :ic
      :associate B)
  (id :accessor id
    :initarg :id
    :type string
    :index t)))

(elephant:defpclass B ()
 ((ii :accessor ii
      :associate (A ic)
      :documentation "Intended as 1 to N relation: B can point to many
      As, but each A must point to exactly one B")
  (id :accessor id
      :initarg :id
      :type string
      :index t)))

(defun make-A (id)
  (make-instance 'A :id id))

(defun make-B (id)
  (let
      ((b (make-instance 'B :id id))
       (alist (map 'list #'make-A '("a1" "a2" "a3"))))
    (dolist (a alist)
      (setf (ic a) b))))

(dolist (b-id '("b1" "b2" "b3"))
  (make-B b-id))

(defvar as (elephant:get-instances-by-class 'A))
(defvar bs (elephant:get-instances-by-class 'B))

(format t "With open store: first a has id ~a and ic ~a~&" (id (first as)) (ic (first as)))
;;Prints: With open store: first a has id a1 and ic #<B oid:2>
(format t "With open store: first b has id ~a and ii ~a~&" (id (first bs)) (ii (first bs)))
;;Prints: #<B oid:2>With open store: first b has id b1 and ii (#<A oid:4>
;;                #<A oid:6>
;;                #<A oid:7>)

(elephant:close-store)

(elephant:open-store '(:BDB "/tmp/db"))
(setf as (elephant:get-instances-by-class 'A))
(setf bs (elephant:get-instances-by-class 'B))
(format t "With reopened store: first a has id ~a and ic ~a" (id (first as)) (ic (first as)))
;;Prints: With reopened store: first a has id a1 and ic #<B oid:2>
(format t "With reopened store: first b has id ~a and ii ~a" (id (first bs)) (ii (first bs)))
;;Prints: With reopened store: first b has id b1 and ii NIL