OrientDB database creation class

Problem: OrientDB does not support BigInteger’s out of the box. The simple solution is to tune OrientDB serializer to output BigIntegers as Integers and this allows us to store them in database nicely.

Although it’s a pretty straight-forward and simple class I decided to create a post about it because it took me a while to fight with the BigInteger issue.


import java.io.File;
import java.math.BigInteger;

import com.orientechnologies.orient.core.serialization.serializer.object.OObjectSerializer;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
import com.orientechnologies.orient.object.serialization.OObjectSerializerContext;
import com.orientechnologies.orient.object.serialization.OObjectSerializerHelper;
import com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl;

public class CreateNewDatabase {

	public static void main(String[] args) {

		// Don't run without database location argument.
		if (args == null || args.length != 1) {
					.println("Expected single argument of folder path where to store the database.");

		String path = args[0];
		if (path.length() < 2) {
			System.out.println("Path is too short: " + path);

		if (!path.endsWith(File.separator)) {
			path = path + File.separator;

		OObjectDatabaseTx db = null;

		try {

			File folder = new File(path);
			if (folder.exists()) {
				throw new Exception("Database already exists on that path!");

			// Create the folder as required.
			if (!folder.mkdirs()) {
				throw new Exception("Failed to create required folders:"
						+ folder.getAbsolutePath());
			System.out.println("Creating database to: " + path);
			db = new OObjectDatabaseTx("plocal:" + path);
			// Avoid overwriting existing databases.
			if (db.exists()) {
				throw new Exception("Database already exists on that path!");

			// Create database

			// Important code to serialize BigIntegers to Integers.
			// It's vital that this code is run before you register your classes.
			OObjectSerializerContext serializerContext = new OObjectSerializerContext();
					.bind(new OObjectSerializer<BigInteger, Integer>() {

						public Integer serializeFieldValue(Class<?> itype,
								BigInteger iFieldValue) {
							return iFieldValue.intValue();

						public BigInteger unserializeFieldValue(Class<?> itype,
								Integer iFieldValue) {
							return new BigInteger(iFieldValue.toString());


			// Tell OrientDB to automatically generate schema during class
			// registration, this saves us some code.
			// Now register our classes...

			// Save the schema of our new database...


		} catch (Exception e) {
		} finally {
			if (db != null) {



As always, freel free to comment and ask questions in the comments.

About Reigo Reinmets

Enterprise Software consultant with 9 years of experience in enterprise software world who's working on various projects that are mostly based on Liferay. Most blog posts here are real-life findings and issues we've encountered while working on projects and Java tutorials on Liferay.

Leave a Reply

Your email address will not be published. Required fields are marked *