Updating Your Objects - Java

Click here to view in C++

Namescopes

Name scopes are designed primarily for individual lookup; you can find a named object by looking up its name in the scope of the appropriate scope object.

cont.nameObj(carObj, "Car");
//name the carObj in the scope of the cont
carObj = cont.lookupObj("Car");
//to look up and assign to the carObj the object named "Car" in the cont scope



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;
    }
};


Part.java


import com.objy.db.app.*;
class Part extends ooObj
{
    public int partNum;
    public void setPartNum(int num)
    {
        markModified(); //to mark object for update
        partNum = num;
    }
    public int returnPartNum()
    {
        fetch(); //mark object for read
        return partNum;
    }
};


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
		Session session = null;
		Connection connection = 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);
			cont.nameObj(obj, "Car");
			session.commit();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			session.abort();
		}
	}
}


Updating an Object

To update an object, you call the methods from the object. Be sure to include the call to markModified() on any methods that will update the data in the object.

Car carObj; //declare object
carObj = ... //instantiate car
carObj.setVin(); //call method



Using the database created from the example above, you can lookup an object and update it.

update.cpp


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 = null;
			obj = cont.lookupObj("Car");
			obj.setVin(23);
			session.commit();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			session.abort();
		}
	}
}



Deleting an Object

To delete an object, you call the object's delete method.

obj.delete();



Using the database created from the example above, you can lookup an object and delete it.

update.cpp


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 = null;
			obj = cont.lookupObj("Car");
			obj.setVin(23);
			obj.delete();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			session.abort();
		}
	}
}