1 2 Previous Next 26 Replies Latest reply on Jan 26, 2010 5:20 PM by pmuir Go to original post
      • 15. Re: Error when using @SessionScoped and @Entity Annotation
        nickarls

        As I see it (and I was wrong the last time I answered a producers-question ;-))


        It is essentially request scoped since it is a field of a request scoped bean. It is dependent in the sense that it's not proxied itself.

        • 16. Re: Error when using @SessionScoped and @Entity Annotation
          kodero83

          @gavin, thnx for the code piece of the 'Producer' method.


          However, i can't get any parameters from the view, though i the jsf page doess not complain of any properties not found.


          The code is as above.


          Kindly advice.

          • 17. Re: Error when using @SessionScoped and @Entity Annotation
            janey

            Yes that's true. It doesn't workt that way. I temporary solved the problem by changing the class as follows:


            @RequestScoped @Named
            public class NewAccountAction {
            
                 private float amount;
                 
                 @PersistenceContext 
                 private EntityManager database;
                               
                 /**
                  * @param amount the amount to set
                  */
                 public void setAmount(float amount) {
                      this.amount = amount;
                 }
            
                 /**
                  * @return the amount
                  */
                 public float getAmount() {
                      return amount;
                 }
            
                 /**
                 * 
                 */
                public void persist() 
                {
                     Account newAccount = new Account();
                     newAccount.setAmount(getAmount());
                     
                     System.out.println(newAccount.getAmount()); // works
                     database.persist(newAccount); // no errors
                }  
            [...]
            }    
            



            But I am not really satisfied with that solution. For me it's just a kind of a workaround.


            • 18. Re: Error when using @SessionScoped and @Entity Annotation
              janey

              And I also needed to changed the view:




                  <h:form id="newAccountFormId" styleClass="edit">
              
                      <rich:panel>
                          <f:facet name="header">Add Account</f:facet>            
                          Amount:
                          <h:inputText id="newAmountId" required="true" value="#{newAccountAction.amount}" />                
                      </rich:panel>
              
                      <div class="actionButtons">
              
                          <h:commandButton id="save"
                                        value="Save"
                                       action="#{newAccountAction.persist}"/>
                         
                          <h:commandButton id="cancelAdd" value="Cancel" action="AccountList.xhtml"/>
              
                      </div>
                  </h:form>
              



              • 19. Re: Error when using @SessionScoped and @Entity Annotation
                gavin.king

                I don't understand. The approach I showed should work. Show us exactly what you tried.

                • 20. Re: Error when using @SessionScoped and @Entity Annotation
                  alin.heyoulin.qq.com
                  
                  @RequestScoped @Named
                  public class NewUserAction {
                     @Inject EntityManager em;
                  
                     @Produces @Named
                     private User newUser = new User();
                  
                     public persist() {
                        em.persist(newUser);
                     }
                  }
                  
                  I think newUser is not RequestScoped. So persist this newUser only a new User.
                  
                  


                  • 21. Re: Error when using @SessionScoped and @Entity Annotation
                    alin.heyoulin.qq.com

                    Produces newUser can't inherit NewUserAction's scope.

                    • 22. Re: Error when using @SessionScoped and @Entity Annotation
                      meetoblivion

                      Gavin,


                      In your exact example, I used the following action:




                      @RequestScoped @Named
                      public class NewUserAction {
                      
                         @Produces @Named("newUser")
                         private User newUser = new User();
                      
                         public void persist() {
                            System.out.println("new user's name: "+newUser.getName());
                         }
                      }





                      With user defined as:




                      public class User {
                          private String name;
                      
                          public String getName() {
                              return name;
                          }
                      
                          public void setName(String name) {
                              this.name = name;
                          }
                      }





                      And the view defined as:




                      <html xmlns="http://www.w3.org/1999/xhtml"
                            xmlns:h="http://java.sun.com/jsf/html">
                          <h:head>
                              <title>Facelet Title</title>
                          </h:head>
                          <h:body>
                              Hello from Facelets
                              <h:form>
                                  <h:panelGrid columns="2">
                                      <h:outputLabel value="Name:"/>
                                      <h:inputText value="#{newUser.name}"/>
                                      <h:commandButton action="#{newUserAction.persist}" value="Save"/>
                                  </h:panelGrid>
                              </h:form>
                          </h:body>
                      </html>





                      The following stack trace is encountered:



                      WARNING: /index.xhtml @13,55 value="#{newUser.name}": Target Unreachable, identifier 'newUser' resolved to null
                      javax.el.PropertyNotFoundException: /index.xhtml @13,55 value="#{newUser.name}": Target Unreachable, identifier 'newUser' resolved to null
                              at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:93)
                              at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
                              at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1008)
                              at javax.faces.component.UIInput.validate(UIInput.java:934)
                              at javax.faces.component.UIInput.executeValidate(UIInput.java:1189)
                              at javax.faces.component.UIInput.processValidators(UIInput.java:691)
                              at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1080)
                              at javax.faces.component.UIForm.processValidators(UIForm.java:243)
                              at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1080)
                              at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1080)
                              at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1180)
                              at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
                              at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
                              at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
                              at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
                              at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
                              at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
                              at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
                              at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
                              at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
                              at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
                              at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
                              at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
                              at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)
                              at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
                              at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
                              at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
                              at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
                              at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
                              at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
                              at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
                              at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
                              at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
                              at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
                              at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
                              at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
                              at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
                              at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
                              at java.lang.Thread.run(Thread.java:619)
                      
                      



                      • 23. Re: Error when using @SessionScoped and @Entity Annotation
                        gavin.king

                        Well, that would have to b a bug in Weld. What happens if you use a producer method instead of the producer field?

                        • 24. Re: Error when using @SessionScoped and @Entity Annotation
                          janey

                          I found a post in the WELD forum with a similar problem:


                          My Link


                          If I change the class as said in the post and use a producer method, it works:


                          @RequestScoped @Named
                          public class NewAccountAction {
                          
                               private Account newAccount = new Account();
                               
                               @PersistenceContext 
                               private EntityManager database;
                               
                               @Produces
                               @Named
                               public Account getNewAccount() {
                                 return newAccount;
                               }     
                          
                               /**
                               * 
                               */
                              public void persist() 
                              {
                                   System.out.println(newAccount.getAmount());
                                   database.persist(newAccount);
                              }  
                          [...]
                          



                          Of course, the view also needs to be changed.

                          • 25. Re: Error when using @SessionScoped and @Entity Annotation
                            meetoblivion

                            Alright, so I screwed up in my first run through.  beans.xml was in the wrong spot.


                            So I fixed the project and reran; I can reproduce what's happening.  So it looks like in each request of the view, the request scope is being invoked, the value in the text box doesn't get sent to the same bean.


                            Here's an interesting observation, if my code is this:




                            @RequestScoped @Named
                            public class NewUserAction {
                            
                                @Produces @Named("newUser")
                                public User produceNewUser() {
                                    return newUser;
                                }
                                private User newUser = new User();
                            
                               public void persist() {
                                  System.out.println("new user's name: "+newUser.getName());
                               }
                            }





                            If my code is this, it doesn't




                            @RequestScoped @Named
                            public class NewUserAction {
                            
                                @Inject @Named("newUser") User newUser;
                            
                               public void persist() {
                                  System.out.println("new user's name: "+newUser.getName());
                               }
                            }
                            public class UserProducer {
                                @Produces @Named("newUser")
                                public User produceNewUser() {
                                    return newUser;
                                }
                               private User newUser = new User();
                            }







                            Of course, the view also needs to be changed.



                            It's not supposed to.  I changed my code and it works correctly.

                            • 26. Re: Error when using @SessionScoped and @Entity Annotation
                              pmuir

                              I tested this in trunk, and can't reproduce...


                              http://fisheye.jboss.org/changelog/weld/?cs=5633

                              1 2 Previous Next