Click here to view in C++ Unidirectional Associations
This is how you would define a unidirectional association in the java file.
public static OneToOne belongsTo_Relationship()
{
return new OneToOne(
"belongsTo", // Relationship field
"Car", // Destination class
null, // Inverse relationship
Relationship.COPY_DELETE, // Copying behavior
Relationship.VERSION_DELETE, // Copying behavior
false, // Delete propagation
false, // Lock propagation
Relationship.INLINE_NONE); //Storage mode
}
public ToOneRelationship belongsTo;
public ToOneRelationship getBelongsTo()
{
fetch();
return belongsTo;
}
For a one-to-many relationship you do:
public static OneToMany belongsTo_Relationship()
{
return new OneToMany(
"belongsTo", // Relationship field
"Car", // Destination class
null, // Inverse relationship
Relationship.COPY_DELETE, // Copying behavior
Relationship.VERSION_DELETE, // Copying behavior
false, // Delete propagation
false, // Lock propagation
Relationship.INLINE_NONE); //Storage mode
}
public ToManyRelationship belongsTo;
public ToManyRelationship getBelongsTo()
{
fetch();
return belongsTo;
}
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;
}
public static OneToOne belongsTo_Relationship()
{
return new OneToOne(
"belongsTo", // Relationship field
"Car", // Destination class
null, // Inverse relationship
Relationship.COPY_DELETE, // Copying behavior
Relationship.VERSION_DELETE, // Copying behavior
false, // Delete propagation
false, // Lock propagation
Relationship.INLINE_NONE); //Storage mode
}
public ToOneRelationship belongsTo;
public ToOneRelationship getBelongsTo()
{
fetch();
return belongsTo;
}
};
Note: Unlike bidirectional associations, if you delete an object, you must remember to delete all associations pointing to it. Bidirectional Associations
This is how you would define a bidirectional association in the java file.
//one-to-one
public static OneToOne belongsTo_Relationship()
{
return new OneToOne(
"belongsTo", // Relationship field
"Car", // Destination class
"contains", // Inverse relationship
Relationship.COPY_DELETE, // Copying behavior
Relationship.VERSION_DELETE, // Copying behavior
false, // Delete propagation
false, // Lock propagation
Relationship.INLINE_NONE); //Storage mode
}
public ToOneRelationship belongsTo;
public ToOneRelationship getBelongsTo()
{
fetch();
return belongsTo;
}
//many-to-one
public static ManyToOne contains_Relationship()
{
return new ManyToOne(
"contains", // Relationship field
"Part", // Destination class
"belongsTo", // Inverse relationship
Relationship.COPY_DELETE, // Copying behavior
Relationship.VERSION_DELETE, // Copying behavior
false, // Delete propagation
false, // Lock propagation
Relationship.INLINE_NONE); //Storage mode
}
public ToOneRelationship contains;
public ToOneRelationship getContains()
{
fetch();
return contains;
}
//one-to-many
public static OneToMany belongsTo_Relationship()
{
return new OneToMany(
"belongsTo", // Relationship field
"Car", // Destination class
"contains", // Inverse relationship
Relationship.COPY_DELETE, // Copying behavior
Relationship.VERSION_DELETE, // Copying behavior
false, // Delete propagation
false, // Lock propagation
Relationship.INLINE_NONE); //Storage mode
}
public ToManyRelationship belongsTo;
public ToManyRelationship getBelongsTo()
{
fetch();
return belongsTo;
}
//Many-to-Many
public static ManyToMany belongsTo_Relationship()
{
return new ManyToMany(
"belongsTo", // Relationship field
"Car", // Destination class
"contains", // Inverse relationship
Relationship.COPY_DELETE, // Copying behavior
Relationship.VERSION_DELETE, // Copying behavior
false, // Delete propagation
false, // Lock propagation
Relationship.INLINE_NONE); //Storage mode
}
public ToManyRelationship belongsTo;
public ToManyRelationship getBelongsTo()
{
fetch();
return belongsTo;
}
In this example, we have a class Car and a class Part. We will have an association from a part to a car called belongsTo. Car.java
import com.objy.db.app.ooObj;
import com.objy.db.app.ManyToMany;
import com.objy.db.app.Relationship;
import com.objy.db.app.ToManyRelationship;
public class Car extends ooObj
{
public static ManyToMany contains_Relationship()
{
return new ManyToMany(
"contains", // Relationship field
"Part", // Destination class
"belongsTo", // Inverse relationship
Relationship.COPY_DELETE, // Copying behavior
Relationship.VERSION_DELETE, // Copying behavior
false, // Delete propagation
false, // Lock propagation
Relationship.INLINE_NONE); //Storage mode
}
private int VIN;
public ToManyRelationship contains;
public int getVIN()
{
fetch();
return VIN;
}
public void setVIN(int value)
{
markModified();
this.VIN = value;
}
public ToManyRelationship getContains()
{
fetch();
return contains;
}
}
Part.java
import com.objy.db.app.ooObj;
import com.objy.db.app.ManyToMany;
import com.objy.db.app.Relationship;
import com.objy.db.app.ToManyRelationship;
public class Part extends ooObj
{
public static ManyToMany belongsTo_Relationship()
{
return new ManyToMany(
"belongsTo", // Relationship field
"Car", // Destination class
"contains", // Inverse relationship
Relationship.COPY_DELETE, // Copying behavior
Relationship.VERSION_DELETE, // Copying behavior
false, // Delete propagation
false, // Lock propagation
Relationship.INLINE_NONE); //Storage mode
}
public int partNum;
public ToManyRelationship belongsTo;
public int getPartNum()
{
fetch();
return partNum;
}
public void setPartNum(int value)
{
markModified();
this.partNum = value;
}
public ToManyRelationship getBelongsTo()
{
fetch();
return belongsTo;
}
}
Note: Bidirectional association automatically takes care of its own integrity. If you delete one side of the association, the other side is automatically deleted. If you delete an object of one side of the association, the association on the other side is deleted automatically.
Adding Associations.To create a relationship between the source object and the specified object use the ToOneRelationship.form() method for the to-one association and ToManyRelationship.add() for the to-many association. partObj.belongsTo.form(carObj); //partObj is the Part object //belongsTo is the name of the to-one association attribute. //carObj is the Car object //partObj.belongsTo.add(carObj); //for to-many associations, use add() method The following is an example of adding 100 parts to 1 car. UnidirectionalAssoc.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 = new Car();
cont.cluster(obj);
Part partObj = null;
for(int i = 0; i < 100; i++)
{
partObj = new Part();
cont.cluster(partObj);
partObj.setPartNum(i);
partObj.belongsTo.form(carObj);
//partObj.belongsTo.add(carObj); //for to-many relationship
}
session.commit();
}
catch(Exception e)
{
e.printStackTrace();
session.abort();
}
}
}
Deleting Associations.To delete a to-one association, you use the drop() method. partObj.belongsTo.remove(); //partObj->belongsTo.drop(carObj); //carObj is the Car that this association is pointing to Continuing from the associations created in the last database, here you find a Part with partNum 23 and delete its association to the car. UnidirectionalAssoc.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);
}
Part partObj = null;
Iterator partItr = cont.scan("Part", "partNum == 23");
if(partItr.hasNext())
{
partObj = (Part) partItr.next();
partObj.belongsTo.remove();
//Car carObj = ...; //instantiate carObj
//partObj.belongsTo.drop(carObj); //for to-many
}
session.commit();
}
catch(Exception e)
{
e.printStackTrace();
session.abort();
}
}
}
|
|||
