• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can someone tell me why this doesn't work? What doing wrong?

 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I have a BookBean that contains a collection of ChapterBeans. Now when you call BookHome.create(...), in ejbPostCreate, it does the following:



When I do that, it throws a TransactionRolledbackException with the message:

"Unexpected token in statement [SELECT TITLE, CHAPTER_NUMBER, BOOK_ID FROM CHAPTER WHERE (ID=?)]"

HOWEVER, if I do the below code instead, it works!



So this tells me it's not a relationship-code/descriptor problem. Is it a transaction issue? What am I not understanding here?

Thank you for ANY help you can give!
[ May 05, 2005: Message edited by: Robert Paris ]
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the first code snippet is doing a findByPrimaryKey() and the second is not. That's why the first is getting the query problem and the second is not.
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean by that. I did a check (with System.out.print) and when I do findByPrimaryKey, it DOES find them and create the local objects.

Does anyone know what else it could be?
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant that the first code example does a findByPrimaryKey() and the second does not. The error message you're getting corresponds to the findByPrimaryKey() query.
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Carman:
I meant that the first code example does a findByPrimaryKey() and the second does not. The error message you're getting corresponds to the findByPrimaryKey() query.


No, it does not. As stated in my previous post, it occurs at the "addAll()" method. Checking the stacktrace, the findByPrimaryKey method returns perfectly, and this error occurs during the load process where it's trying to synch up the cmr fields.
[ May 05, 2005: Message edited by: Robert Paris ]
 
Frank Nimhbauer
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an idea. If you look at the javadoc for Collection.addAll(), you can deduce that it basically calls add() for every object. However, looking at the description for add(), it seems to imply that the implementation of add() is dependent on the underlying implementation of the Collection. Since here you are addressing your collection by it's interface, who knows what's under there? For example, if it's a Set, then it probably calls equals on every object to make sure no duplicates are inserted.
All this to say, try defining your collection as a concrete class like an ArrayList or a Set, maybe something will happen?
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frank Nimhbauer:
Here's an idea. If you look at the javadoc for Collection.addAll(), you can deduce that it basically calls add() for every object. However, looking at the description for add(), it seems to imply that the implementation of add() is dependent on the underlying implementation of the Collection. Since here you are addressing your collection by it's interface, who knows what's under there? For example, if it's a Set, then it probably calls equals on every object to make sure no duplicates are inserted.
All this to say, try defining your collection as a concrete class like an ArrayList or a Set, maybe something will happen?


Thanks for the suggestion.

The problem with defining the class returned from getChapters(), which is the one that addAll() is called on, is that it's created by the EJB server (since that's a CMP field). If you meant the other collection (that's passed in), I tried doing this by not even passing a collection in (just passing in a primary key) and it still doesn't work.

I'm absolutely stumped!
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really hitting a brick wall here. I don't get it, since I have made other classes that seem to be exactly the same (from Monson-Hoefel's book). Anyone?

Using JBoss 3.2 with Java 1.4.2
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robert,

There is something here that looks little bit odd to me. What I don�t understand is why to you pass a collection of remote interfaces to ejbPostCreate()? I assume that the clients of the BookHome bean will first lookup the chapter bean, run a finder method, get the chapter remote interfaces and pass them to the create method of the BookHome bean. If that�s true than I can only tell you that in my opinion, form a design standpoint this is wrong. I would expect it not to work either; but not sure since I never tried anything like this.
Another observation to make is that assuming it will work, then these lines doesn�t make much sense:

Why would you access some beans through their local interfaces and other to their remote interfaces? Local interfaces must be always preferred.
Finally you might notice that the second approach doesn�t use the list of remote interfaces passed through the chapters parameter and work just fine. In my opinion this is actually the way you should implement your method. It also makes a lot of sense from a design perspective: you need only the name of the book in order to grab all chapters. You might also consider moving the code to ejbCreate().
Regards.
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply!

OK, the reason I pass remote ref's is that the client is remote and is creating the book (and the chapters) remotely. It's ok that design-wise this may not be the best, it simply should work (and there can be other instances where a relationship of this sort should work). So, the client creates a Chapter entity via a remote reference, and then in order to create a book, they pass those new chapters to the book bean (which then attempts to create relationships with the chapters passed in via the local interfaces).

I have also tried this where the chapters are created, and then only the primary keys (Integers) are passed to the ejbCreate, and still no luck.

As for moving it to ejbCreate, that is a direct violation of the spec. You MUST not invoke CMR methods in the ejbCreate, but instead in ejbPostCreate.


I just cannot figure out why this isn't working since there's an example almost exactly the same in ReservationBean from Richard Monson-Hoefel's book (where the create method takes a CustomerRemote, gets its primary key and then gets a local interface before creating a connection). So I'm stumped!
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out!! Apparently, the word "POSITION" was causing the error. (I had been storing the position of the chapter in the book collection) It was being interpreted as a keyword. I didn't know Position was a SQL keyword. Where is that from?

Anyways, when I changed it to chapterPosition, everything worked!
[ May 05, 2005: Message edited by: Robert Paris ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!