5 Replies Latest reply on May 3, 2010 8:08 AM by anitha.nagani.raj.gmail.com

    Updating the values in each table in the database

    anitha.nagani.raj.gmail.com

      Hello all,


      I'm a newbie to seam and I'm developing an application using Seam framework. I integrated seam with eclipse and i developed a CRUD application.


      I have 6 tables in my database and corresponding entity beans. In each entity bean i have fields for user who accessed the application lastly and the time when the application was lastly modified.


      But I'm not displaying these two fields in the view. That is, I don't want the user of the application to enter these two fields manually but it should get automatically updated in the database.


      I read few documents regarding how to do this but till now nothing was useful. Please help me in doing this.

        • 1. Re: Updating the values in each table in the database
          raphaufrj

          What you have to do it's to audit your coluns using JPA. Follow this workaround: http://blogs.exist.com/aco/2008/03/24/reusing-jpa-models-using-embedded-entities-and-composition/

          • 2. Re: Updating the values in each table in the database
            tausuahmed

            Hi Anitha,



            I Suppose you have Generated your project using seam-gen process. If that is the case you will have for each entity a Home class and List class in your action package,
            and for your requirement you can set the two fields which you want to get automatically updated in the database in your Home class inside createInstance() method.



            Regards,
            Tauseef

            • 3. Re: Updating the values in each table in the database
              anitha.nagani.raj.gmail.com

              Hi,
              Thanks for the reply.. But can you tell me in brief how to set those fields in Home class?


              package org.domain.user.session;


              import java.util.Date;


              import org.domain.user.entity.;
              import org.jboss.seam.annotations.Name;
              import org.jboss.seam.framework.EntityHome;

              @Name(customerHome)
              public class CustomerHome extends EntityHome {

                   
                     

                      public void setCustomerCustomerId(Integer id) {
                              setId(id);
                      }

                      public Integer getCustomerCustomerId() {
                              return (Integer) getId();
                      }

                      @Override
                      protected Customer createInstance() {
                              Customer customer
              new Customer();
                              String lastUser;
                              Date lastUpdatedUser;


                              return customer;
                      }


                      public void load() {
                              if (isIdDefined()) {
                                      wire();
                              }
                      }


                      public void wire() {
                              getInstance();
                      }


                      public boolean isWired() {
                              return true;
                      }


                      public Customer getDefinedInstance() {
                              return isIdDefined() ? getInstance() : null;
                      }


              }


              This is my Home class and i want to update last user and last updated user fields in the database.
              After seeing your reply i have put those two fields in the createInstance() method after creating a new instance for the customer. Is this correct? If not, please help me in proceeding.

              • 4. Re: Updating the values in each table in the database
                tausuahmed

                Hi anitha,


                Inside your createInstance() method write the code to get your lastUser and lastUpdatedUser values


                I have written sample code, hope you understand it.


                @Override protected Customer createInstance() {


                Customer customer = new Customer();


                String lastUser =  getlastUserInfo(); // get lastUser Info using getInfo() method.


                //set the value you want to save
                customer.setlastUser(lastUser);


                Date lastUpdatedUser = getInfolastUpdatedUser(); //get lastUpdatedUser  Info using getlastUpdatedUserInfo() method.


                //set the value you want to save
                customer.setlastUpdatedUser(lastUpdatedUser);


                return customer;


                }

                • 5. Re: Updating the values in each table in the database
                  anitha.nagani.raj.gmail.com
                  Hello,

                  Very Thanks for providing me this idea. Have a look at my changed code.

                  CustomerHome.java

                  @Override
                       protected Customer createInstance() {
                            Customer customer = new Customer();
                            Date lastUpdatedUser = getLastUpdatedUser();
                            customer.setLastUpdatedUser(new java.sql.Date(System.currentTimeMillis()));
                            return customer;
                       }

                  public Date getLastUpdatedUser()
                       {
                           return lastUpdatedUser;     
                       }

                  As you have said in the last post i have created get and set methods in home.java. What should be the type of this field in customer table? I had 'time' as the type for this field in the table previously.
                  If i change to 'date' type or any other type than 'time' i get the following error when i try to add any customer.

                  /home.xhtml: Error reading 'resultList' on type org.domain.user.session.CustomerList_$$_javassist_seam_6.

                  Stacktrace says


                  javax.el.ELException: /home.xhtml: Error reading 'resultList' on type org.domain.user.session.CustomerList_$$_javassist_seam_6
                       at com.sun.facelets.compiler.TextInstruction.write(TextInstruction.java:48)
                       at com.sun.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:39)
                       at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:275)
                       at org.richfaces.renderkit.html.PanelRenderer.doEncodeBegin(PanelRenderer.java:189)
                       at org.richfaces.renderkit.html.PanelRenderer.doEncodeBegin(PanelRenderer.java:148)
                       at org.ajax4jsf.renderkit.RendererBase.encodeBegin(RendererBase.java:100)
                       at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:788)
                       at javax.faces.component.UIComponent.encodeAll(UIComponent.java:884)
                       at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
                       at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592)
                       at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
                       at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
                       at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
                       at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
                       at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
                       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
                       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
                       at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
                       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                       at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
                       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                       at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
                       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                       at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
                       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                       at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
                       at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
                       at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:368)
                       at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:495)
                       at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
                       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                       at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
                       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                       at org.jboss.seam.web.HotDeployFilter.doFilter(HotDeployFilter.java:53)
                       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                       at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
                       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
                       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
                       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
                       at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
                       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
                       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                       at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
                       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
                       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
                       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
                       at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
                       at java.lang.Thread.run(Unknown Source)
                  Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute query
                       at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:629)
                       at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:73)
                       at org.jboss.seam.framework.EntityQuery.initResultList(EntityQuery.java:80)
                       at org.jboss.seam.framework.EntityQuery.getResultList(EntityQuery.java:71)
                       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.jboss.seam.util.Reflections.invoke(Reflections.java:22)
                       at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32)
                       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
                       at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
                       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                       at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:97)
                       at org.jboss.seam.util.Work.workInTransaction(Work.java:47)
                       at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:91)
                       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                       at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
                       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                       at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
                       at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
                       at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
                       at org.domain.user.session.CustomerList_$$_javassist_seam_6.getResultList(CustomerList_$$_javassist_seam_6.java)
                       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 javax.el.BeanELResolver.getValue(BeanELResolver.java:62)
                       at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
                       at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:64)
                       at org.jboss.el.parser.AstPropertySuffix.getValue(AstPropertySuffix.java:53)
                       at org.jboss.el.parser.AstValue.getValue(AstValue.java:67)
                       at org.jboss.el.parser.AstEmpty.getValue(AstEmpty.java:29)
                       at org.jboss.el.parser.AstChoice.getValue(AstChoice.java:27)
                       at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
                       at com.sun.facelets.el.ELText$ELTextVariable.writeText(ELText.java:184)
                       at com.sun.facelets.el.ELText$ELTextComposite.writeText(ELText.java:108)
                       at com.sun.facelets.compiler.TextInstruction.write(TextInstruction.java:45)
                       ... 56 more
                  Caused by: org.hibernate.exception.GenericJDBCException: could not execute query
                       at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
                       at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
                       at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
                       at org.hibernate.loader.Loader.doList(Loader.java:2223)
                       at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
                       at org.hibernate.loader.Loader.list(Loader.java:2099)
                       at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
                       at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
                       at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
                       at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
                       at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
                       at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:64)
                       ... 92 more
                  Caused by: java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 9 to TIMESTAMP.
                       at com.mysql.jdbc.ResultSet.getTimestampFromBytes(ResultSet.java:6965)
                       at com.mysql.jdbc.ResultSet.getTimestampInternal(ResultSet.java:6999)
                       at com.mysql.jdbc.ResultSet.getTimestamp(ResultSet.java:6319)
                       at com.mysql.jdbc.ResultSet.getTimestamp(ResultSet.java:6357)
                       at org.jboss.resource.adapter.jdbc.WrappedResultSet.getTimestamp(WrappedResultSet.java:971)
                       at org.hibernate.type.TimestampType.get(TimestampType.java:30)
                       at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
                       at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
                       at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
                       at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2096)
                       at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1380)
                       at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)
                       at org.hibernate.loader.Loader.getRow(Loader.java:1206)
                       at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
                       at org.hibernate.loader.Loader.doQuery(Loader.java:701)
                       at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
                       at org.hibernate.loader.Loader.doList(Loader.java:2220)
                       ... 100 more

                  Please guide me how to proceed..