createQuery not working but createNativeQuery works
sensorhound-nan Mar 7, 2017 4:11 PMHi, guy, I have a very strange problem.
I am setting up some endpoints in my application and I have an endpoints like this:
@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();
}
}
The idea is that I need to get all the information "IO Configuration" from the table, and I have 32 rows in a table called "IO_CONFIGURATION", the pojo for this entity is like this:
@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;
...
}
And here is how I insert the rows:
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();
}
And this is part of the table on my cqlsh console:
It is very clear my data has been inserted into the database.
All services I have written for my DAO, like insert, delete, modify, work perfect, so I suppose there is no problem with the connection between wildfly and my cassandra database.
But queries don't work as they are expected if I am using HQL.
For the endpoint I mentioned above, this is the method I am trying to call:
@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();
}
If I use createNativeQuery like the first line, the endpoint will work perfect, and this is the result I get from resteasy:
But if I use the createQuery like the second line, the endpoint will not work and give me an empty list.
Here is my persistence.xml for reference:
<?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>
I don't know what's the reason. It's very strange, can someone explain this to me?
Thanks

