5 Replies Latest reply on Dec 31, 2012 6:52 AM by fabio valsecchi

    How to persist an HashMap?

    fabio valsecchi Newbie

      Hi there,

      I've a problem in my application regarding an Entity called Cart, this is the code:

       

      ----------------------------------------------------------------

      package com.ibytecode.entities;

       

      import java.io.Serializable;

      import javax.persistence.Entity;

      import javax.persistence.Id;

      import javax.persistence.Table;

      import java.util.Map;

      import javax.persistence.OneToMany;

      import javax.persistence.CascadeType;

      import javax.persistence.ElementCollection;

      import javax.persistence.FetchType;

       

      @Entity

      @Table(name = "carts")

      public class Cart implements Serializable {

      private static final long serialVersionUID = 3L;

       

                public Cart() {

              super();

          }

       

      @Id

          private String username;

      @ElementCollection

                private Map<Book,Integer> articles;

      private float bill;

       

                public String getUsername() {

        return username;

                }

                public void setUsername(String username) {

                          this.username = username;

                }

                @OneToMany(cascade={CascadeType.ALL},fetch=FetchType.EAGER)

                public Map<Book,Integer> getArticles() {

        return articles;

                }

                public void setArticles(Map<Book,Integer> articles) {

                          this.articles = articles;

                }

                public float getBill() {

        return bill;

                }

                public void setBill(float bill) {

                          this.bill = bill;

                }

      }

      ----------------------------------------------------------------

       

      And this is the code of the CartBean:

       

      ----------------------------------------------------------------

      package com.ibytecode.businesslogic;

       

      import java.util.HashMap;

      import com.ibytecode.entities.Book;

      import com.ibytecode.business.ICart;

      import com.ibytecode.entities.Cart;

      import javax.ejb.Remote;

      import javax.ejb.Stateful;

      import javax.persistence.EntityManager;

      import javax.persistence.PersistenceContext;

      import javax.persistence.PersistenceContextType;

       

      @Stateful

      @Remote(ICart.class)

      public class CartBean implements ICart {

                @PersistenceContext(unitName = "JPABOOK", type=PersistenceContextType.EXTENDED)

          EntityManager entityManager;

                private Cart cart = null;

       

          public CartBean() {}

         

          @Override

          public void create(String username) {

                    cart = entityManager.find(Cart.class, username);

          

              if (cart == null) {

                        cart = new Cart();

                        cart.setUsername(username);

              //cart.setArticles(new HashMap<Book,Integer>());

                        cart.setBill(0);

                        entityManager.persist(cart);

              }

          }

         

          public void addArticle(Book book, Integer quantity) {

                    Integer currentQuantity = cart.getArticles().get(book);

              if (currentQuantity == null) {

                        currentQuantity = 0;

              }

              currentQuantity += quantity;

              cart.getArticles().put(book, currentQuantity);

          }

      }

      ----------------------------------------------------------------

       

      In the database i've create a table carts in this way: CREATE TABLE carts (username VARCHAR(30), bill FLOAT, PRIMARY KEY (username));

      Do you know if in this case the table carts must have a field articles? If yes, what type I can assign to it?

       

      However the problem is that i got this error:

       

      java.lang.RuntimeException: ClassNotFoundException marshaling EJB parameters

      org.jboss.as.ejb3.remote.LocalEjbReceiver.clone(LocalEjbReceiver.java:229)

      org.jboss.as.ejb3.remote.LocalEjbReceiver.processInvocation(LocalEjbReceiver.java:183)

      org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:179)

      org.jboss.ejb.client.TransactionInterceptor.handleInvocation(TransactionInterceptor.java:43)

      org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181)

      org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:128)

      org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181)

      org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:136)

      org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:121)

      org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:104)

      $Proxy47.addArticle(Unknown Source)

      com.ibytecode.servlets.CartController.addToCart(CartController.java:74)

      com.ibytecode.servlets.CartController.doGet(CartController.java:26)

      javax.servlet.http.HttpServlet.service(HttpServlet.java:734)

      javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

       

      The eclipse console give this output:

       

      20:54:51,830 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-1) SQL Error: 1146, SQLState: 42S02

      20:54:51,831 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-1) Table 'books.cart_articles' doesn't exist

      20:54:51,831 INFO  [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (http-localhost-127.0.0.1-8080-1) HHH000106: Forcing container resource cleanup on transaction completion

      20:54:51,832 INFO  [org.jboss.as.ejb3] (http-localhost-127.0.0.1-8080-1) JBAS014101: Failed to find SFSB instance with session ID {[125, -43, -68, 124, -20, -33, 68, -37, -93, -56, 126, 20, 91, -9, 125, 112]} in cache

      20:54:51,833 ERROR [org.jboss.ejb3.invocation] (http-localhost-127.0.0.1-8080-1) JBAS014134: EJB Invocation failed on component CartBean for method public abstract void com.ibytecode.business.ICart.addArticle(com.ibytecode.entities.Book,java.lang.Integer): javax.ejb.EJBException: org.hibernate.exception.SQLGrammarException: Table 'books.cart_articles' doesn't exist

                at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:166) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:230) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:304) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.as.ejb3.remote.EJBRemoteTransactionPropagatingInterceptor.processInvocation(EJBRemoteTransactionPropagatingInterceptor.java:80) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:32) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:165) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.as.ejb3.remote.LocalEjbReceiver.processInvocation(LocalEjbReceiver.java:179) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:179) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at org.jboss.ejb.client.TransactionInterceptor.handleInvocation(TransactionInterceptor.java:43) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:128) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:136) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:121) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:104) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at $Proxy47.addArticle(Unknown Source)          at com.ibytecode.servlets.CartController.addToCart(CartController.java:74)

                at com.ibytecode.servlets.CartController.doGet(CartController.java:26)

                at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]

                at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)

                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)

                at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)

                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)

                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)

                at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

                at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)

                at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)

                at java.lang.Thread.run(Thread.java:680) [classes.jar:1.6.0_37]

      Caused by: org.hibernate.exception.SQLGrammarException: Table 'books.cart_articles' doesn't exist

                at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at $Proxy19.executeQuery(Unknown Source)          at org.hibernate.loader.Loader.getResultSet(Loader.java:1978) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.loader.Loader.doQuery(Loader.java:829) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.loader.Loader.loadCollection(Loader.java:2175) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:61) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:622) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:82) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1606) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:379) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:112) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.collection.internal.AbstractPersistentCollection.readElementByIndex(AbstractPersistentCollection.java:189) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.collection.internal.PersistentMap.get(PersistentMap.java:165) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at com.ibytecode.businesslogic.CartBean.addArticle(CartBean.java:36) [bookshop.jar:]

                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [classes.jar:1.6.0_37]

                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [classes.jar:1.6.0_37]

                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [classes.jar:1.6.0_37]

                at java.lang.reflect.Method.invoke(Method.java:597) [classes.jar:1.6.0_37]

                at org.jboss.as.ee.component.ManagedReferenceMethodInterceptorFactory$ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptorFactory.java:72) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:36) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.as.jpa.interceptor.SFSBInvocationInterceptor.processInvocation(SFSBInvocationInterceptor.java:58) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.as.ejb3.component.stateful.StatefulSessionSynchronizationInterceptor.processInvocation(StatefulSessionSynchronizationInterceptor.java:156) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:53) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.as.ejb3.component.stateful.StatefulComponentInstanceInterceptor.processInvocation(StatefulComponentInstanceInterceptor.java:66) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:228) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                ... 44 more

      Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'books.cart_articles' doesn't exist

                at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [classes.jar:1.6.0_37]

                at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) [classes.jar:1.6.0_37]

                at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) [classes.jar:1.6.0_37]

                at java.lang.reflect.Constructor.newInstance(Constructor.java:513) [classes.jar:1.6.0_37]

                at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)

                at com.mysql.jdbc.Util.getInstance(Util.java:386)

                at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)

                at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)

                at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)

                at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)

                at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)

                at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)

                at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)

                at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)

                at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:462)

                at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source) [:1.6.0_37]

                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [classes.jar:1.6.0_37]

                at java.lang.reflect.Method.invoke(Method.java:597) [classes.jar:1.6.0_37]

                at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                ... 83 more

       

       

      20:54:51,864 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/bookshopwebsite].[com.ibytecode.servlets.CartController]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet com.ibytecode.servlets.CartController threw exception: java.lang.RuntimeException: ClassNotFoundException marshaling EJB parameters

                at org.jboss.as.ejb3.remote.LocalEjbReceiver.clone(LocalEjbReceiver.java:229) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.as.ejb3.remote.LocalEjbReceiver.processInvocation(LocalEjbReceiver.java:183) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:179) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at org.jboss.ejb.client.TransactionInterceptor.handleInvocation(TransactionInterceptor.java:43) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:128) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:136) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:121) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:104) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                at $Proxy47.addArticle(Unknown Source)          at com.ibytecode.servlets.CartController.addToCart(CartController.java:74) [classes:]

                at com.ibytecode.servlets.CartController.doGet(CartController.java:26) [classes:]

                at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]

                at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]

                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]

                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]

                at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]

                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]

                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]

                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]

                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]

                at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]

                at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]

                at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]

                at java.lang.Thread.run(Thread.java:680) [classes.jar:1.6.0_37]

      Caused by: java.lang.ClassNotFoundException: org.hibernate.exception.SQLGrammarException from [Module "deployment.bookshopwebsite.war:main" from Service Module Loader]

                at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)

                at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:468)

                at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:456)

                at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:423)

                at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)

                at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:120)

                at java.lang.Class.forName0(Native Method) [classes.jar:1.6.0_37]

                at java.lang.Class.forName(Class.java:247) [classes.jar:1.6.0_37]

                at org.jboss.marshalling.cloner.ClassLoaderClassCloner.clone(ClassLoaderClassCloner.java:46)

                at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:158)

                at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:135)

                at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:187)

                at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:135)

                at org.jboss.marshalling.cloner.SerializingCloner.cloneFields(SerializingCloner.java:348)

                at org.jboss.marshalling.cloner.SerializingCloner.initSerializableClone(SerializingCloner.java:304)

                at org.jboss.marshalling.cloner.SerializingCloner.initSerializableClone(SerializingCloner.java:286)

                at org.jboss.marshalling.cloner.SerializingCloner.initSerializableClone(SerializingCloner.java:286)

                at org.jboss.marshalling.cloner.SerializingCloner.initSerializableClone(SerializingCloner.java:286)

                at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:254)

                at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:135)

                at org.jboss.as.ejb3.remote.LocalEjbReceiver.clone(LocalEjbReceiver.java:225) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                ... 27 more

       

       

      Can someone give me a hint about that?

      Thank you in advance

      Fabio

        • 1. Re: How to persist an HashMap?
          Frank Langelage Master

          You cannot store multiple entries in one field.

          So you have to create a dependent table / sub table. See the cause for the failure: Table 'books.cart_articles' doesn't exist.

           

          Either use OneToMany or ElementCollection and don't mix using annotations on fields and methods.

           

          The are many articles to find about jpa2 + ElementCollection + HashMap.

          1 of 1 people found this helpful
          • 2. Re: How to persist an HashMap?
            Nicklas Karlsson Master

            There is also the java.lang.ClassNotFoundException: org.hibernate.exception.SQLGrammarException that might indicate another problem once you get past the mapping thingies.

            • 3. Re: How to persist an HashMap?
              fabio valsecchi Newbie

              Looking on this site in the section "Additional Map Support in JPA2" I edit my code in this way:

               

              ------------------------------------------------------------------

              @Entity

              @Table(name = "carts")

              public class Cart implements Serializable {

              private static final long serialVersionUID = 3L;

               

                        public Cart() {

                      super();

                  }

               

              @Id

                  private String username;

                        @OneToMany(cascade = CascadeType.ALL, mappedBy = "username")

                        @MapKeyJoinColumn(name="isbn", insertable=false, updatable=false)

                  private Map<Book,Integer> articles;

              private float bill;

               

              ......

               

              ------------------------------------------------------------------

               

              I changed the annotation inserting @OneToMany and @MapKeyJoinColumn. In the first I specified the username field that is the ID of Cart. In the second one isbn is the name of the ID of Book.

              The problem is that now with these annotations, JBoss cannot deploy my app!

              What's wrong?

              • 4. Re: How to persist an HashMap?
                Nicklas Karlsson Master

                The problem is not JBoss being unable to your app, if the mappings/queries are wrong, it won't deploy on any AS.

                • 5. Re: How to persist an HashMap?
                  fabio valsecchi Newbie

                  I solve the problem! I post the solution:

                   

                  ----------------------------------------------

                  .....

                  @Id

                      private String username;

                  @ElementCollection

                      @CollectionTable(name="cartsbooks", joinColumns=@JoinColumn(name="username"))

                      @Column(name="quantity")

                      @MapKeyJoinColumn(name="isbn", referencedColumnName="isbn")

                      private Map<Book,Integer> articles;

                  .....

                  ----------------------------------------------

                   

                  I found the solution here.