Hi Lukasz,
looks strange to me ;-)
You want to map a m:n relation between User and Role, then both
descriptor have to include a collection-descriptor pointing to
indirection table.
http://db.apache.org/ojb/docu/guides/basic-technique.html#Support+for+Non-Decomposed+m%3An+Mappings
Then in your code it seems you want to do insert and link of objects by
hand (delete too?), then you have to set auto-update/delete to NONE
http://db.apache.org/ojb/docu/guides/basic-technique.html#Link+references
or if OJB should do the job set OBJECT.
Please have a look in test suite class M2NTest.java. This class shows
the object handling with different auto-xxx settings.
regards,
Armin
ukasz Grabski wrote:
Hi
I have funny problem with OJB and Collections (m:n relations). Sample servlet code
(GET metgod):
//---
//adding roles
CMSRole admin = new CMSRole(CMS_ADMIN);
CMSRole author = new CMSRole(CMS_AUTHOR);
CMSRole developer = new CMSRole(CMS_DEVELOPER);
CMSRole editor = new CMSRole(CMS_EDITOR);
CMSRole censor = new CMSRole(CMS_CENSOR);
broker.beginTransaction();
broker.store(admin);
broker.store(author);
broker.store(developer);
broker.store(editor);
broker.store(censor);
broker.commitTransaction();
//adding user with roles
CMSUser test1 = new CMSUser(test1, test1);
test1.addRole(new CMSRole(CMS_CENSOR));
test1.addRole(new CMSRole(CMS_EDITOR));
broker.beginTransaction();
broker.store(test1);
broker.store(test2);
broker.serviceBrokerHelper().link(test1, true);
broker.serviceBrokerHelper().link(test2, true);
broker.commitTransaction();
out.println(ROLES LIST: \n);
QueryByCriteria rolesQuery = new QueryByCriteria(CMSRole.class, new Criteria());
result = broker.getCollectionByQuery(rolesQuery);
Iterator iter = result.iterator();
while (iter.hasNext()) {
CMSRole rola = (CMSRole)iter.next();
out.println(Role name: + rola.getRoleName());
}
QueryByCriteria usersQuery = new QueryByCriteria(CMSUser.class);
result = broker.getCollectionByQuery(usersQuery);
Iterator iter2 = result.iterator();
out.println(USERS LIST: \n);
while (iter2.hasNext()) {
CMSUser user = (CMSUser)iter2.next();
out.println(User name: + user.getUserName());
out.println(Roles count: + user.getRoles().size() + \n);
}
//---
This code creates few Roles and then creates user with two roles added. And here is a funny part: when database is empty and I'm adding roles and user first time, everything is fine, I get string Roles count: 2. But when I comment adding roles and user code and refresh page, Roles count is 0. What am I doing wrong ? Here is a part of my repository.xml file:
//---
class-descriptor
class=org.imeri.cms.ojb.security.CMSRole
table=CMS_ROLES
field-descriptor
name=roleId
column=role_id
jdbc-type=INTEGER
primarykey=true
indexed=true
autoincrement=true
documentationIdentyfikator wiersza/documentation
/field-descriptor
field-descriptor
name=roleName
column=role_name
jdbc-type=VARCHAR
nullable=false
length=50
documentationNazwa roli/documentation
/field-descriptor
index-descriptor
name=ROLE_NAME_UNIQUE
unique=true
index-column name=role_name/
/index-descriptor
/class-descriptor
class-descriptor
class=org.imeri.cms.ojb.security.CMSUser
table=CMS_USERS
field-descriptor
name=userId
column=user_id
jdbc-type=INTEGER
primarykey=true
indexed=true
autoincrement=true
documentationIdentyfikator wiersza/documentation
/field-descriptor
field-descriptor
name=userName
column=user_name
jdbc-type=VARCHAR
nullable=false
length=50
documentationNazwa uzytkownika/documentation
/field-descriptor
field-descriptor
name=password
column=user_password
jdbc-type=VARCHAR
nullable=false
length=50
documentationHaslo uzytkownika/documentation
/field-descriptor
collection-descriptor
name=roles
collection-class=org.apache.ojb.broker.util.collections.ManageableVector
element-class-ref=org.imeri.cms.ojb.security.CMSRole
indirection-table=USERS_ROLES_KEYS
auto-retrieve=true
auto-update=link
auto-delete=link
fk-pointing-to-this-class column=userId/
fk-pointing-to-element-class column=roleId/
/collection-descriptor
index-descriptor
name=USER_NAME_UNIQUE
unique=true
index-column name=user_name/
/index-descriptor
/class-descriptor