I'm constructing a hql query that needs to return orders where the User's ID is null:
IList<Order> rows = DataContext.LoadList<Order>(
"from Order " +
"where OrderDate < ? " +
" and User.ID is null" //have also tried 'and User is null'
, DateTime.Today.AddMonths(-1));
This returns no records even though a direct query against the db returns a few dozen.
If I use the same query but replace the 'is null' with '33' (a valid userID), I'll get that user's orders. So the foundation of the query is valid - something's wrong with how I'm expressing the 'is null' bit.
Probably has something to do with the fact that, in the .NET project the Order object contains a complex User object where as in the database, the Orders table just holds integer (ID) that's a foreign key to the User table.
Still - since 'and User.ID = 33' works as expected I don't follow why 'is null' doesn't.
This answer suggests I need to switch of the Criteria route. This answer suggests that my current code should work.
thx
Rather than building a query statement, use a Query factory approach:
Query q = new Query();
q.Criteria.Add(new Criteria("User.ID", CriteriaOperator.IsNull));
Works.
Related
For table cmdb_rel_ci, I want to retrieve unique parent.sys_class_name with count for "type=In Rack::Rack contains". I am doing practice in out of the box instance.
At table level URL is as below:
URL
I want to retrieve result from above URL with my below script.
var count = new GlideAggregate('cmdb_rel_ci');
count.addQuery('type','e76b8c7b0a0a0aa70082c9f7c2f9dc64');// sys_id of type In Rack::Rack contains e76b8c7b0a0a0aa70082c9f7c2f9dc64
count.addAggregate('COUNT', 'parent.sys_class_name');
count.query();
while(count.next()){
var parentClassName = count.parent.sys_class_name.toString();
var parentClassNameCount = count.getAggregate('COUNT','parent.sys_class_name');
gs.log(parentClassName + " : " + parentClassNameCount );
}
The issue is I am getting parentClassName empty.
Try this instead:
var parentClassName = count.getValue("parent.sys_class_name")
Since it's a GlideAggregate query (instead of GlideRecord), the query being issued isn't returning all of the fields on the target table. With GlideRecord, dot-walking through a reference field (e.g. parent.sys_class_name) automatically resolves that referenced record to provide access to its field values. This is made possible by the fact that the driving/original query brought back the value of the parent field. This is not happening with GlideAggregate. The query in this case basically looks like:
SELECT cmdb1.`sys_class_name` AS `parent_sys_class_name`, count(*)
FROM (cmdb_rel_ci cmdb_rel_ci0 LEFT JOIN cmdb cmdb1 ON cmdb_rel_ci0.`parent` = cmdb1.`sys_id` )
WHERE cmdb_rel_ci0.`type` = 'e76b8c7b0a0a0aa70082c9f7c2f9dc64'
GROUP BY cmdb1.`sys_class_name`
ORDER BY cmdb1.`sys_class_name`
So, you actually have access specifically to that dot-walked sys_class_name that's being grouped, but not through the dot-walk. The call to getValue("parent.sys_class_name") is expectedly resolved to the returned column aliased as parent_sys_class_name.
That being said, what you're doing probably should also work, based on user expectations, so you've not done anything incorrect here.
I have a table called Products having column called ApprovedState which contains values like "AL,AK,AR". I'm getting a CSV list from front-end as listCSVState='AL,AK'.
Can someone help me to write a LINQ query which will return me all products which are approved in listCSVState. I have tried following code but not getting the correct result.
from product in db.Products where ((listCSVState== null) || (listCSVState.Contains(product.StateApprovals)))
Trivial in LINQ to Objects, tricky in LINQ to Entities due to lack of string.Split support.
Still, a combination of Any, string.Contains and string concatenations can do the job, like this
var states = listCSVState.Split(',');
var query = db.Products.Where(product => states.Any(state =>
("," + product.StateApprovals + ",").Contains("," + state + ",")));
Good night, I am working in DB2 and I have been having some troubles making a query with case when
I have to find a way to make a query to define if a record of some category is valid only if the required fields of the relation are required and have been marked as selected, but in this case some registrys are empty and then I must exclude, I have a query like this:
Select (case (sum(category_id) where required='true' and selected='true') = sum(category_id) where required='true' then 'Yes'
else 'no'
from category_table
The problem, is that in some category the required field are all false (it's means do not exist) then I must put a condition like the sum equal to cero, but i always get a syntax error. I have something like this,
Select (case (sum(category_id) where required='true' and selected='true') = sum(category_id) where required='true' and (count(category_id) where required='true' != 0 ) then 'Yes'
else 'no'
from category_table
I have this problem in db2, saddly I can not put the original query hear because the proxy of my work don't le me pass emails to outside or even login at the web page from there.
I would be grateful for any help.
Have you tried using the VALUE function? if no rows will qualify the case conditions then you'll get Zero in the result.
I want to retrieve value from db ,based on jsp textbox value please suggest me accurate HQL query for this purpose:
select all from table where name = en and password = pwd
I have tried for above work via this way but got error in
getHibernateTemplate().find("from User where Employee_Name= ?"+ a);
Error -:
java.lang.reflect.InvocationTargetException
You can do this
Query query = session.createQuery("from User where Employee_Name= :name");
query.setParameter("name", "asdf");
List list = query.list();
or you can do the same as on the first answer
Seems like the issue is with the prepaid statement, you must use
hibernatemplate.find(String queryString,Object value)
Look here for more spec.
Hope this code will fix it.
getHibernateTemplate().find("from User where Employee_Name=?",a);
Query query=session.createQuery("from User where Employee_Name= "+"'en'"+
"and Employee_Pass= " + "'pwd'");
List<User> list=query.list();
Iterator<User > itr=list.iterator();
while(itr.hasNext()){
User q=itr.next();
use getter method of User class to fetch all the values
}
I try to create a cypher-query in my Java-Spring-Application which should answer the question "give all employees who did not create a item in item.nameList":
#Query("START it=node:__types__(className = 'de.my.domain.ItemCl') MATCH empl-[r:CREATE]->it WHERE (it.name NOT IN ({0})) RETURN DISTINCT empl")
List<Employee> findAllEmployeesWhoNeverCreatedItemFromItemNameList(List<String> itemNameList);
This query gives an "org.springframework.dao.InvalidDataAccessResourceUsageException" and marks the "NOT" as failure.
If I try the same query without NOT ("give all employees who did create a item in item.nameList", the query does what it should.
In this thread Peter Neubauer told that this "NOT IN" exists in cypher: https://groups.google.com/forum/#!topic/neo4j/_PehVUfGaIA
Any idea what is wrong?
NOT is a negation, so you have to do it like this:
WHERE NOT(it.name IN({0}))