部署后 Hibernate 查询 return 空列表
Hibernate query return empty list after deployment
伙计们,我在使用 wildfly 10.1.0.Final、hibernate ogm 5.0.10.Final 和 Cassandra 3.0.9 设置端点时遇到了一些问题。
我的 cassandra 数据库中名为 "IO_CONFIGURATION" 的 table 中有 32 行。
这是我的 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;
}
}
这是我正在尝试的查询 运行:
@SuppressWarnings("unchecked")
public List<IOConfiguration> findAllIOConfiguration() {
Query query = this.getManager().createQuery("from IOConfiguration");
return query.getResultList();
}
我的查询 运行 作为独立的 java 应用程序非常完美。这是我为测试查询而编写的代码:
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();
}
}
在运行这个测试之后,它会给我一个结果:
***************** 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();
}
}
但是当我转到localhost:8080/aigateway/rest/ioconfiguration时,它会给我一个空列表。
这是我的 persistence.xml 设置连接:
<?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>
不知道是什么问题,求大神指教!
伙计们
我使用 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。
伙计们,我在使用 wildfly 10.1.0.Final、hibernate ogm 5.0.10.Final 和 Cassandra 3.0.9 设置端点时遇到了一些问题。
我的 cassandra 数据库中名为 "IO_CONFIGURATION" 的 table 中有 32 行。
这是我的 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;
}
}
这是我正在尝试的查询 运行:
@SuppressWarnings("unchecked")
public List<IOConfiguration> findAllIOConfiguration() {
Query query = this.getManager().createQuery("from IOConfiguration");
return query.getResultList();
}
我的查询 运行 作为独立的 java 应用程序非常完美。这是我为测试查询而编写的代码:
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();
}
}
在运行这个测试之后,它会给我一个结果:
***************** 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();
}
}
但是当我转到localhost:8080/aigateway/rest/ioconfiguration时,它会给我一个空列表。
这是我的 persistence.xml 设置连接:
<?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>
不知道是什么问题,求大神指教!
伙计们
我使用 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。