9 Replies Latest reply on Mar 11, 2002 2:54 AM by Todd Marshall

    calling finders from the bean class

    Todd Marshall Newbie

      I've defined an entity bean with CMP. In it, I have a username that I want to check for uniqueness upon creation. To do this, I need to call a finder method from my bean class. Since the container is defining the finder methods using jaws and the Home interface, how do I make this call?

      -Todd

        • 1. Re: calling finders from the bean class
          Wellie Chao Newbie

          Why don't you just define the column as unique and have the database handle the problem of enforcing uniqueness? You could make the create method accept an id and username.

          • 2. Re: calling finders from the bean class
            Todd Marshall Newbie

            I thought of that, but when a new user is created, I need to return (or throw) an error condition back to the calling code, so a page can be displayed prompting the user for a new user id.

            I could catch the sql exception that the database will throw if a unique constraint is violated in the db, but wouldn't that then tie the application to a specific database? My experience tells me that I'd get a proprietary error code back in the exception (i.e. oracle is going to give me a different error code than postgres would for violating a unique constraint).

            This is quite important since we are developing the app on postgres, but it must scale to either DB2 or Oracle in the future.

            -Todd

            • 3. Re: calling finders from the bean class
              Raja Master

              You can make the userid as a primary key.. And catch DuplicateKeyException.

              • 4. Re: calling finders from the bean class
                Jason Carroll Newbie

                Also if your field isn't part of the primary key and you are using JBoss 3.0(EJB 2.0) you could use an ejbSelect method to do what you want.

                • 5. Re: calling finders from the bean class
                  Todd Marshall Newbie

                  unfortunately, I can't use 3.0 till it's stable...

                  just for the hell of it, I tried setting the login_name field to unique in the database. However, when a violation happens, the SQLException is thrown on the server side, not on the client side. All I get on the client is a javax.transaction.RollbackException. Which as far as I can tell is useless to me for this application, and the server would still be throwing SQLExceptions.

                  Anyway, can you throw exceptions in the EJB class that you can then catch in your calling JSPs? I've been looking at RemoteExceptions...


                  -Todd

                  • 6. Re: calling finders from the bean class
                    Chris Huang Newbie

                    From jsp, you can call home.findByPrimaryKey() before calling home.create().

                    • 7. Re: calling finders from the bean class
                      Todd Marshall Newbie

                      crap, that's pretty obvious... guess I didn't think of that cause it's too damn easy. :)

                      thanks a bunch chuang, that solves my problem (except I'll call a finder method for the login name findByLoginName() )

                      just for giggles, does anyone know how to throw exceptions in the bean class, and catch them in the calling jsp? I thought of some other scenarios last night where I might want to do this...

                      -T

                      • 8. Re: calling finders from the bean class
                        Todd Marshall Newbie

                        hmmm... There is a problem with checking uniqueness with a findBy method in the jsp. There is a *very* small chance that while the jsp was doing the search for a username, another user could submit that username, and the code wouldn't detect it.

                        This will still work for my purposes, but if you needed to enforce uniqueness on a piece of data that had a higher probability of collision, then it wouldn't be precise enough.

                        -T

                        • 9. Re: calling finders from the bean class
                          Todd Marshall Newbie

                          I ended up using a much more optimal solution (that should get rid of the chance for race conditions...)

                          in the setLoginName method of my ejb class, I get the home interface of my bean and invoke a finder to find duplicates, if one is found, I throw an exception.

                          quick note on throwing exceptions from your ejb class:
                          you must add a throws clause to your main interface, and any create methods in your home interface that call the throwing code.

                          (your compiler will not remind you about any exceptions you've forgotten to throw in your home class)

                          -Todd