部署后Hibernate查询返回空列表

时间:2017-03-07 18:59:29

标签: hibernate jdbc hql wildfly-10 persistence.xml

伙计们,我在项目中设置端点时遇到一些问题。我使用的是wildfly 10.1.0.Final,hibernate ogm 5.0.10.Final和Cassandra 3.0.9。

我的cassandra数据库中名为“IO_CONFIGURATION”的表中有32行。

以下是我DAO的代码:

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

以下是我尝试运行的查询:

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();  
  }  
}  

我的查询作为独立的Java应用程序运行完美。这是我为测试查询而编写的代码:

***************** 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  

运行此测试后,它会给我一个结果:

@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();  
  }  
}  

但是当我在部署我的项目后尝试在我的端点上执行相同的调用时,它只会给我一个空列表。

以下是我设置的端点的代码:

<?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>  

但是当我去localhost:8080 / aigateway / rest / ioconfiguration时,它会给我一个空列表。

这是我用于设置连接的persistence.xml:

FleetType

我不知道问题是什么,有人请帮助我!

1 个答案:

答案 0 :(得分:0)

我使用 createNativeQuery 解决了这个问题。我无法在HQL中使用createQuery进行查询。

这是我得到这个想法的hibernate cassandra网站: https://docs.jboss.org/hibernate/ogm/5.0/reference/en-US/html/ch14.html#ogm-cassandra

他们在14.4中提到: 14.4。原生查询

支持本机查询:您可以使用EntityManager基础结构执行本机CQL查询。

目前,仅支持序数参数,命名参数不起作用。

例14.1。使用本机CQL查询

查询query = em.createNativeQuery(“SELECT * FROM \”WILDE_POEM \“WHERE name =?”); query.setParameter(1,“Portia”); // CQL参数位置从1开始 列表结果= query.getResultList();

我仍然不知道使用 createQuery 有什么问题。我只是假设cassandra还不支持HQL。