Re: Empty Collection - again :(

2004-10-06 Thread Armin Waibel
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: 

//---


Identyfikator wiersza


Nazwa roli









Identyfikator wiersza


Nazwa uzytkownika


Haslo uzytkownika









//---
Best regards
Lukasz Grabski

-
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]


Empty Collection - again :(

2004-10-05 Thread Łukasz Grabski
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: 

//---



Identyfikator wiersza


Nazwa roli







Identyfikator wiersza


Nazwa uzytkownika


Haslo uzytkownika










//---

Best regards
Lukasz Grabski



-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]