You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Reggie Hayes <re...@comcast.net> on 2003/07/04 16:15:53 UTC
mapping 1:n assocaition with anonymous
I believe I found a bug performing a mapping for a 1:n association
setting the access attribute of field-descriptor equal to 'anonymous'.
When, I execute the broker.store() method OJB does not store the parent
id in the child table. The code is listed below. The code works if I
modify the Child class to store parentId and remove the anonymous tag
from the field-descriptor. But I am working with existing Java code and
would rather not have to modify it by adding id's in every class that
needs this association in the database.
Repository.xml:
<!-- Definitions for Parent -->
<class-descriptor class="Parent" table="PARENT">
<field-descriptor name="id" column="PARENT_ID" jdbc-type="INTEGER"
primarykey="true" autoincrement="true"/>
<field-descriptor name="name" column="NAME" jdbc-type="VARCHAR"/>
<field-descriptor name="age" column="AGE" jdbc-type="INTEGER"/>
<collection-descriptor name="children"
element-class-ref="Child"
auto-update="true"
orderby="id" sort="DESC">
<inverse-foreignkey field-ref="parentId"/>
</collection-descriptor>
</class-descriptor>
<!-- Definitions for Child -->
<class-descriptor class="Child" table="CHILD">
<field-descriptor name="id" column="CHILD_ID" jdbc-type="INTEGER"
primarykey="true" autoincrement="true"/>
<field-descriptor name="name" column="NAME" jdbc-type="VARCHAR"/>
<field-descriptor name="age" column="AGE" jdbc-type="INTEGER"/>
<field-descriptor name="parentId" column="PARENT_ID"
jdbc-type="INTEGER" access="anonymous"/>
<reference-descriptor name="parent" class-ref="Parent"
auto-update="true">
<foreignkey field-ref="parentId"/>
</reference-descriptor>
</class-descriptor>
public class Parent extends Person {
Parent()
{
super();
setName("Valerie");
setAge(33);
children = new Vector();
}
public void addChildren(Child child)
{
children.add(child);
}
public Vector getChildren()
{
return children;
}
private Vector children;
}
public class Child extends Person {
Child()
{
super();
setName("Carmyn");
setAge(5);
}
public void setParent(Parent parent)
{
this.parent = parent;
}
public Parent getParent()
{
return parent;
}
private Parent parent;
}
public class OJBTester {
public static void main(String[] args) {
PersistenceBroker broker = null;
try {
broker =
PersistenceBrokerFactory.defaultPersistenceBroker();
Child camber = new Child();
Parent reggie = new Parent();
reggie.addChildren(camber);
broker.beginTransaction();
broker.store(reggie);
broker.commitTransaction();
}
catch (Throwable t) {
t.printStackTrace();
}
}
}
inserting "reused" objects in a loop
Posted by Ajitesh Das <aj...@sbcglobal.net>.
When I am trying to "reuse" the object to "store" again, I am getting the
silent failure. If I create a new object before the failed store call, the
"store" method passes thro' fine. Does OJB marks the object as "already
used"?
The following code reproduces the issue:
broker = PersistenceBrokerFactory.defaultPersistenceBroker();
broker.beginTransaction();
Test zs = new Test();
System.out.println("<<<1>>>Setting attributes of Test");
zs.setPosX(30);
zs.setPosY(40);
broker.store(zs);
// if I uncomment following statement, the 2nd "store"
// method works fine
// ts = new Test();
System.out.println("<<<2>>>Setting attributes of Test");
zs.setPosX(31);
zs.setPosY(41);
broker.store(zs); // 2nd broker.store method call
broker.commitTransaction();
the second call "broker.store()" fails silently and does not store
data directly.