I can not keep trying right now, but i will tomorrow, the visible
difference i see is that of storing the user i use the way :
Transaction tx = getImplementation().newTransaction();
tx.begin();
db.makePersistent(o);
tx.commit();
But the collections of roles is modified before that. Maybe i'am not
doing it the right way, but it's working for all my other similar
relations...
Are we supposed to lock the object before any modification are made to it ?
When i tried the way you did in the sample you give i get an exception
org.apache.ojb.odmg.TransactionAbortedExceptionOJB: Can't commit
objects: Unexpected behavior
java.lang.RuntimeException: Unexpected behavior
org.apache.ojb.odmg.ObjectEnvelopeTable.cascadeInsertSingleReferences(ObjectEnvelopeTable.java:602)
org.apache.ojb.odmg.ObjectEnvelopeTable.cascadeInsertFor(ObjectEnvelopeTable.java:555)
org.apache.ojb.odmg.ObjectEnvelopeTable.cascadeMarkedForInsert(ObjectEnvelopeTable.java:535)
org.apache.ojb.odmg.ObjectEnvelopeTable.cascadingDependents(ObjectEnvelopeTable.java:504)
Armin Waibel wrote:
Jean-Yves Sironneau wrote:
i'am getting the same error.
i'am going to try to do it with a non abstract class maybe it can be
that.
I setup a similar test
Implementation odmg = OJB.getInstance();
Database db = odmg.newDatabase();
db.open(null, Database.OPEN_READ_WRITE);
BookShelf bookShelf = new BookShelf(prefix);
TransactionExt tx = (TransactionExt) odmg.newTransaction();
tx.begin();
db.makePersistent(bookShelf);
tx.commit();
tx.begin();
tx.lock(bookShelf, Transaction.WRITE);
BookShelfItem ev1 = new DVD(bookShelf);
BookShelfItem ev2 = new Book(bookShelf);
bookShelf.addItem(ev1);
bookShelf.addItem(ev2);
tx.commit();
works fine! BookSelf has a 1:n to abstract class BookSelfItem and the
test add a concrete objects to an existing BookSelf object. The FK is
defined as anonymous field in the abstract class:
<class-descriptor
class="org.apache.ojb.broker.CollectionTest$BookShelfItem">
<extent-class class-ref="org.apache.ojb.broker.CollectionTest$Book"/>
<extent-class class-ref="org.apache.ojb.broker.CollectionTest$DVD"/>
</class-descriptor>
<class-descriptor
class="org.apache.ojb.broker.CollectionTest$BookShelf"
table="COL_BOOKSHELFS"
>
...
<collection-descriptor
name="items"
element-class-ref="org.apache.ojb.broker.CollectionTest$BookShelfItem"
auto-retrieve="true"
>
<inverse-foreignkey field-ref="shelfFk"/>
</collection-descriptor>
</class-descriptor>
<class-descriptor
class="org.apache.ojb.broker.CollectionTest$Book"
table="COL_BOOKS"
>
<field-descriptor
name="pk"
column="PK"
jdbc-type="INTEGER"
primarykey="true"
autoincrement="true"
/>
<field-descriptor
name="shelfFk"
column="BOOKSHELF_FK"
jdbc-type="INTEGER"
access="anonymous"
/>
<reference-descriptor
name="shelf"
class-ref="org.apache.ojb.broker.CollectionTest$BookShelf"
auto-retrieve="true"
auto-update="false"
auto-delete="false"
proxy="true"
>
<foreignkey field-ref="shelfFk"/>
</reference-descriptor>
</class-descriptor>
Did you checked your test with a debugger? Is the PK of the User
object populated with non null value? I don't know what's going on in
your persistence layer, is User 't' locked before the admin is added?
But something like that will work too:
...
BookShelfItem ev1 = new DVD(bookShelf);
BookShelfItem ev2 = new Book(bookShelf);
bookShelf.addItem(ev1);
bookShelf.addItem(ev2);
tx.begin();
tx.lock(bookShelf, Transaction.READ);
db.makePersistent(ev1);
db.makePersistent(ev2);
tx.commit();
Could you setup a test only using the odmg-api which reproduce your
problem?
regards,
Armin
Armin Waibel wrote:
Could you try to set auto-update="link" in 1:n reference of User and
run your test again:
<collection-descriptor
name="roles"
element-class-ref="org.jys.clepsydra.data.users.Role"
auto-update="link"
auto-delete="true"
>
<inverse-foreignkey field-ref="userId"/>
</collection-descriptor>
regards
Armin
Jean-Yves Sironneau wrote:
here is the extract of the repository file :
Oups i think i forgot to explain that Role is an abstract class,
and that Viewer inherits from Role, Author from Viewer and Admin
from Author here are the classes too :
public class Viewer extends Role {
public Viewer(DocumentBase base) {
super(base);
}
public Viewer() {
super();
}
}
public class Author extends Viewer {
public Author(DocumentBase base) {
super(base);
}
public Author() {
super();
}
}
public class Admin extends Author {
public Admin(DocumentBase base) {
super(base);
}
public Admin() {
super();
}
}
<class-descriptor
class="org.jys.clepsydra.data.users.Admin"
table="Admin"
>
<field-descriptor
name="userId"
column="userId"
jdbc-type="VARCHAR"
nullable="false"
length="254"
access="anonymous"
>
</field-descriptor>
<field-descriptor
name="documentBaseId"
column="documentBaseId"
jdbc-type="VARCHAR"
nullable="false"
length="254"
access="anonymous"
>
</field-descriptor>
<field-descriptor
name="id"
column="id"
jdbc-type="VARCHAR"
primarykey="true"
length="254"
>
</field-descriptor>
<reference-descriptor
name="documentBase"
class-ref="org.jys.clepsydra.data.DocumentBase"
auto-update="none"
auto-delete="none"
>
<foreignkey field-ref="documentBaseId"/>
</reference-descriptor>
</class-descriptor>
<class-descriptor
class="org.jys.clepsydra.data.users.Author"
table="Author"
>
<extent-class class-ref="org.jys.clepsydra.data.users.Admin"/>
<field-descriptor
name="userId"
column="userId"
jdbc-type="VARCHAR"
nullable="false"
length="254"
access="anonymous"
>
</field-descriptor>
<field-descriptor
name="documentBaseId"
column="documentBaseId"
jdbc-type="VARCHAR"
nullable="false"
length="254"
access="anonymous"
>
</field-descriptor>
<field-descriptor
name="id"
column="id"
jdbc-type="VARCHAR"
primarykey="true"
length="254"
>
</field-descriptor>
<reference-descriptor
name="documentBase"
class-ref="org.jys.clepsydra.data.DocumentBase"
auto-update="none"
auto-delete="none"
>
<foreignkey field-ref="documentBaseId"/>
</reference-descriptor>
</class-descriptor>
<class-descriptor
class="org.jys.clepsydra.data.users.Role"
table="Role"
>
<extent-class class-ref="org.jys.clepsydra.data.users.Viewer"/>
<field-descriptor
name="userId"
column="userId"
jdbc-type="VARCHAR"
nullable="false"
length="254"
access="anonymous"
>
</field-descriptor>
<field-descriptor
name="documentBaseId"
column="documentBaseId"
jdbc-type="VARCHAR"
nullable="false"
length="254"
access="anonymous"
>
</field-descriptor>
<field-descriptor
name="id"
column="id"
jdbc-type="VARCHAR"
primarykey="true"
length="254"
>
</field-descriptor>
<reference-descriptor
name="documentBase"
class-ref="org.jys.clepsydra.data.DocumentBase"
auto-update="none"
auto-delete="none"
>
<foreignkey field-ref="documentBaseId"/>
</reference-descriptor>
</class-descriptor>
<class-descriptor
class="org.jys.clepsydra.data.users.User"
table="UUser"
>
<field-descriptor
name="id"
column="id"
jdbc-type="VARCHAR"
primarykey="true"
length="254"
>
</field-descriptor>
<field-descriptor
name="creationDate"
column="creationDate"
jdbc-type="DATE"
>
</field-descriptor>
<field-descriptor
name="active"
column="active"
jdbc-type="BIT"
>
</field-descriptor>
<field-descriptor
name="name"
column="name"
jdbc-type="VARCHAR"
nullable="false"
length="254"
>
</field-descriptor>
<field-descriptor
name="password"
column="password"
jdbc-type="VARCHAR"
nullable="false"
length="254"
>
</field-descriptor>
<field-descriptor
name="applicationAdmin"
column="applicationAdmin"
jdbc-type="BIT"
>
</field-descriptor>
<field-descriptor
name="email"
column="email"
jdbc-type="VARCHAR"
length="254"
>
</field-descriptor>
<field-descriptor
name="url"
column="url"
jdbc-type="VARCHAR"
length="254"
>
</field-descriptor>
<collection-descriptor
name="roles"
element-class-ref="org.jys.clepsydra.data.users.Role"
auto-delete="true"
>
<inverse-foreignkey field-ref="userId"/>
</collection-descriptor>
</class-descriptor>
<class-descriptor
class="org.jys.clepsydra.data.users.Viewer"
table="Viewer"
>
<extent-class class-ref="org.jys.clepsydra.data.users.Author"/>
<field-descriptor
name="userId"
column="userId"
jdbc-type="VARCHAR"
nullable="false"
length="254"
access="anonymous"
>
</field-descriptor>
<field-descriptor
name="documentBaseId"
column="documentBaseId"
jdbc-type="VARCHAR"
nullable="false"
length="254"
access="anonymous"
>
</field-descriptor>
<field-descriptor
name="id"
column="id"
jdbc-type="VARCHAR"
primarykey="true"
length="254"
>
</field-descriptor>
<reference-descriptor
name="documentBase"
class-ref="org.jys.clepsydra.data.DocumentBase"
auto-update="none"
auto-delete="none"
>
<foreignkey field-ref="documentBaseId"/>
</reference-descriptor>
</class-descriptor>
Armin Waibel wrote:
Hi Jean-Yves,
in User the 1:n reference the auto-update setting is undefined and
by default OJB use 'link' for 1:n relations, thus I would expect
that the FK in Admin will be set.
Could you post the generated repository.xml for User, Admin, Role?
regards
Armin
Jean-Yves Sironneau wrote:
Hello,
I'am having trouble trying to find out what i am doing wrong, i
have a class user and a class role and a user has a collection of
roles, this kind of schema is working for other classes with the
same kind of relationship but not for this one.
OJB is trying to insert a null value in the column of the foreign
key id, but the role is in the collection of roles of the user
instance.
I'am using latest CVS OJB, my two classes are :
public class User implements INamable {
/**
* @ojb.field primarykey="true"
*/
private String id;
* /**
* @ojb.collection
element-class-ref="org.jys.clepsydra.data.users.Role"
* foreignkey="userId" auto-delete="true"
*/ private List<Role> roles = new
Vector<Role>();
*
/**
* @ojb.field
*/
private String url;
public User(String name) {
this.name = name;
}
public User() {
super();
}
/**
* @return Returns the roles.
*/
public List<Role> getRoles() {
return roles;
}
/**
* @param role
* @throws DataIntegrityException
*/
public void addRole(final Role role) throws
DataIntegrityException {
if (findRole(role.getDocumentBase()) != null) {
throw new DataIntegrityException(this + " already has
a role for " + role.getDocumentBase());
}
roles.add(role);
}
}
and
public abstract class Role implements IDataObject {
/**
* @ojb.field primarykey="true"
*/
private String id;
/**
* @ojb.reference foreignkey="documentBaseId"
auto-update="none" auto-delete="none"
*/
private DocumentBase documentBase;
public Role(DocumentBase base) {
super();
this.documentBase = base;
}
public Role() {
super();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public DocumentBase getDocumentBase() {
return documentBase;
}
public boolean equals(Object object) {
if (object instanceof Role) {
return this.id.equals(((IDataObject) object).getId());
}
return false;
}
public String toString() {
return this.getClass() + " " + this.getDocumentBase();
}
}
and what i am trying to do is
Role admin = new Admin(getCurrentBase());
DataHelper.generateId(admin);
User t = getTestUser();
t.addRole(admin);
DataHelper.store(t);
If u have any clue about... Thank you.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]