Crash while retrieving Results from a Reportquery and query.setEndAtIndex()
Hello, i'm having Problems when retrieving Results with a Reportquery. I set the max number of results to 10 and on the 9th row my loop crashes (because in the database there are only 8 rows). I would expect ojb to only return 8 Rows. Code snippet: var crit = new Packages.org.apache.ojb.broker.query.Criteria(); crit.addEqualToField(logs.menuLinkId,id); var q = new Packages.org.apache.ojb.broker.query.ReportQueryByCriteria(Packages.nymphoon.MenuLink, crit); var attributes = new Array(3); attributes[0] = logs.menuLinkId; attributes[1] = count(*); attributes[2] = name; q.setAttributes(attributes); q.addGroupBy(logs.menuLinkId); q.addGroupBy(name); q.addOrderByDescending(count(*)); q.setEndAtIndex(10); dao.begin(); var it = dao.getReport (q); while ( it.hasNext() ) { var o = it.next(); // crash suggestions.add(o); } dao.commit(); The while loop crashes after the 8th Element. An Iterator of Type broker.accesslayer.PagingIterator (it) is instanciated correctly. I thought that if there are only 8 Rows it.hasNext() would return false? I could add a try/catch in the inner loop, but i don't think this is a good solution. Can anyone help me with this? The loop works fine when i set q.setEndAtIndex(5); i.e. Christoph pgpcucCVfioa7.pgp Description: PGP signature
Re: Crash while retrieving Results from a Reportquery and query.setEndAtIndex()
Jakob Braeuchi schrieb: Hello, what do you mean by 'crash' ? could you please post the exception. hmm of course, sorry that i forgot this :/ The Exception is: org.apache.cocoon.ProcessingException: Failed to execute pipeline.: java.lang.RuntimeException: java.lang.RuntimeException: org.apache.avalon.framework.CascadingRuntimeException: file:/usr/local/jakarta-tomcat-4.1.31/webapps/cocoon_2.1.6/samples/guschtel/nymphoon/flow/authentication.js, line 318: uncaught JavaScript exception: at displayMenu (file:/usr/local/jakarta-tomcat-4.1.31/webapps/cocoon_2.1.6/samples/guschtel/nymphoon/flow/authentication.js, Line 318): java.util.NoSuchElementException: Could not obtain next object: inner hasNext was false 317: while ( it.hasNext() ) { 318: var o = it.next(); // crashes here 319: suggestions.add(o); 320: } Christoph pgpOsS4vqFqk2.pgp Description: PGP signature
Re: Crash while retrieving Results from a Reportquery and query.setEndAtIndex()
Antonio Gallardo schrieb: Hello, Line 318): java.util.NoSuchElementException: Could not obtain next object: inner hasNext was false 317: while ( it.hasNext() ) { 318: var o = it.next(); // crashes here 319: suggestions.add(o); 320: } Thanks. This is inside Cocoon Flow. Seems that OJB has nothing to do in this case. Please send more code of this Javascript. I would recommend to move this business code to a java code and call it from your Flow function. There are some small incompatibilities while manipulating Java object inside Javascript. I have interest in know how is declared it. Which kind of object it is. Same Thing happens when i use it in a java Class. Javascript is just easier to Debug for me, because i'm not using a java IDE. My workaround is to put a try/catch block into the while-loop. My Java-Code: import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.io.*; import java.text.*; import java.util.*; import org.apache.ojb.broker.query.*; ... Criteria crit = new Criteria(); crit.addEqualToField(logs.menuLinkId,id); crit.addEqualToField(stylesheet.id,stylesheetId); ReportQueryByCriteria q = new ReportQueryByCriteria(MenuLink.class,crit); String[] attributes = new String[4]; attributes[0] = logs.menuLinkId; attributes[1] = count(*); attributes[2] = name; attributes[3] = stylesheet.type; q.setAttributes(attributes); q.addGroupBy(logs.menuLinkId); q.addGroupBy(name); q.addGroupBy(stylesheet.type); q.addOrderByDescending(count(*)); // top 10 q.setEndAtIndex(10); ArrayList menulinks = new ArrayList(); dao.begin(); Iterator it = dao.getReport (q); while ( it != null it.hasNext() ) { // Catch Exception try { Object[] o = (Object[]) it.next(); menulinks.add(o); } catch (Exception e) { //nothing } } dao.commit(); Christoph pgp2U6j5MIt9Y.pgp Description: PGP signature
Need help with (Report-) Query
Hello, i'm trying to build a simple query with ojb. SELECT menu_links.*,it.cnt FROM menu_links, (SELECT menu_link_id,COUNT(*) AS cnt FROM logs GROUP BY menu_link_id ORDER BY COUNT(*) DESC) AS it WHERE menu_links.id = it.menu_link_id ORDER BY it.cnt DESC; Basically select all Objects from menu_links where the id matches this query: SELECT menu_link_id FROM logs GROUP BY menu_link_id ORDER BY COUNT(*) DESC LIMIT 10 I tried the following: ReportQueryByCriteria subQuery; Criteria subCrit = new Criteria(); subCrit.addSql(LIMIT 10); // does also not work without this subQuery = QueryFactory.newReportQuery(Log.class, subCrit); subQuery.setAttributes(new String[] { menu_link_id}); subQuery.addGroupBy(COUNT(*)); Criteria crit = new Criteria(); crit.addIn(id, subQuery); QueryByCriteria q = QueryFactory.newQuery(MenuLink.class, crit); Collection mls = dao.retrieve(q); But i always get null. Can anyone help me out on how to do this? Christoph pgpptp6yUhm4d.pgp Description: PGP signature
Re: Update of 1toN relationship
Nils Liebelt schrieb: Hello, I experience a problem while updating a 1toN relationship: I got a contact object with has n service objects. If I create a new service with a certain contactId (foreign key for contact) and store that object, the addressed contact object does not have the newly added service in the corresponding collection immediately. Makes sense cause of the object cache. But if turn the object cache off it still takes time until I retrieve the completed collection? Auto-update is set true. Do I have to update through the contact? put a contact.addService(this); in your setContact() method (imho), correct me if i'm wrong. This works for me. HTH Christoph pgp43wBizJ3Nc.pgp Description: PGP signature
Re: Sorting Child Objects
Robert S. Sfeir schrieb: Hello, Steps 1.) and 2.) are imo ok. the sql contains an order by and ArrayList also maintains the order. The problem is in step 3.) how could we warn the user that his list may cause sorting problems ? Yup exactly what I was originally thinking, I do think however that we can just change from a HashSet to a SortedSet, then the ArrayList and *Set Impl will work. HashSet is fine for speed I guess if there is no ordering. So maybe we check to see if we have orderedby and get a SortedSet, if not HashSet. Then document behavior? If not possible, then definitely throw an exception to get the user's attention. I have a similar Problem (i think). When retrieving a Collection for the first time, the order is fine like i want it. When retrieving it a second or third, or ... time i seem to get a random order. I have this Problem when displaying the collection of objects on cocoon with cforms in a repeater. Updating in the database works fine, the field by which i want to sort is ok. My Repository looks like this: collection-descriptor name=profileMenuCategories element-class-ref=nymphoon.ProfileMenuCategory auto-retrieve=true auto-delete=true auto-update=true orderby=sortNumber sort=ASC !-- foreign key in the other side referencing this class -- inverse-foreignkey field-ref=profileId/ /collection-descriptor sortNumber is a Field in the class ProfileMenuCategory. class-descriptor class=nymphoon.ProfileMenuCategory table=rel_profiles_menu_categories ... field-descriptor name=sortNumber default-fetch=true column=sortnumber jdbc-type=INTEGER/ For the collection in the java Class i use an ArrayList. Did i do something wrong? Or how can i track the Problem further down? Christoph pgpRXt3sOqiUy.pgp Description: PGP signature
Re: Sorting Child Objects
Robert S. Sfeir schrieb: Hello, I have a similar Problem (i think). When retrieving a Collection for the first time, the order is fine like i want it. When retrieving it a second or third, or ... time i seem to get a random order. My disordering is consistently ordered by record ID. So it's not out of order, just not the order I'm telling it to orderby. Ah, ic. My Problem is solved (for now). I changed the Cache implementation from ObjectCacheDefaultImpl to ObjectCachePerBrokerImpl now its working fine (until i get a problem with that ;-)). Christoph pgp0QlZJYE5sT.pgp Description: PGP signature
Re: Sorting Child Objects
Robert S. Sfeir schrieb: Hello, ObjectCachePerBrokerImpl now its working fine (until i get a problem with that ;-)). You wouldn't be using Spring Framework by any chance? I did that because Spring requires it as well. No, i'm just using Cocoon. Christoph pgpZOXSeilpwA.pgp Description: PGP signature
Re: Decomposed m:n mapping with ojb and jdo
Christoph Hermann schrieb: Hello again, The problem seems to lie here: !-- ProfileMenuCategory -- class-descriptor class=papillon.ProfileMenuCategory table=rel_profiles_menu_categories field-descriptor name=profile_id column=profiles_id jdbc-type=INTEGER primarykey=true / field-descriptor name=category_id column=menu_categories_id jdbc-type=INTEGER primarykey=true / field-descriptor name=sortNr column=sort_nr jdbc-type=INTEGER / reference-descriptor name=profile class-ref=papillon.Profile auto-retrieve=true auto-update=false auto-delete=false foreignkey field-ref=profile_id / /reference-descriptor reference-descriptor name=category class-ref=papillon.MenuCategory auto-retrieve=true auto-update=false auto-delete=false foreignkey field-ref=category_id / /reference-descriptor /class-descriptor I can insert Profiles and categories, but i can't insert ProfileMenucategories. Can someone enlight me what is wrong with this definition? Christoph pgpTmHVcYTZfc.pgp Description: PGP signature
Re: Decomposed m:n mapping with ojb and jdo
Thomas Dudziak schrieb: Hello, Both your references use a primarykey part as their foreignkey. Is this really what you want ? The referenced objects must then have the same primarykey value (part) as the ProfileMenuCategory object, and that before you store the ProfileMenutCategory object, otherwise you would overwrite your primarykey, which you definitly don't want. IMO it is easier to have separate fields in ProfileMenuCategory that are only used as foreignkey holders. And for references you can even use anonymous fields, i.e. you don't need real java fields, you can simply add two field descriptors with access=anonymous. Thanks for your answer. I think you hit the point, but i don't exactly know how i would correct it. The code (snippet) is used to figure out where the problem was, is: var pc = new Packages.papillon.ProfileMenuCategory(); pc.setCategory_id(13); pc.setProfile_id(15); pc.setSortNr(1); dao.insert(pc); I get the following exception: ERROR: DAO (org.apache.ojb.broker.PersistenceBrokerSQLException: SQL failure while insert object data for class papillon.ProfileMenuCategory, PK of the given object is [ profile_id=20 category_id=16], object was [EMAIL PROTECTED], exception message is [FEHLER: Einfügen oder Aktualisieren in Tabelle »rel_profiles_menu_categories« verletzt Fremdschlüssel-Constraint »$1« ]) I try to explain what i wanted to acomplish: Profile has a primary Key MenuCategory has a primary key ProfileMenuCategory has (in the database) a primary key (to ensure the relationship can only exist once) consisting of both of these with a foreign key constraint. When i do this, pc.setCategory_id(13); pc.setProfile_id(15); i want the fields in the database stored, and the instances of category and profile in profilemenucategory populated by the objects corresponding to these ids. How can i accomplish this? (Can you show me how the repository.xml for ProfileMenuCategory should look like?) Thx for your help so far, Christoph pgpTR8Cod7fG7.pgp Description: PGP signature
Re: Decomposed m:n mapping with ojb and jdo
Thomas Dudziak schrieb: Hello, You should not use the primarykey for this. It would be better if you add a simple primarykey field to the ProfileMenuCategory, make the other two fields normal fields, and in the database add a unqiue constraint on these two fields. This way, the database will throw an exception when a ProfileMenuCategory object is inserted whose combination of these fields has already been used. No, this is against everything i know of database design. The way you desribed it you are creating an additional _not_needed_ primary key. An entry in the relation-table is identified by the two foreign keys which together build the primary key of this table. Christoph pgpAeLk30mAiv.pgp Description: PGP signature
Decomposed m:n mapping with ojb and jdo
Hello, i try to realize an m:n mapping using ojb/jdo and update the java classes with cocoon using cforms. My problem ist, that i don't get the intermediate table to update correctly in the database. I have the following three classes: public class Profile implements Serializable { private int id; private String name; private Collection menuCategories = new ArrayList(); private Collection profileMenuCategories = new ArrayList(); ... } public class ProfileMenuCategory implements Serializable { private int category_id; private int profile_id; private int sortNr; private Profile profile = new Profile(); private MenuCategory category = new MenuCategory(); ... } public class MenuCategory implements Serializable { private int id; private String name; private Collection profiles = new ArrayList(); private Collection profileMenuCategories = new ArrayList(); ... } and the following Database-Schema (postgreql): CREATE TABLE profiles ( id INT8 DEFAULT nextval('profiles_id_seq'::text) NOT NULL, name VARCHAR(255) NOT NULL, CONSTRAINT profiles_id_pkey PRIMARY KEY(id), CONSTRAINT profiles_name_ukey UNIQUE(name) ); CREATE TABLE menu_categories ( id INT8 DEFAULT nextval('menu_categories_id_seq'::text) NOT NULL, name VARCHAR(255) NOT NULL, CONSTRAINT menu_categories_id_pkey PRIMARY KEY(id), CONSTRAINT menu_categories_name_ukey UNIQUE(name) ); CREATE TABLE rel_profiles_menu_categories ( profiles_id INT8 NOT NULL REFERENCES profiles(id), menu_categories_id INT8 NOT NULL REFERENCES menu_categories(id), sort_nr INT8 DEFAULT 0 NOT NULL, CONSTRAINT rel_profiles_menu_categories_pkey PRIMARY KEY(profiles_id,menu_categories_id) ); My repository.xml looks like this: !-- Profile -- class-descriptor class=papillon.Profile table=profiles field-descriptor name=id primarykey=true nullable=false default-fetch=true autoincrement=true column=id sequence-name=profiles_id_seq jdbc-type=INTEGER / field-descriptor name=name nullable=false default-fetch=true column=name jdbc-type=VARCHAR / collection-descriptor name=profileMenuCategories element-class-ref=papillon.ProfileMenuCategory otm-dependent=false auto-retrieve=true auto-update=false auto-delete=false orderby=sortNr sort=ASC inverse-foreignkey field-ref=profile_id / /collection-descriptor collection-descriptor name=menuCategories element-class-ref=papillon.MenuCategory auto-retrieve=true auto-update=link auto-delete=link indirection-table=rel_profiles_menu_categories otm-dependent=false collection-class=org.apache.ojb.broker.util.collections.ManageableArrayList orderby=sort_nr sort=ASC fk-pointing-to-this-class column=profiles_id/ fk-pointing-to-element-class column=menu_categories_id/ /collection-descriptor /class-descriptor !-- Category -- class-descriptor class=papillon.MenuCategory table=menu_categories field-descriptor name=id primarykey=true nullable=false default-fetch=true autoincrement=true column=id sequence-name=menu_categories_id_seq jdbc-type=INTEGER / field-descriptor name=name nullable=false default-fetch=true column=name jdbc-type=VARCHAR / collection-descriptor name=profileMenuCategories element-class-ref=papillon.ProfileMenuCategory orderby=sortNr sort=ASC auto-retrieve=true auto-update=false auto-delete=false otm-dependent=false inverse-foreignkey field-ref=category_id / /collection-descriptor collection-descriptor name=profiles element-class-ref=papillon.Profile auto-retrieve=true auto-update=link auto-delete=link indirection-table=rel_profiles_menu_categories otm-dependent=false collection-class=org.apache.ojb.broker.util.collections.ManageableArrayList orderby=sort_nr sort=ASC fk-pointing-to-this-class column=menu_categories_id/ fk-pointing-to-element-class column=profiles_id/ /collection-descriptor /class-descriptor !-- ProfileMenuCategory -- class-descriptor class=papillon.ProfileMenuCategory table=rel_profiles_menu_categories field-descriptor name=profile_id column=profiles_id jdbc-type=INTEGER primarykey=true / field-descriptor name=category_id column=menu_categories_id jdbc-type=INTEGER primarykey=true / field-descriptor name=sortNr column=sort_nr jdbc-type=INTEGER / reference-descriptor name=profile class-ref=papillon.Profile auto-retrieve=true auto-update=false auto-delete=false foreignkey field-ref=profile_id / /reference-descriptor reference-descriptor name=category class-ref=papillon.MenuCategory auto-retrieve=true auto-update=false auto-delete=false foreignkey field-ref=category_id / /reference-descriptor /class-descriptor I tried to vary the auto-xxx settings, but none does give the expected results. (No matter what i do, the intermediate table is not updated.)