I use new in JPQL query like the following way
select
ob.property1,
NEW package1.CustomObject(item, dimension, material, product)
from mainTable ob
LEFT JOIN ....
The application complains about the coma after ob.property1 saying it is an unexpected token. It seems that you cannot use multiple column with a new inside the select clause. Can you help me
According to this you have the following options:
Wrapping the values in a type-safe Java object that will be returned as the results of the query.
select new package1.CustomObject(item, dimension, material, product)
from MainTable ...
The projection class must be fully qualified in the entity query, and it must define a matching constructor. The class here need not be mapped. It can be a DTO class. If it does represent an entity, the resulting instances are returned in the NEW state (not managed!).
HQL supports additional "dynamic instantiation" features.
The query can specify to return a List rather than an Object[] for scalar results.
select new list(item, dimension, material, product)
from MainTable ...
The results from this query will be a List<List> as opposed to a List<Object[]>.
Wrapping the scalar results in a Map
select new map(
item as iName,
dimension as iDimension,
material as iMaterial,
product as iProduct)
from MainTable ...
The results from this query will be a List<Map<String, Object>> as opposed to a List<Object[]>. The keys of the map are defined by the aliases given to the select expressions. If the user doesn’t assign aliases, the key will be the index of each particular result set column (e.g. 0, 1, 2, etc).
Related
I need to create a new table in MS Access based on a specific attribute. To simplify the problem, I will illustrate the desired result with a dummy table:
As you can see, I have different flowers (distinguished by their numbers - Flowerno), along with their attributes and values. The new table should be based on the distinct pairs of Attribute "color" and its Value. So in this example, we have two pairs: (color, red) and (color, yellow). Now, each pair belongs to a specific Flowerno and its other attributes like shape and fragrance. I would like to create a table with the attribute color as the main focus and present its other attributes of the same flower:
In other words, I want to list all colors along with the attributes and values distinctively in three different columns.
You need code that will do a lookup of color associated with flowerno and return that value for use in sorting/grouping. Could try calculating a field with DLookup:
GrpColor: DLookup("value", "table","attribute='color' AND flowerno=" & [flowerno])
Unfortunately, domain aggregate function can cause slow performance in query. Alternative would use subquery:
SELECT table.*, Q.GrpColor FROM table
INNER JOIN (SELECT FlowerNo, [Value] AS GrpColor
FROM table WHERE attribute = "color") AS Q
ON table.FlowerNo = Q.FlowerNo
WHERE Attribute<>"color";
I have a table with one of the field defined as "attributes" OBJECT (DYNAMIC).
Now, my usecase is to be able to check if any particular string is part of this OBJECT. In SQL terms I want to execute a like command on this whole OBJECT or even whole Document. Do we currently support this feature
Query I want to execute: select * from gra.test_table where attributes like '%53.22.232.27%';
When I execute this on Crate 4.5.1, I am running into error: UnsupportedFeatureException[Unknown function: (gra.test_table.attributes LIKE '%53.22.232.27%'), no overload found for matching argument types: (object, text). Possible candidates: op_like(text, text):boolean]
When I execute this on Crate 3.x, I am running into error:
SQLActionException[SQLParseException: Cannot cast attributes to type string]
Table structure is below and attributes is the field I am talking about
CREATE TABLE IF NOT EXISTS "test"."test_table" (
"accountname" STRING,
"attributes" OBJECT (DYNAMIC) AS (
"accesslist" STRING,
"accesslistid" STRING,
"accessmask" STRING,
"accessreason" STRING
),
"employeeid" STRING,
"day" TIMESTAMP,
PRIMARY KEY ("day", "id"),
INDEX "all_columns_ft" USING FULLTEXT ("employeeid") WITH (
analyzer = 'standard'
)
)
CLUSTERED INTO 1 SHARDS
PARTITIONED BY ("day")
WITH (
"allocation.max_retries" = 5
)
Using like on a whole object is not supported.
A possible (slow) workaround could be to use a regular expression operator on the text cast of an object typed column:
select * from gra.test_table where attributes::text ~ '.*53.22.232.27.*'
But be aware that through the cast, no index could be utilized and such a full table scan + filter is executed resulting in slow query execution.
It is straight forward to create a calculated field in a table that uses data IN the table... due to the fact that the expression builder is straight forward to use. However, it appears to me that the expression builder for the calculated field only works with data IN the table;
i.e: expression builder in table MYTABLE works with fields FIELD1.MYTABLE, FIELD2.MYTABLE etc.
Inventory Problem
My problem is that I have two 'count' fields that result from my queries that apply to INPUTQUERY and OUTPUTQUERY (gives me a count of all input data added and a count of all output data added) and now I want to subtract the two to get a stock.
I can't link the table that was created from my query because it wont be able to continually update do the relationship itself, and thus i'm stuck either using the expression builder/SQL.
First question:
Is it possible to have the expression builder reference data from other tables?
i.e expressionbuilder for:
MAINTABLE CALCULATEDFIELD.MAINTABLE = INPUTSUM.INPUTTABLE - OUTPUTSUM.OUTPUTTABLE
(which gives a difference of the two)?
Second question:
if the above isn't possible, can I do this through an SQL code ?
i.e
SELECT(data from INPUTSUM)
FROM(INPUTTABLE)
-
SELECT(data from OUTPUTSUM)
FROM(OUTPUTTABLE)
Try this:
SELECT SUM(T.INPUTSUM) - SUM(T.OUTPUTSUM) AS RESULTSUM
FROM
(
SELECT INPUTSUM, 0 AS OUTPUTSUM
FROM INPUTTABLE
UNION
SELECT 0 AS INPUTSUM, OUTPUTSUM
FROM OUTPUTTABLE
) AS T
it looks that SqlQuery only supports sql that starts with select *? Doesn't it support other sql that only select some columns like
select id, name from person and maps the columns to the corresponding POJO?
If I use SqlFieldQuery to run sql, the result is a QueryCursor of List(each List contains one record of the result). But if the sql starts with select *, the this list's contents would be different with field query like:
select id,name,age from person
For the select *, each List is constructed with 3 parts:
the first elment is the key of the cache
the second element is the pojo object that contains the data
the tailing element are the values for each column.
Why was it so designed? If I don't know what the sql that SqlFieldsQuery runs , then I need additional effort to figure out what the List contains.
SqlQuery returns key and value objects, while SqlFieldsQuery allows to select specific fields. Which one to use depends on your use case.
Currently select * indeed includes predefined _key and _val fields, and this will be improved in the future. However, generally it's a good practice to list fields you want to fetch when running SQL queries (this is true for any SQL database, not only Ignite). This way your code will be protected from unexpected behavior in case schema is changed, for example.
I am creating a java bean from a master detail table. Lets call the master table A and Detail table B. When I make this in to a java bean I have BeanA and BeanB. BeanA will have all the relavant rows in the A table and will also have a list collection of BeanB (detail table B) based on the primarykey. So BeanA will look like
class BeanA {
String property1;
String Property2;
List<BeanB> lstpropery; //This is the data of detail table based on the primary key
}
In this scenerio I am using Spring JDBC to query the table. So when I query the table A and loop through the resultset and set the BeanA properties, I will also set the lstproperty by doing the query to B table.
so the code will be
String qry = "select * from A';
result = this.queryTemplate.query(qry, obj, new RowMapper(){
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
BeanA bean1= new BeanA();
bean1.setProperty1(rs.getString(1));
bean1.setProperty2(rs.getString(2));
bean1.setLstpropery(callTableB(String primaryKey)); // I see a performance issue here
return bean1;
}
});
Is there a better way to do this?
Use a left join :
select a.*, b.* from A a left outer join B b on a.id = b.aId
(You should in fact explicitely list all the columns and assign aliases to each of them).
This query will retrieve a row per couple A-B, and will also retrieve all the As which have no B.
For each row, see if you already have an A with the returned A ID. If not, construct the A and put it in a map (indexed by A ID). Then construct a B instance with the columns from the B table (unless the returned B ID is null, which means that the A has no B), and add it to the list of Bs of the A you got from the map or just constructed.
Note that ORMs like JPA make this tedious work much easier: they populate an object tree from a single request for you.
If you have a chance to use Hibernate, then lazy-fetching concept might be useful here.