Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: for the plan.

Jump to: navigation, search

XDI4j Tutorial 8


Higgins logo 76Wx100H.jpg

This tutorial explains how to work with persistent graphs.

Persistent graphs

So far we have only been working with the MemoryGraphFactory, which creates XDI graphs stored in memory.

Working with persistent XDI graphs is as simple as using a different graph factory, for example:

  • XMLGraphFactory - Creates a persistent XDI graph backed by a file in XDI/XML serialization format.
  • BDBGraphFactory - Creates a persistent XDI graph backed by a BDB database.
  • HibernateGraphFactory - Creates a persistent XDI graph backed by Hibernate

Just like the MemoryGraphFactory, all of the above can be created and used either programmatically, or in the XDI4j server via the GraphMessagingTarget.

Example Code:

package org.eclipse.higgins.xdi4j.tutorial;

import org.eclipse.higgins.xdi4j.Graph;
import org.eclipse.higgins.xdi4j.addressing.Addressing;
import org.eclipse.higgins.xdi4j.impl.xml.XMLGraphFactory;
import org.eclipse.higgins.xdi4j.xri3.impl.XRI3;
import org.eclipse.higgins.xdi4j.xri3.impl.XRI3Segment;

 * This example app shows how to work with an XDI graph backed
 * by an XDI/XML file.
public class Tutorial8 {

	public static void main(String[] args) throws Exception {

		XMLGraphFactory factory = new XMLGraphFactory();
		Graph graph = factory.openGraph();

		// see if the graph is filled already
		String data = Addressing.findLiteralData(graph, new XRI3("=alice/+name"));
		if (data == null) {
			graph.createStatement(new XRI3Segment("=alice"), new XRI3Segment("+name"), "Alice X.");
			System.out.println("Data created in graph.");
		} else {
			System.out.println("Data found in graph: " + data);

		// don't forget to close the graph


To run the XDI4j server with an XDI endpoint backed by a BDB database, the following WEB-INF/application.xml file could be used:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="" xmlns:util="" xmlns:xsi="" xsi:schemaLocation="">

	<bean id="graphfactory" class="org.eclipse.higgins.xdi4j.impl.keyvalue.bdb.BDBGraphFactory">
		<property name="databasePath" value="./my-graph/" />

	<bean id="mygraph" factory-bean="graphfactory" factory-method="openGraph" />

	<bean name="/myendpoint" class="org.eclipse.higgins.xdi4j.messaging.server.impl.graph.GraphMessagingTarget">
		<property name="graph" ref="mygraph" />

Back to the top