伙计,我有一个非常奇怪的问题。
我在我的应用程序中设置了一些端点,我有一个这样的端点:
@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();
ioConfigurationDAO.destroy();
return Response.status(Response.Status.OK).entity(list).build();
}
}
我的想法是我需要获取所有信息&#34; IO Configuration&#34;从表中,我在一个名为&#34; IO_CONFIGURATION&#34;的表中有32行,这个实体的pojo是这样的:
@Entity
@Indexed
@Table(name = "IO_CONFIGURATION",
indexes = {@Index(columnList = "CHANNEL_NAME", name = "CHANNEL_NAME")})
public class IOConfiguration implements Serializable {
private static final long serialVersionUID = 7542743172221933818L;
@Id
@GenericGenerator(name = "IOConfiguration", strategy = "uuid")
@GeneratedValue(generator = "IOConfiguration")
@Column(name = "IO_CONFIGURATION_ID")
private String ioConfigurationId;
@Field(analyze = Analyze.NO)
@Column(name = "CHANNEL_NAME")
private String channelName;
@Column(name = "NAME")
private String name;
@Column(name = "CONVERTION_TYPE")
private String conversionType;
@Column(name = "M_INFO")
private Double mInfo;
@Column(name = "B_INFO")
private Double bInfo;
@Column(name = "VOLTAGE_DIVIDE")
private String voltageDivide;
@Column(name = "SAMPLE_RANGE")
private String sampleRange;
@Column(name = "SAMPEL_PERIOD")
private Integer samplePeriod;
@Column(name = "STORE_ROW")
private Boolean storeRow;
@Column(name = "STORE_CONVERTED")
private Boolean storeConverted;
@Column(name = "DEFAULT_GRAPH")
private String defaultGraph;
@Column(name = "TITLE")
private String title;
@Column(name = "UNITS")
private String units;
@Column(name = "RANGE_LOWERBOUND")
private Integer rangeLowerbound;
@Column(name = "RANGE_UPPERBOUND")
private Integer rangeUpperbound;
@OneToMany(mappedBy = "ioConfiguration", fetch = FetchType.EAGER)
private List<Alert> alerts;
@OneToMany(mappedBy = "ioConfiguration", fetch = FetchType.EAGER)
private List<DataSeriesMeta> dataSeriesMeta;
@OneToMany(mappedBy = "ioConfiguration", fetch = FetchType.LAZY)
private List<NodeData> nodeData;
@Column(name = "CODE")
private String code;
@Column(name = "ACTIVE")
private Boolean active;
...
}
以下是我插入行的方法:
private void init() {
ioConfigurationDAO = new IOConfigurationDAO();
ioConfigurationDAO.init();
property = new AigatewayProperty();
for (int i = 1; i <= property.MAX_PORT_NUM; ++i) {
ioConfigurationDAO.getManager().getTransaction().begin();
ioConfigurationDAO.createIOConfiguration(i);
ioConfigurationDAO.getManager().getTransaction().commit();
}
List<IOConfiguration> list = ioConfigurationDAO.findAllIOConfiguration();
System.out.println(list);
ioConfigurationDAO.destroy();
}
我可以看到从cqlsh控制台插入到我的数据库中的行。
我为DAO编写的所有服务,如插入,删除,修改,工作都很完美,所以我认为wildfly和我的cassandra数据库之间的连接没有问题。
但是,如果我使用HQL,查询将无法正常工作。
对于我上面提到的端点,这是我试图调用的方法:
@SuppressWarnings("unchecked")
public List<IOConfiguration> findAllIOConfiguration() {
Query query = this.getManager().createNativeQuery("select * from \"IO_CONFIGURATION\"");
// Query query = this.getManager().createQuery("from IOConfiguration");
return query.getResultList();
}
如果我像第一行一样使用 createNativeQuery ,端点将完美运行,这是我从resteasy获得的结果:
但是,如果我像第二行一样使用 createQuery ,端点将无效并给我一个空列表。
这是我的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="JPAService">
<!-- Use the Hibernate OGM provider: configuration will be transparent -->
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<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="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>
我不知道原因是什么。这很奇怪,有人可以向我解释一下吗?
由于
修改
由于某些stackoverflow点问题,我无法上传有关某些数据库连接测试结果的屏幕截图。以下是我可以分享的有关我的数据库的一些可用资源。 我正在使用cassandra,所以我使用了一个名为&#39; dev&#39;存储我的所有表。 这是我在#34之后从终端获得的结果;描述dev&#34;:
CREATE KEYSPACE dev with replication = {&#39; class&#39;:&#39; SimpleStrategy&#39;, &#39; replication_factor&#39;:&#39; 1&#39;}和durable_writes = true;
CREATE TABLE dev。&#34; DATA_SERIES_META&#34; ( &#34; DATA_SERIES_META_ID&#34;文字PRIMARY KEY, &#34; B_INFO&#34;双, &#34; CHANNEL_NAME&#34;文本, &#34; CONVERTION_TYPE&#34;文本, &#34; IO_CONFIGURATION&#34;文本, &#34; LAST_SAMPLE_TIME&#34;文本, &#34; M_INFO&#34;双, &#34; NAME&#34;文本, &#34; SAMPEL_PERIOD&#34; INT, &#34; SAMPLE_RANGE&#34;文本, &#34; START_TIME&#34;文本, &#34; STORE_CONVERTED&#34;布尔, &#34; STORE_ROW&#34;布尔, &#34; TOTAL_SAMPLE&#34; BIGINT, &#34; VOLTAGE_DIVIDE&#34; text)WITH bloom_filter_fp_chance = 0.01 AND caching = {&#39; keys&#39;:&#39; ALL&#39;,&#39; rows_per_partition&#39;:&#39; NONE&#39;} 和评论=&#39;&#39; AND compaction = {&#39; class&#39;:&#39; org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy&#39;, &#39; max_threshold&#39;:&#39; 32&#39;,&#39; min_threshold&#39;:&#39; 4&#39;} AND compression = {&#39; chunk_length_in_kb&#39;:&#39; 64&#39;,&#39; class&#39;:&#39; org.apache.cassandra.io.compress.LZ4Compressor&#39;} AND crc_check_chance = 1.0 AND dclocal_read_repair_chance = 0.1 AND default_time_to_live = 0 AND gc_grace_seconds = 864000 AND max_index_interval = 2048 AND memtable_flush_period_in_ms = 0 AND min_index_interval = 128 AND read_repair_chance = 0.0 AND speculative_retry =&#39; 99PERCENTILE&#39 ;;创建INDES DATA_SERIES_META_IO_CONFIGURATION on dev。&#34; DATA_SERIES_META&#34; (&#34; IO_CONFIGURATION&#34);
CREATE TABLE dev。&#34; NODE_DATA&#34; ( &#34; NODEDATA_ID&#34;文字PRIMARY KEY, &#34; IO_CONFIGURATION&#34;文本, &#34; TIME&#34; BIGINT, &#34; VALUE&#34; double)WITH bloom_filter_fp_chance = 0.01 AND caching = {&#39; keys&#39;:&#39; ALL&#39;,&#39; rows_per_partition&#39;:&#39; NONE&#39;} 和评论=&#39;&#39; AND compaction = {&#39; class&#39;:&#39; org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy&#39;, &#39; max_threshold&#39;:&#39; 32&#39;,&#39; min_threshold&#39;:&#39; 4&#39;} AND compression = {&#39; chunk_length_in_kb&#39;:&#39; 64&#39;,&#39; class&#39;:&#39; org.apache.cassandra.io.compress.LZ4Compressor&#39;} AND crc_check_chance = 1.0 AND dclocal_read_repair_chance = 0.1 AND default_time_to_live = 0 AND gc_grace_seconds = 864000 AND max_index_interval = 2048 AND memtable_flush_period_in_ms = 0 AND min_index_interval = 128 AND read_repair_chance = 0.0 AND speculative_retry =&#39; 99PERCENTILE&#39 ;;在dev。&#34; NODE_DATA&#34;上创建索引NODE_DATA_IO_CONFIGURATION (&#34; IO_CONFIGURATION&#34);
CREATE TABLE dev。&#34; IO_CONFIGURATION&#34; ( &#34; IO_CONFIGURATION_ID&#34;文字PRIMARY KEY, &#34; ACTIVE&#34;布尔, &#34; B_INFO&#34;双, &#34; CHANNEL_NAME&#34;文本, &#34; CODE&#34;文本, &#34; CONVERSION_TYPE&#34;文本, &#34; DEFAULT_GRAPH&#34;文本, &#34; M_INFO&#34;双, &#34; NAME&#34;文本, &#34; RANGE_LOWERBOUND&#34; INT, &#34; RANGE_UPPERBOUND&#34; INT, &#34; SAMPLE_PERIOD&#34; INT, &#34; SAMPLE_RANGE&#34;文本, &#34; STORE_CONVERTED&#34;布尔, &#34; STORE_ROW&#34;布尔, &#34; TITLE&#34;文本, &#34; UNIT&#34;文本, &#34; VOLTAGE_DIVIDE&#34; text)WITH bloom_filter_fp_chance = 0.01 AND caching = {&#39; keys&#39;:&#39; ALL&#39;,&#39; rows_per_partition&#39;:&#39; NONE&#39;} 和评论=&#39;&#39; AND compaction = {&#39; class&#39;:&#39; org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy&#39;, &#39; max_threshold&#39;:&#39; 32&#39;,&#39; min_threshold&#39;:&#39; 4&#39;} AND compression = {&#39; chunk_length_in_kb&#39;:&#39; 64&#39;,&#39; class&#39;:&#39; org.apache.cassandra.io.compress.LZ4Compressor&#39;} AND crc_check_chance = 1.0 AND dclocal_read_repair_chance = 0.1 AND default_time_to_live = 0 AND gc_grace_seconds = 864000 AND max_index_interval = 2048 AND memtable_flush_period_in_ms = 0 AND min_index_interval = 128 AND read_repair_chance = 0.0 AND speculative_retry =&#39; 99PERCENTILE&#39 ;;创建索引IO_CONFIGURATION_CHANNEL_NAME开发。&#34; IO_CONFIGURATION&#34; (&#34; CHANNEL_NAME&#34);
CREATE TABLE dev。&#34; ALERT&#34; ( &#34; ALERT_ID&#34;文字PRIMARY KEY, &#34; IO_CONFIGURATION&#34;文本, &#34; OPERATOR&#34;文本, &#34; VALUE&#34; double)WITH bloom_filter_fp_chance = 0.01 AND caching = {&#39; keys&#39;:&#39; ALL&#39;,&#39; rows_per_partition&#39;:&#39; NONE&#39;} 和评论=&#39;&#39; AND compaction = {&#39; class&#39;:&#39; org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy&#39;, &#39; max_threshold&#39;:&#39; 32&#39;,&#39; min_threshold&#39;:&#39; 4&#39;} AND compression = {&#39; chunk_length_in_kb&#39;:&#39; 64&#39;,&#39; class&#39;:&#39; org.apache.cassandra.io.compress.LZ4Compressor&#39;} AND crc_check_chance = 1.0 AND dclocal_read_repair_chance = 0.1 AND default_time_to_live = 0 AND gc_grace_seconds = 864000 AND max_index_interval = 2048 AND memtable_flush_period_in_ms = 0 AND min_index_interval = 128 AND read_repair_chance = 0.0 AND speculative_retry =&#39; 99PERCENTILE&#39 ;;创建索引ALERT_IO_CONFIGURATION on dev。&#34; ALERT&#34; (&#34; IO_CONFIGURATION&#34);
所以可能问题不在于我有一个重复的表。
以下是关于如何将行插入&#34; IO_CONFIGURATION&#34;的代码。表:
public IOConfiguration createIOConfiguration(Integer i) {
if (i <= 0) {
throw new IllegalArgumentException(
"Invalid configuration channel found when trying to create IO configuration");
} else if (i.equals(null)) {
throw new NullPointerException(
"Configuration channel is null when trying to create IO configuration");
}
IOConfiguration emp = new IOConfiguration();
emp.setChannelName(ChannelName.fromInt(i));
this.getManager().persist(emp);
return emp;
}
我无法分享结果的屏幕截图,但我确信在调用此函数后,我可以在&#34; IO_CONFIGURATION&#34;中插入一行。表
我现在正在为这个项目使用createNativeQuery。可以只选择一行,因为我可以在查询后指定一个pojo类类型并且它工作正常。
但是在使用此原生查询时从表中选择数据列表非常令人沮丧。我必须手动将对象类型转换为我的pojo,因为当我在查询后指定一个pojo类时,如:
Query query = ioConfigurationDAO.getManager()
.createNativeQuery("select * from \"IO_CONFIGURATION\"", IOConfiguration.class);
它只会给我数据库中的第一行,我不知道发生了什么。
如果你们需要更多信息,我愿意尽可能多地分享。
谢谢!
答案 0 :(得分:0)
要使用Cassandra和Hibernate OGM执行查询,您现在需要Hibernate Search。
本文档的这一段应该为您提供所有细节: https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html_single/index.html#ogm-query-using-hibernate-search
简而言之,您需要在实体类上添加注释org.hibernate.search.annotations.Index
,并在要搜索的列上添加org.hibernate.search.annotations.Field
。
编辑:我们没有意识到您已经在使用Hibernate Search,我会给出答案。
这意味着它只会找到被索引的实体。如果您的数据库中已包含某些实体,则需要先将它们编入索引。
首次启动应用程序时,您需要执行以下操作:
FullTextSession session = Search.getFullTextSession( openSession() );
session.createIndexer( IOConfiguration.class ).startAndWait();
这将确保在索引中添加现有实体。有关using the mass indexer。
的更多详细信息,请查看Hibernate Search文档当您想要重新创建/刷新索引时,只需执行此操作,当您使用Hibernate OGM执行CRUD操作时,索引会自动更新。