0

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:

@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!

LebroNan
  • 149
  • 7
  • 18
  • You can set hibernate log to Trace and check exactly which SQL is getting fired on database. – Atul Mar 07 '17 at 19:40
  • All other services I've written for my DAO work perfect. So I suppose that there is no problem about the connection between my database and wildfly server. But only the queries don't work, I don't know why. – LebroNan Mar 07 '17 at 21:17
  • Possible duplicate of [createQuery not working but createNativeQuery works](http://stackoverflow.com/questions/42658592/createquery-not-working-but-createnativequery-works) – Naros Mar 07 '17 at 21:56
  • Oh yeah, that's another question posted by me. I kinda solve this problem using native query. But it is very strange I wanna know why. – LebroNan Mar 07 '17 at 22:36

1 Answers1

0

guys

I solved this problem using createNativeQuery. I am not able to do query using createQuery in HQL.

Here is the hibernate cassandra websites where I got this idea: https://docs.jboss.org/hibernate/ogm/5.0/reference/en-US/html/ch14.html#ogm-cassandra

As they mention in 14.4: 14.4. Native queries

Native queries are supported: you can execute native CQL queries using the EntityManager infrastructure.

Currently, only ordinal parameters are supported, named parameters do not work.

Example 14.1. Using native CQL queries

Query query = em.createNativeQuery( "SELECT * FROM \"WILDE_POEM\" WHERE name = ?" ); query.setParameter( 1, "Portia" ); // CQL parameters positions start at 1 List results = query.getResultList();

I still don't know what is wrong using createQuery. I just assume that cassandra doesn't support HQL yet.

LebroNan
  • 149
  • 7
  • 18
  • I've answered to this question here: http://stackoverflow.com/questions/42658592/createquery-not-working-but-createnativequery-works – Davide D'Alto Mar 20 '17 at 16:30