Creating a User Defined Object - Java

Click here to view in C++


You define your object like any other class in Java. The only requirement is that all persistent objects (objects you would like to store in the database) will have to inherit from ooObj. Here's an example:

Car.java


import com.objy.db.app.*;
class Car extends ooObj
{
    public int VIN;
    public void setVin(int vinNum)
    {
        markModified(); //to mark object for update
        VIN = vinNum;
    }
    public int returnVin()
    {
        fetch(); //mark object for read
        return VIN;
    }
};



Be sure to include the path in your application to include these class files.

createObj.java


import com.objy.db.*;
import com.objy.db.app.*;

public class createObj
{
	public static void main(String[] args)
	{
		String bootfile = "test.boot";		//insert bootfile name here
		String dbName = "testDB";		//insert db name here
		String contName = "testCont";		//insert cont name here

		Connection connection = null;
		Session session = null;
		try
		{
			connection = Connection.open(bootfile, oo.openReadWrite);
			connection.loadSchemaClasses(true);
			session = new Session();
			ooFDObj fd = session.getFD();
			session.begin();
			ooDBObj db = null;
			if(fd.hasDB(dbName))
			{
				db = fd.lookupDB(dbName);
			}
			else
			{
				db = fd.newDB(dbName);
			}
			ooContObj cont = null;
			if(db.hasContainer(contName))
			{
				cont = db.lookupContainer(contName);
			}
			else
			{
				cont = new ooContObj();
				db.addContainer(cont, 0, contName, 0, 0);
			}

			Car obj = new Car();
			cont.cluster(obj);
			session.commit();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			session.abort();
		}
	}
}