1 Reply Latest reply on Mar 17, 2017 9:57 AM by Lebron James

    Hibernate query return empty list after deployment.

    Lebron James Newbie

      Hi, guys, I have some problems setting up my endpoints in my project.I am using using wildfly 10.1.0.Final, hibernate ogm 5.0.10.Final and Cassandra 3.0.9.

       

      I have 32 rows in a table named "IO_CONFIGURATION" in my cassandra database.

       

      Here is the code for my DAO:

      public interface DataAccessObject {
        public EntityManager getManager();
      
        public void setManager(EntityManager em);
      
        public EntityManagerFactory getEntityManagerFactory();
      
        public void setEntityManagerFactory(EntityManagerFactory emf);
      }
      

       

      public abstract class AbstractDAO implements DataAccessObject {
        public <T> T get(Class<T> clazz, Object id) {
          return getManager().find(clazz, id);
        }
      
        public <T> T update(T entity) {
          return getManager().merge(entity);
        }
      
        public <T> T create(T entity) {
          getManager().persist(entity);
          return entity;
        }
      
        public <T> void delete(T entity) {
          getManager().remove(entity);
        }
      
        @SuppressWarnings("unchecked")
        protected <T> T executeQueryForSingleResult(Query query) {
          try {
            return (T) query.getSingleResult();
          } catch (NoResultException e) {
            return null;
          }
        }
      }
      

       

      @Singleton
      @Startup
      public class CassandraDAO<T extends Serializable> extends AbstractDAO {
      
        private EntityManagerFactory emf;
        // @PersistenceContext(unitName = "JPAService")
        private EntityManager em;
      
        @PostConstruct
        public void init() {
          System.out.println("***************** init CassandraDAO ***************************");
          emf = Persistence.createEntityManagerFactory("JPAService");
          em = emf.createEntityManager();
        }
      
        @PreDestroy
        public void destroy() {
          System.out.println("***************** destroying CassandraDAO *************************");
          if (em.isOpen()) {
            em.close();
          }
          if (emf.isOpen()) {
            emf.close();
          }
        }
      
        @Override
        public EntityManager getManager() {
          return em;
        }
      
        @Override
        public void setManager(EntityManager em) {
          this.em = em;
        }
      
        @Override
        public EntityManagerFactory getEntityManagerFactory() {
          return emf;
        }
      
        @Override
        public void setEntityManagerFactory(EntityManagerFactory emf) {
          this.emf = emf;
        }
      }
      

       

      Here is the query that I am trying to run:

      @SuppressWarnings("unchecked")
        public List<IOConfiguration> findAllIOConfiguration() {
          Query query = this.getManager().createQuery("from IOConfiguration");
          return query.getResultList();
        }
      

       

      My query runs perfect as a standalone java application. Here is the code I wrote for test the query:

      public class DatabaseConnectionTest {
        @EJB
        private static IOConfigurationDAO ioConfigurationDAO;
      
        public static void main(String[] args) {
          DatabaseConnectionTest dbTest = new DatabaseConnectionTest();
          dbTest.test();
        }
      
        public void test() {
          ioConfigurationDAO = new IOConfigurationDAO();
          ioConfigurationDAO.init();
          List<IOConfiguration> list = ioConfigurationDAO.findAllIOConfiguration();
          System.out.println(list.size());
          ioConfigurationDAO.destroy();
        }
      }
      

      And after run this test, it will give me a result:

      ***************** init CassandraDAO ***************************
      Mar 06, 2017 5:53:39 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
      INFO: HHH000204: Processing PersistenceUnitInfo [
          name: JPAService
          ...]
      Mar 06, 2017 5:53:39 PM org.hibernate.Version logVersion
      INFO: HHH000412: Hibernate Core {5.0.9.Final}
      Mar 06, 2017 5:53:39 PM org.hibernate.cfg.Environment <clinit>
      INFO: HHH000206: hibernate.properties not found
      Mar 06, 2017 5:53:39 PM org.hibernate.cfg.Environment buildBytecodeProvider
      INFO: HHH000021: Bytecode provider name : javassist
      Mar 06, 2017 5:53:39 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
      INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
      Mar 06, 2017 5:53:39 PM org.hibernate.ogm.datastore.impl.DatastoreProviderInitiator initiateService
      INFO: OGM000016: NoSQL Datastore provider: org.hibernate.ogm.datastore.cassandra.impl.CassandraDatastoreProvider
      Mar 06, 2017 5:53:39 PM org.hibernate.ogm.datastore.cassandra.impl.CassandraDatastoreProvider start
      INFO: OGM001601: Connecting to Cassandra at 127.0.0.1:9042
      SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
      SLF4J: Defaulting to no-operation (NOP) logger implementation
      SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
      Mar 06, 2017 5:53:40 PM org.hibernate.ogm.dialect.impl.GridDialectInitiator$GridDialectInstantiator newInstance
      INFO: OGM000017: Grid Dialect: org.hibernate.ogm.datastore.cassandra.CassandraDialect
      Mar 06, 2017 5:53:40 PM org.hibernate.ogm.dialect.impl.GridDialectInitiator$GridDialectInstantiator newInstance
      INFO: Grid dialect logs are disabled
      Mar 06, 2017 5:53:40 PM org.hibernate.dialect.Dialect <init>
      INFO: HHH000400: Using dialect: org.hibernate.ogm.dialect.impl.OgmDialect
      Mar 06, 2017 5:53:40 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl makeLobCreatorBuilder
      INFO: HHH000422: Disabling contextual LOB creation as connection was null
      Mar 06, 2017 5:53:40 PM org.hibernate.id.UUIDHexGenerator <init>
      WARN: HHH000409: Using org.hibernate.id.UUIDHexGenerator which does not generate IETF RFC 4122 compliant UUID values; consider using org.hibernate.id.UUIDGenerator instead
      Mar 06, 2017 5:53:40 PM org.hibernate.boot.internal.SessionFactoryBuilderImpl$SessionFactoryOptionsStateStandardImpl <init>
      WARN: Unrecognized hbm2ddl_auto value : off.  Supported values include create, create-drop, update, and validate.  Ignoring
      Mar 06, 2017 5:53:40 PM org.hibernate.search.engine.Version <clinit>
      INFO: HSEARCH000034: Hibernate Search 5.5.3.Final
      Mar 06, 2017 5:53:40 PM org.hibernate.ogm.cfg.impl.Version <clinit>
      INFO: OGM000001: Hibernate OGM 5.0.4.Final
      Mar 06, 2017 5:53:40 PM org.hibernate.search.engine.impl.ConfigContext getLuceneMatchVersion
      WARN: HSEARCH000075: Configuration setting hibernate.search.lucene_version was not specified: using LUCENE_CURRENT.
      32
      ***************** destroying CassandraDAO *************************
      Mar 06, 2017 5:53:41 PM org.hibernate.ogm.datastore.cassandra.impl.CassandraDatastoreProvider stop
      INFO: OGM001602: Closing connection to Cassandra
      

       

      But when I try to do the same call on my endpoints after deploy my project, it will just give me an empty list.

      Here is the code for the endpoints I set up:

      @Path("/ioconfiguration")
      public class IOConfigurationEndPoint {
        @EJB
        private static IOConfigurationDAO ioConfigurationDAO;
        @GET
        @Produces(MediaType.APPLICATION_JSON)
        public Response getAllIoConfigurations() {
          ioConfigurationDAO = new IOConfigurationDAO();
          ioConfigurationDAO.init();
          List<IOConfiguration> list = ioConfigurationDAO.findAllIOConfiguration();
          System.out.println(list);
          ioConfigurationDAO.destroy();
          return Response.status(Response.Status.OK).entity(list).build();
        }
      }
      

      But when I go to localhost:8080/aigateway/rest/ioconfiguration, it will give me an empty list.

       

      Here is my persistence.xml for setting up the connection:

      <?xml version="1.0"?>
      <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
                   version="2.0">
      
          <!--persistence-unit name="ogm-jpa-tutorial" transaction-type="JTA"-->
          <persistence-unit name="JPAService">
              <!-- Use the Hibernate OGM provider: configuration will be transparent -->
              <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
      <!--         <jta-data-source>java:jboss/datasources/cassandraDS</jta-data-source> -->
              <class>com.sensorhound.aigateway.domain.Alert</class>
              <class>com.sensorhound.aigateway.domain.DataSeriesMeta</class>
              <class>com.sensorhound.aigateway.domain.IOConfiguration</class>
              <class>com.sensorhound.aigateway.domain.NodeData</class>
              <properties>
                  <property name="hibernate.transaction.jta.platform" value="JBossAS" />
                  <property name="hibernate.hbm2ddl.auto" value="off"/>
                  <property name="jboss.as.jpa.providerModule" value="org.hibernate:5.0" />
                  <property name="hibernate.ogm.datastore.provider" value="cassandra_experimental"/>
                  <property name="hibernate.ogm.datastore.host" value="127.0.0.1:9042"/>
                  <property name="hibernate.ogm.datastore.database" value="dev"/>
              </properties>
          </persistence-unit>
      </persistence>
      

       

      I don't know what the problem is, someone please help me!