Please help JEE people!D:

Discussion in 'Unrelated Discussion' started by nofear1299, February 27, 2014.

  1. nofear1299

    nofear1299 Active Member

    Messages:
    294
    Likes Received:
    147
    I'm busy doing an assignment and have been stuck on this for a few hours now. I posted a question on StackOverflow but no one is helping yet and it's nearly 3am and I need to get this at least done tonight.

    So here is my question:

    I need to update a field using JPA. My connection pool has all the settings for the Derby server.

    I want to update all the account's balances. I have this method in my DAO.

    Code:
    publicvoid addInterest(Account acc){// TODO Auto-generated method stubint accBal = acc.getBalance();
        accBal = accBal +1000;
        acc.setBalance(accBal);}
    I have added

    @PersistenceContextprivateEntityManager em;
    to the top of the file.

    My JSF calls a method in the backing bean -

    Code:
    publicString adjust(){
    List<Account>accList = service.getAllAccounts();
    try{
    for(Account n : accList){
                service.addInt(n);
    }
    return"success";
    }catch(Exception e){return"failed";}}
    where service is my interface to my access class.

    I can't seem to get it to update. I have tried persisting it again, tried making it's own entitymanager in that.

    I have absolutely no idea now. Any help would be appreciated

    Everything I try I get an EJBTransactionRolledBackException

    Please help awesome peoples D:

    And my lecturer didn't tell us how to do this. I tried a query, everything I can find on the net:(
  2. cola_colin

    cola_colin Moderator Alumni

    Messages:
    12,074
    Likes Received:
    16,221
    I tried to format the code a little bit.
    All I am seeing now is that you are calling a method addInt that is not mentioned anywhere else in your post.

    However in general NEVER catch execeptions without logging them. At least print a stacktrace to some log.
  3. nofear1299

    nofear1299 Active Member

    Messages:
    294
    Likes Received:
    147
    Well here is the whole thing from beginning to end:
    JSF backing bean :
    Code:
    package backingBeans;
    
    import java.util.List;
    
    import javax.ejb.EJB;
    import javax.faces.bean.ManagedBean;
    import javax.persistence.EntityManager;
    
    import bankManagement.AccountAccessInterfaceLocal;
    import bankManagement.domain.Account;
    
    @ManagedBean(name = "interestAdjust")
    public class InterestBackingBean {
    
        private int balance;
        final private int  INT_PERC = 1;
        @EJB
        private AccountAccessInterfaceLocal service;
    
      
        public int getBalance() {
            return balance;
        }
    
        public void setBalance(int balance) {
            this.balance = balance;
        }
      
        public String adjust()
        {
            System.out.println("adjsut method");
            List<Account>accList = service.getAllAccounts();
            try {
                for (Account n : accList)
                {
                    service.addInt(n);
                }
                return "success";
            }
            catch (Exception e)
            {
                return "failed";
            }
        }
    }
    
    Then here is the account access class which the jsf is linked to:
    Code:
    @[USER=1959437]stateless[/USER]
    public class AccountAccess implements AccountAccessInterfaceRemote,AccountAccessInterfaceLocal {
    
        @EJB
        private AccountDataAccessInteface dao;
        @Override
        public List<Account> getAllAccounts() {
            return dao.getAccounts();
        }
    
    
        @Override
        public void addInt(Account acc) {
            // TODO Auto-generated method stub
            dao.addInterest(acc);
        }
    
    
    Then the dao class:

    Code:
    @[USER=1959437]stateless[/USER]
    public class AccountDataAccess implements AccountDataAccessInteface {
    
        @PersistenceContext
        private EntityManager em;
    
        public void addInterest(Account acc) {
            // TODO Auto-generated method stub
    
            int accBal = acc.getBalance();
            accBal = accBal + 1000;
            acc.setBalance(accBal);
        }
    The adding of accounts works, but not deleting or updating.
    Last edited: February 27, 2014
  4. nofear1299

    nofear1299 Active Member

    Messages:
    294
    Likes Received:
    147
    And I'm getting the stacktrace in my console - it says:
    Code:
    Exception in thread "main" javax.ejb.EJBTransactionRolledbackException
        at com.sun.ejb.containers.BaseContainer.mapLocal3xException(BaseContainer.java:2279)
        at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2060)
        at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1979)
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:220)
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
        at com.sun.proxy.$Proxy744.addInterest(Unknown Source)
        at bankManagement.AccountAccess.addInt(AccountAccess.java:73)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
        at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
        at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
        at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
        at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
        at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
        at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:55)
        at sun.reflect.GeneratedMethodAccessor1658.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
        at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
        at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
        at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
        at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
        at sun.reflect.GeneratedMethodAccessor1696.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
        at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
        at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
        at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667)
        at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655)
        at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:205)
        at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:79)
        at com.sun.proxy.$Proxy746.addInt(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie.dispatchToMethod(ReflectiveTie.java:143)
        at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:173)
        at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatchToServant(ServerRequestDispatcherImpl.java:528)
        at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatch(ServerRequestDispatcherImpl.java:199)
        at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequestRequest(MessageMediatorImpl.java:1549)
        at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:1425)
        at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleInput(MessageMediatorImpl.java:930)
        at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:213)
        at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:694)
        at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.dispatch(MessageMediatorImpl.java:496)
        at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.doWork(MessageMediatorImpl.java:2222)
        at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
        at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)
    Caused by: javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean
        at com.sun.ejb.containers.EJBContainerTransactionManager.checkExceptionClientTx(EJBContainerTransactionManager.java:662)
        at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:507)
        at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4475)
        at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2009)
        ... 52 more
    Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
    Exception Description: Syntax error parsing [UPDATE Account a SET a.balance = 11000 WHERE acc.firstName LIKE Matt AND acc.surname LIKEJordaan].
    [45, 96] The expression is not a valid conditional expression.
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1585)
        at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createQuery(EntityManagerWrapper.java:456)
        at bankManagement.dataAccess.AccountDataAccess.addInterest(AccountDataAccess.java:67)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
        at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
        at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
        at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
        at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
        at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
        at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
        at sun.reflect.GeneratedMethodAccessor1658.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
        at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
        at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
        at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
        at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
        at sun.reflect.GeneratedMethodAccessor1696.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
        at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
        at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
        at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667)
        at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655)
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
        ... 50 more
    Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JPQLException
    Exception Description: Syntax error parsing [UPDATE Account a SET a.balance = 11000 WHERE acc.firstName LIKE Matt AND acc.surname LIKEJordaan].
    [45, 96] The expression is not a valid conditional expression.
        at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
        at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:334)
        at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
        at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1583)
        ... 80 more
    
  5. cola_colin

    cola_colin Moderator Alumni

    Messages:
    12,074
    Likes Received:
    16,221
    2. things:

    1.) Again never do this kind of thing anywhere:

    try {...}catch(Exception e){return "ERROR";}

    You will feel bad once you end up getting that ERROR in a production system where you can't quickly add a log of the actual error information. Like really BAD.
    Always at least do
    e.printStackTrace(); to print out all info about the problem.

    2.) The most interesting part of stacktraces is the bottom. There is the actual cause. Read the stacktrace you posted. Like the last few lines:
    Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JPQLException
    Exception Description: Syntax error parsing [UPDATE Account a SET a.balance = 11000 WHERE acc.firstName LIKE Matt AND acc.surname LIKEJordaan].
    [45, 96] The expression is not a valid conditional expression.

    I have no idea where that SQL is coming from. I have never used the exact frameworks you are using. But something is creating invalid SQL.

    EDIT:
    Judging by the fact that updating an account by using the name with LIKE is a pretty damn bad practice I'd guess that SQL is somehow defined by you.
    I'd recommend to use an account id instead.
  6. nofear1299

    nofear1299 Active Member

    Messages:
    294
    Likes Received:
    147
    The thing is the db server creates the sql using JPQL I haven't added any SQL.

    I tidied it up so the addInt() method is just:
    Code:
        public void addInterest(Account acc) {
            // TODO Auto-generated method stub
            int accBal = acc.getBalance();
            accBal = accBal + 1000;
            acc.setBalance(accBal);
        }
    and I don't get any errors but it is not committing
  7. cola_colin

    cola_colin Moderator Alumni

    Messages:
    12,074
    Likes Received:
    16,221
    Then how does the JPQL know that it should use like and the names like this?
    You have to have defined that in some way.
  8. nofear1299

    nofear1299 Active Member

    Messages:
    294
    Likes Received:
    147
    I can mail you the whole project if you like. I am assuming it uses the account details using the get and sets.

    I did try using an sql statement earlier but have since taken that out awhile back
  9. nofear1299

    nofear1299 Active Member

    Messages:
    294
    Likes Received:
    147
    I'm not getting any errors now but it just isn't committing
  10. cola_colin

    cola_colin Moderator Alumni

    Messages:
    12,074
    Likes Received:
    16,221
    Try to dig into how to make it us a specific id to identify accounts. Using the names with like is plain wrong on many levels. I've never used JPQL and I am a pretty big fan of not using ORM frameworks anyway. They usually just obfuscate what is happening. Better use a pure SQL DSL for Java/Scala/your language of choice.
    No need to send me the project, I need to go sleep now. Good luck
  11. cptconundrum

    cptconundrum Post Master General

    Messages:
    4,186
    Likes Received:
    4,900
    Yeah, updating with a LIKE is pretty scary. Actually, LIKE is pretty scary in general. I have found it useful a few times, but I usually prefer a separate search indexing service for that.

    Like colin said though, your data should never be structured in a way that makes you look up single rows using multiple inputs. You use an id for looking up a specific row and leave other fields for searches that might contain more than one result.
  12. nofear1299

    nofear1299 Active Member

    Messages:
    294
    Likes Received:
    147

    I was using it in a for loop to update all of the records in it now I got that working by doing this:

    Code:
        @Override
        @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
        public void addInterest(Account acc) {
            // TODO Auto-generated method stub
            Account up = em.find(Account.class, acc.getId());
            int accBal = up.getBalance();
            accBal = accBal + 1000;
            up.setBalance(accBal);
        }
    Now I am having an issue with another method. I want to shoot myself. :(

    I want to take an employee and add money to it - so essentially a deposit function. But for some reason I can only send one parameter to my backing bean and I lose the id in the process of going from one page to another and I can't get it back...

    I want to break my computer in half
    cptconundrum likes this.
  13. nofear1299

    nofear1299 Active Member

    Messages:
    294
    Likes Received:
    147
    @cptconundrum Don't just like! HELP ME D': I've been up for 24 hours now fighting this bitch. Any clues?:p
  14. cptconundrum

    cptconundrum Post Master General

    Messages:
    4,186
    Likes Received:
    4,900
    Well that's progress! Unfortunately java leaves you with so many different ways to do the same thing when it comes to java web frameworks that I don't really have any way to tell you exactly what to do (and since it's homework, I probably wouldn't anyway ;)). You might want to try saving the id in the url that gets called or passing it along in the request object, depending on how things are set up.

    *Edit*
    Also, I fully understand deadlines and have put in more than a couple all nighters myself (trust me, you don't really start to hallucinate until about 60 hours awake), but in almost every case I can think of I found the answer pretty quickly after a short nap. Caffeine is a hell of a drug, but two hours of sleep can be worth so much more.
    Last edited: February 27, 2014
  15. nofear1299

    nofear1299 Active Member

    Messages:
    294
    Likes Received:
    147
    This just wasn't explained in any of our notes, which is frustrating the hell out of me.

    I don't understand why it is seemingly impossible to pass two parameters to a function in a jsf, or why my variables are staying in memory.
  16. cptconundrum

    cptconundrum Post Master General

    Messages:
    4,186
    Likes Received:
    4,900
    What do you mean by your variables are staying in memory? That's what variables do. :p

    If you meant to say they are not staying in memory, well that makes sense too. Java on a web server tries as hard as it can to forget everything between http requests. Variables really only stick around if they are static, but java gives you access to request and session objects as well. Without knowing how your project is set up, I can't really pick a wy of doing it for you. I suspect you will not want to use static variables though because you only want the one person to access it, and you won't want to put it in the session object because it shouldn't persist past the one or two requests. That leaves you with just the request and response objects for passing values from server to client and back between requests.

    I hope that works as a starting point for you, because I really need to get to sleep too. Good luck!
  17. nofear1299

    nofear1299 Active Member

    Messages:
    294
    Likes Received:
    147
    Right now I don't care, this is above and beyond the scope of what is expected. I just feel it has to be there for completeness.

    I'm leaning towards the static var as that would be easiest I think - security isn't a problem it is not going to go live.

    So I just declare it as a static variable and it will stay?
  18. cptconundrum

    cptconundrum Post Master General

    Messages:
    4,186
    Likes Received:
    4,900
    Static variables aren't tied to the request. It is the wrong way to do this for a lot of reasons, but yes it should allow a variable to persist across all users, all sessions, and all requests forever until the server restarts or the value gets changed. It isn't just bad security, you're also begging for bugs and it will break if you ever have more than one user doing things at once or if a request only partially completes and then has an error. Basically, just don't. ;)

    Typically you might find this useful for global config options on the server or for something like a database connection pool.
  19. nofear1299

    nofear1299 Active Member

    Messages:
    294
    Likes Received:
    147
    So, all I had to do was add @SessionScoped to the bean classes.

    FML. I spent 8 hours on that last night

    I COULD HAVE SLEPT
  20. nofear1299

    nofear1299 Active Member

    Messages:
    294
    Likes Received:
    147
    @cptconundrum Oh didn't see your edit - and coffee saved my life. All seems to be working now barring my delete statement... But i will take it

Share This Page