You define your object like any other class in C++. The only requirement is that all persistent objects (objects you would like to store in the database) will have to inherit from ooObj. You define your class in a DDL file and process it using Objectivity's ooddlx.exe tool. Here's an example: objects.ddl
ooddlx output
Be sure to include objects.h in your application and all of the other files in your project. createObj.cpp
#include <iostream>
using namespace std;
#include <ooObjy.h>
#include "objects.h"
int main(int argc, char* argv[])
{
int returnval = 0;
//ooObjy::setLoggingOptions(oocLogAll, oocTrue, oocFalse); //enable session logging for debug
ooObjy::startup();
char *bootfile = "test.boot"; //insert bootfile name here
char *dbName = "testDB"; //insert database name here
char *contName = "testCont"; //insert container name here
ooConnection *connection = ooObjy::getConnection(bootfile);
ooSession *session = connection->createSession("main_session"); //insert session name for log or '0' for default
try
{
session->begin(oocUpdate);
ooHandle(ooFDObj) fd;
fd.exist(bootfile, oocUpdate); //open in update mode
ooHandle(ooDBObj) db; //open/create db
if (db.exist(fd, dbName));
else
{
db = new ooDBObj(dbName);
}
ooHandle(ooContObj) cont; //open/create cont
if (cont.exist(db, contName, oocUpdate));
else
{
cont = new(db, 4, 0, contName) ooContObj;
}
ooHandle(Car) carObj;
carObj = new(cont) Car();
carObj.nameObj(cont, "Car"); //name object "Car" in scope of cont
session->commit();
}
catch(ooException &error)
{
cerr << error.what() << endl;
session->abort();
returnval = 1;
}
catch(...)
{
cerr << "unknown error" << endl;
session->abort();
returnval = 1;
}
ooObjy::shutdown();
return returnval;
}
|
|||
