8 Replies Latest reply on Sep 25, 2007 9:34 AM by B Y

    I cannot get this test through, please help

    B Y Newbie

      I creat a project from seam-gen2.0 CR. I am using Jboss server 4.2.1, Seam2.0 CR, MySQL 5.0. The project can be built, depoyed and run correctly. Then I create a simple test case, but I cannot make it works.

      I tested that entity factory can be created; entity manager can also be created. But I cannot do database operation, including select and insert. Please help!

      Test code:

      package persistentTier;
      import java.util.List;
      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.EntityManagerFactory;
      import javax.persistence.Persistence;
      import javax.persistence.PersistenceContext;
      import javax.persistence.Query;
      import org.hibernate.Session;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.mock.SeamTest;
      import org.testng.annotations.Test;
      import org.tt.user.User;
      public class EntityManagerTest extends SeamTest {
       private EntityManager em;
       public void unitTestEntityManager() throws Exception{
       EntityManagerFactory emf = Persistence.createEntityManagerFactory("tt");
       em =emf.createEntityManager();
       User user=em.find(User.class, new Double(1));
       assert user.getPassword().equals("5527686");

      User class:
      package org.tt.user;
      import java.io.Serializable;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.Table;
      import org.hibernate.validator.NotNull;
      import org.jboss.seam.annotations.Name;
      @Name ("user")
      @Table (name="user")
      public class User implements Serializable {
       private double id;
       private String username;
       private String password;
       public User (){
       @Id @GeneratedValue(strategy =GenerationType.AUTO)
       public double getId () {return this.id;}
       public void setId(long id){this.id=id;}
       public String getUsername(){return this.username;}
       public void setUsername(String username){this.username=username;}
       public String getPassword(){return this.password;}
       public void setPassword(String password){this.password=password;}

      Database, MySQL 5.0
      CREATE TABLE `user` (
       `id` double NOT NULL auto_increment,
       `username` varchar(50) NOT NULL,
       `password` varchar(50) NOT NULL,
       PRIMARY KEY (`id`)
      /*Data for the table `user` */
      insert into `user`(`id`,`username`,`password`) values (1,'oldreaper','5527686');

      Erro message
      javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not load an entity: [org.tt.user.User#1.0]
       at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:647)
       at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:194)
       at persistentTier.EntityManagerTest.unitTestEntityManager(EntityManagerTest.java:29)
      Caused by: org.hibernate.exception.SQLGrammarException: could not load an entity: [org.tt.user.User#1.0]
       at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
       at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
       at org.hibernate.loader.Loader.loadEntity(Loader.java:1865)
       at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
       at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
       at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2992)
       at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)
       at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
       at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
       at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
       at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
       at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
       at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
       at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
       at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:174)
       ... 23 more
      Caused by: java.sql.SQLException: Table not found in statement [select user0_.id as id2_0_, user0_.username as username2_0_, user0_.password as password2_0_ from user user0_ where user0_.id=?]
       at org.hsqldb.jdbc.Util.throwError(Unknown Source)
       at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
       at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
       at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.doPrepareStatement(BaseWrapperManagedConnection.java:386)
       at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.prepareStatement(BaseWrapperManagedConnection.java:374)
       at org.jboss.resource.adapter.jdbc.WrappedConnection.prepareStatement(WrappedConnection.java:187)
       at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:497)
       at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:415)
       at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:139)
       at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1538)
       at org.hibernate.loader.Loader.doQuery(Loader.java:661)
       at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
       at org.hibernate.loader.Loader.loadEntity(Loader.java:1851)
       ... 35 more
      ... Removed 22 stack frames

        • 1. Re: I cannot get this test through, please help
          Matt Drees Master

          Seam gen tests run off an in-memory hsql database, not your mysql database. So, if in your persistence-test.xml you don't have something like

          <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

          , no tables will created for your test. I'm guessing you're missing that. If you're not, post what you have instead.

          • 2. Re: I cannot get this test through, please help
            B Y Newbie

            The problems are that:

            1)I don't want to use HSQLDB
            2)I don't want the test drop and create tables for me for every test.

            So, can I configure the datasource and persistence files that allow me to use MySQL with existing data? if I can, please let me know how can I do it?


            • 3. Re: I cannot get this test through, please help
              Matt Drees Master

              Yes. Edit persistence-test.xml.

              • 4. Re: I cannot get this test through, please help
                B Y Newbie

                I tried for serveral time, it report connectiong errors when I change the test database to MySQL. What I did is copy the persistence-dev.xml and edit it for a little. However, it seems does not work correctly.

                can you provide more details about how to do this? or suggest an example?

                • 5. Re: I cannot get this test through, please help
                  Pete Muir Master

                  Have you made sure the datasource is available to jboss embedded?

                  • 6. Re: I cannot get this test through, please help
                    Saeed Iqbal Novice

                    seam-gen for me has been buggy so i use this now http://chiralsoftware.com/master-sauce/ Master Sauce

                    for mysql connection in the resources folder in the **ds.xml i have this

                    <?xml version="1.0" encoding="UTF-8"?>
                    <!DOCTYPE datasources
                     PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"

                    and in META-INF/persistence*.xml i have this

                    <?xml version="1.0" encoding="UTF-8"?>
                    <!-- Persistence deployment descriptor for dev profile -->
                    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
                     <persistence-unit name="sdeskone">
                     <property name="hibernate.hbm2ddl.auto" value="validate"/>
                     <property name="hibernate.cache.use_query_cache" value="true"/>
                     <property name="hibernate.show_sql" value="true"/>
                     <property name="jboss.entity.manager.factory.jndi.name" value="java:/sdeskoneEntityManagerFactory"/>

                    seems to do the work but really seam-gen wasted "days/weeks" of my time.

                    • 7. Re: I cannot get this test through, please help
                      B Y Newbie

                      Thank you for your replies.

                      First, my datasourse should works. I can connect it through eclipse database perspective. I can also compile and deploye the EAR to Jboss. I can see Jboss load the datasource.

                      Second the, the configure files seems correct. I check them also.

                      I guess the Seam test defualtly use some configure parameters that I don't know.

                      There are three persistence files under META-INF folder. They are persistence-dev.xml, -prod.xml and -test.xml. Each file should be related to a datasource file.

                      However, under the resources folder, there is no datasource related to persistence-test.xml. There are only two files. One is project-dev-ds.xml and another is project-prod-ds.xml

                      So, I think the project created from Seam-gen defaultly use some build-in configurartion to connect datasource. Is there any way that I can make the test take in the test datasource file?

                      Thank you

                      • 8. Re: I cannot get this test through, please help
                        B Y Newbie

                        I found the test defaultly use datasource files under the folder project/bootstrap/deploy. I try to put mysql datasource file to this fold. But I get an error when the test is loaded:

                        org.jboss.deployers.spi.IncompleteDeploymentException: Summary of incomplete deployments (SEE PREVIOUS ERRORS FOR DETAILS):
                        *** DEPLOYMENTS IN ERROR: Name -> Error
                        vfsfile:/C:/workspace/tt/bootstrap/deploy/test-ds.xml -> java.lang.IllegalStateException: jboss.jca:name=DefaultDS,service=ManagedConnectionFactory is already installed.
                         at org.jboss.embedded.Bootstrap.checkIncomplete(Bootstrap.java:144)
                         at org.jboss.embedded.Bootstrap.bootstrapURL(Bootstrap.java:169)
                         at org.jboss.embedded.Bootstrap.bootstrap(Bootstrap.java:201)
                         at org.jboss.embedded.Bootstrap.bootstrap(Bootstrap.java:214)
                         at org.jboss.seam.mock.EmbeddedBootstrap.startAndDeployResources(EmbeddedBootstrap.java:11)
                         at org.jboss.seam.mock.BaseSeamTest.startJbossEmbeddedIfNecessary(BaseSeamTest.java:968)
                         at org.jboss.seam.mock.BaseSeamTest.init(BaseSeamTest.java:893)
                         at org.jboss.seam.mock.SeamTest.init(SeamTest.java:42)
                        ... Removed 22 stack frames