3 Replies Latest reply on Nov 6, 2008 9:04 PM by Juston Nullington

    Country by country code

    Juston Nullington Newbie

      NPE when using String for primary key. As with country code (US) being the key.


      Here is sample code.


      @Entity
      @Table(uniqueConstraints = @UniqueConstraint(columnNames = "CountryName"))
      public class Country implements java.io.Serializable
      {
      
           private String                              countryId; // "US"
           private String                              countryName; // "United States"
      
           public Country()
           {
           }
      
           @Id
           @Column(name = "CountryID", unique = true, nullable = false, length = 2)
           public String getCountryId()
           {
                return countryId;
           }
      
           @Column(name = "CountryName", unique = true, nullable = false, length = 50)
           public String getCountryName()
           {
                return countryName;
           }
           public void setCountryId(String countryId)
           {
                this.countryId = countryId;
           }
      
           public void setCountryName(String countryName)
           {
                this.countryName = countryName;
           }
      
      }




      @Entity
      @Table(uniqueConstraints = @UniqueConstraint(columnNames = "UserName"))
      public class User implements java.io.Serializable
      {
           private Integer                                   userId;
           private int                                   version;
           private Country                                   country;
           private String                                   userName;
      
           @Id
           @GeneratedValue(strategy = IDENTITY)
           @Column(name = "UserID", unique = true, nullable = false)
           public Integer getUserId()
           {
                return userId;
           }
      
           @JoinColumn(name = "Country", nullable = false)
           public Country getCountry()
           {
                return country;
           }
           
           @Column(name = "UserName", unique = true, nullable = false)
           public String getUserName()
           {
                return userName;
           }
      
           @Version
           @Column(name = "Version", nullable = false)
           public int getVersion()
           {
                return version;
           }
      
           public void setUserId(Integer userId)
           {
                this.userId = userId;
           }
      
           public void setCountry(Country country)
           {
                this.country = country;
           }
      
           public void setUserName(String userName)
           {
                this.userName = userName;
           }
      
           public void setVersion(int version)
           {
                this.version = version;
           }
      
      }
      



      public class UserHome extends EntityHome<User>
      {
           @Override
           public User find()
           {
                final User user = super.find();
                Country country = user.getCountry(); // <-- NPE
           }
      }
      


      user.getCountry() line throws a NPE


      Also have tried with  FetchType.EAGER


      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "Country", nullable = false)
           public Country getCountry()
           {
                return country;
           }
      



      and it also throws NPE.


      The sql it produces looks good and returns both CountryID and CountryName fine.


       


      select
              user0_.UserID as UserId1_6_1_,
              user0_.Country as Country6_1_,
              user0_.UserName as UserName6_1_,
              user0_.Version as Version6_1_,
              country1_.CountryID as CountryID18_0_,
              country1_.CountryName as CountryN3_18_0_,
          from
              Tbl.User user0_
          inner join
              Tbl.Country country1_
                  on user0_.Country=country1_.CountryID
          where
              user0_.UserID=?




      Any ideas?


      Thank you

        • 1. Re: Country by country code
          Shane Bryzak Master

          This looks like a Hibernate issue to me, you'd be better off asking on the Hibernate forums.

          • 2. Re: Country by country code
            Damian Harvey Apprentice

            Post your stacktrace so we can see if it's a Hibernate error or not.

            • 3. Re: Country by country code
              Juston Nullington Newbie

              Looking st the stacktrace the NPE is from the line is from the next line


              log.info("User Country Name: ", country.getCountryName());


              When changed to
              System.err.println("User Country Name: "+ country.getCountryName());
              there was no more NPE and works fine.



              Is @ManyToOne(fetch = FetchType.EAGER) the only way to get it to work?
              The Country class can have additional date but mostly I will just need Country Code and Name so eagerly pull the whole this is not desirable.


              When running without FetchType.EAGER


              userList = entityManager.createQuery("select o from User o").getResultList(); //SeamFactoryApplication.java:165



              Gives this stacktrace


              INFO  [SerializableType] could not read column value from result set: country6_; could not deserialize
              ERROR [viewhandler] Error Rendering View[/home.xhtml]
              org.jboss.seam.InstantiationException: Could not instantiate Seam component: factorySession
                   at org.jboss.seam.Component.newInstance(Component.java:2096)
                   at org.jboss.seam.Component.getInstance(Component.java:1978)
                   at org.jboss.seam.Component.getInstance(Component.java:1940)
                   at org.jboss.seam.Component.getInstance(Component.java:1934)
                   at org.jboss.seam.Component.getInstanceFromFactory(Component.java:2026)
                   at org.jboss.seam.Component.getInstance(Component.java:1968)
                   at org.jboss.seam.Component.getInstance(Component.java:1940)
                   at org.jboss.seam.Component.getInstance(Component.java:1934)
                   at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:55)
                   at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:50)
                   at org.jboss.seam.el.SeamELResolver.resolveBase(SeamELResolver.java:166)
                   at org.jboss.seam.el.SeamELResolver.getValue(SeamELResolver.java:53)
                   at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
                   at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
                   at org.jboss.el.parser.AstIdentifier.getValue(AstIdentifier.java:44)
                   at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
                   at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
                   at com.sun.facelets.tag.jstl.core.ForEachHandler.apply(ForEachHandler.java:121)
                   at com.sun.facelets.tag.jsf.ComponentHandler.applyNextHandler(ComponentHandler.java:314)
                   at com.sun.facelets.tag.jsf.ComponentHandler.apply(ComponentHandler.java:169)
                   at com.sun.facelets.tag.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:47)
                   at com.sun.facelets.tag.jsf.ComponentHandler.applyNextHandler(ComponentHandler.java:314)
                   at com.sun.facelets.tag.jsf.ComponentHandler.apply(ComponentHandler.java:169)
                   at com.sun.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:119)
                   at com.sun.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:49)
                   at com.sun.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:25)
                   at com.sun.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:248)
                   at com.sun.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:294)
                   at com.sun.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:273)
                   at com.sun.facelets.impl.DefaultFaceletContext.includeFacelet(DefaultFaceletContext.java:140)
                   at com.sun.facelets.tag.ui.IncludeHandler.apply(IncludeHandler.java:65)
                   at com.sun.facelets.tag.jsf.ComponentHandler.applyNextHandler(ComponentHandler.java:314)
                   at com.sun.facelets.tag.jsf.ComponentHandler.apply(ComponentHandler.java:169)
                   at com.sun.facelets.tag.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:47)
                   at com.sun.facelets.tag.jsf.core.ViewHandler.apply(ViewHandler.java:109)
                   at com.sun.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:49)
                   at com.sun.facelets.tag.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:47)
                   at com.sun.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:25)
                   at com.sun.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:248)
                   at com.sun.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:294)
                   at com.sun.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:273)
                   at com.sun.facelets.impl.DefaultFaceletContext.includeFacelet(DefaultFaceletContext.java:140)
                   at com.sun.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:113)
                   at com.sun.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:49)
                   at com.sun.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:25)
                   at com.sun.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:95)
                   at com.sun.facelets.FaceletViewHandler.buildView(FaceletViewHandler.java:524)
                   at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:567)
                   at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
                   at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:196)
                   at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:109)
                   at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
                   at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
                   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
                   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:38)
                   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:177)
                   at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:267)
                   at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:380)
                   at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:507)
                   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:58)
                   at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                   at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:51)
                   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:235)
                   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
                   at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:189)
                   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
                   at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:91)
                   at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:92)
                   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:325)
                   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
                   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
                   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                   at java.lang.Thread.run(Thread.java:619)
              Caused by: org.jboss.seam.InstantiationException: Could not instantiate Seam component: factoryApplication
                   at org.jboss.seam.Component.newInstance(Component.java:2096)
                   at org.jboss.seam.Component.getInstance(Component.java:1978)
                   at org.jboss.seam.Component.getInstance(Component.java:1940)
                   at org.jboss.seam.Component.getInstance(Component.java:1934)
                   at org.jboss.seam.Component.getInstanceInAllNamespaces(Component.java:2301)
                   at org.jboss.seam.Component.getValueToInject(Component.java:2253)
                   at org.jboss.seam.Component.injectAttributes(Component.java:1693)
                   at org.jboss.seam.Component.inject(Component.java:1511)
                   at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:61)
                   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.core.SynchronizationInterceptor.aroundInvoke(SynchronizationInterceptor.java:32)
                   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:166)
                   at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102)
                   at org.project.bean.SeamFactorySession_$$_javassist_1.init(SeamFactorySession_$$_javassist_1.java)
                   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                   at java.lang.reflect.Method.invoke(Method.java:597)
                   at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
                   at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:138)
                   at org.jboss.seam.Component.callComponentMethod(Component.java:2201)
                   at org.jboss.seam.Component.callCreateMethod(Component.java:2124)
                   at org.jboss.seam.Component.newInstance(Component.java:2084)
                   ... 95 more
              Caused by: javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not deserialize
                   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
                   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:76)
                   at org.project.bean.SeamFactoryApplication.initUsers(SeamFactoryApplication.java:165)
                   at org.project.bean.SeamFactoryApplication.init(SeamFactoryApplication.java:138)
                   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                   at java.lang.reflect.Method.invoke(Method.java:597)
                   at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
                   at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
                   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.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77)
                   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:166)
                   at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102)
                   at org.project.bean.SeamFactoryApplication_$$_javassist_3.init(SeamFactoryApplication_$$_javassist_3.java)
                   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                   at java.lang.reflect.Method.invoke(Method.java:597)
                   at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
                   at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:138)
                   at org.jboss.seam.Component.callComponentMethod(Component.java:2201)
                   at org.jboss.seam.Component.callCreateMethod(Component.java:2124)
                   at org.jboss.seam.Component.newInstance(Component.java:2084)
                   ... 121 more
              Caused by: org.hibernate.type.SerializationException: could not deserialize
                   at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:188)
                   at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:211)
                   at org.hibernate.type.SerializableType.fromBytes(SerializableType.java:105)
                   at org.hibernate.type.SerializableType.get(SerializableType.java:62)
                   at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:184)
                   at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:173)
                   at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
                   at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2124)
                   at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
                   at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
                   at org.hibernate.loader.Loader.getRow(Loader.java:1230)
                   at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
                   at org.hibernate.loader.Loader.doQuery(Loader.java:724)
                   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
                   at org.hibernate.loader.Loader.doList(Loader.java:2228)
                   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
                   at org.hibernate.loader.Loader.list(Loader.java:2120)
                   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
                   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
                   at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
                   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
                   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
                   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67)
                   ... 149 more
              Caused by: java.io.EOFException
                   at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2281)
                   at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2750)
                   at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:781)
                   at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
                   at org.hibernate.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:223)
                   at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:180)
                   ... 171 more