4 Replies Latest reply on Feb 9, 2008 10:49 PM by Sart Valcerou

    Problem with injection

    Sart Valcerou Newbie
      Hello !
      I'm pretty new to Seam but i really like it :)
      I found that i can't inject entity bean into session bean.

      Can anyone help me ?

      This is my code..


      `@Entity
      @Name("user")
      @Table(name = "user", catalog = "seam")
      public class User implements java.io.Serializable {

           private Integer userId;
           private Setting setting;
           private String userName;

           public User() {
           }

           public User(Setting setting, String userName) {
                this.setting = setting;
                this.userName = userName;
           }

           @Id
           @GeneratedValue(strategy = IDENTITY)
           @Column(name = "user_id", unique = true, nullable = false)
           public Integer getUserId() {
                return this.userId;
           }

           public void setUserId(Integer userId) {
                this.userId = userId;
           }
           @ManyToOne(fetch = FetchType.LAZY)
           @JoinColumn(name = "setting_id", nullable = false)
           @NotNull
           public Setting getSetting() {
                return this.setting;
           }

           public void setSetting(Setting setting) {
                this.setting = setting;
           }

           @Column(name = "user_name", nullable = false, length = 45)
           @NotNull
           @Length(max = 45)
           public String getUserName() {
                return this.userName;
           }

           public void setUserName(String userName) {
                this.userName = userName;
           }

      }
      `


      `
      @Entity
      @Name("setting")
      @Table(name = "setting", catalog = "seam")
      public class Setting implements java.io.Serializable {

           private Integer settingId;
           private String settingName;
           private Set<User> users = new HashSet<User>(0);

           public Setting() {
           }

           public Setting(String settingName) {
                this.settingName = settingName;
           }
           public Setting(String settingName, Set<User> users) {
                this.settingName = settingName;
                this.users = users;
           }

           @Id
           @GeneratedValue(strategy = IDENTITY)
           @Column(name = "setting_id", unique = true, nullable = false)
           public Integer getSettingId() {
                return this.settingId;
           }

           public void setSettingId(Integer settingId) {
                this.settingId = settingId;
           }

           @Column(name = "setting_name", nullable = false, length = 45)
           @NotNull
           @Length(max = 45)
           public String getSettingName() {
                return this.settingName;
           }

           public void setSettingName(String settingName) {
                this.settingName = settingName;
           }
           @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "setting")
           public Set<User> getUsers() {
                return this.users;
           }

           public void setUsers(Set<User> users) {
                this.users = users;
           }

      }
      `
      this is my session bean
      `
      @Name("insertData")
      public @Stateless class InsertDataBean implements InsertData {
           
           @In(create=true)
           private User user;
           
           @In(create=true)
           private Setting setting;
           
           @PersistenceContext
           EntityManager em;
           
           public void insert(){
               user.setSetting(setting);
               em.persist(user);
           }
      }
      `

      `<h:form>
                <h:inputText value="#{user.userName}"/><br />
                <h:inputText value="#{setting.settingName}"/>
                
                <h:commandButton value="Persist" action="#{insertData.insert}"/>
           </h:form>`
        • 1. Re: Problem with injection
          Sart Valcerou Newbie

          @Name("insertData")
          public @Stateless class InsertDataBean implements InsertData {
               
               @In(create=true)
               private User user;
               
               @In(create=true)
               private Setting setting;
               
               @PersistenceContext
               EntityManager em;
               
               public void insert(){
                    user.setSetting(setting);
                   em.persist(user);
               }
          }


          • 2. Re: Problem with injection
            Sart Valcerou Newbie

            this is my view



            <h:form>
                      <h:inputText value="#{user.userName}"/><br />
                      <h:inputText value="#{setting.settingName}"/>
                      
                      <h:commandButton value="Persist" action="#{insertData.insert}"/>
                 </h:form>



            this is exception



            Exception during request processing:
            Caused by javax.servlet.ServletException with message: "#{insertData.insert}: javax.ejb.EJBTransactionRolledbackException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.asd.proj.User.setting"
            
            javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
            org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
            org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
            org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
            org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
            org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
            org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
            org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:44)
            org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
            org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
            org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
            org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:60)
            org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
            org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
            org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
            org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68)
            org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
            org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
            org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
            org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
            org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
            org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
            org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
            org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
            org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
            org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
            org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
            org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
            org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
            org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
            org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
            org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
            org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
            org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
            java.lang.Thread.run(Unknown Source)
            
            
            


            • 3. Re: Problem with injection
              Kenneth Christensen Novice

              You need to persist 'setting' before you persist 'user':


              public void insert() {
                  em.persist(setting);
                  user.setSetting(setting);
                  em.persist(user);
              }
              



              or use a CascadeType.ALL.

              • 4. Re: Problem with injection
                Sart Valcerou Newbie

                :) Thank you very much !