Hibernate. Convert HQL to Criteria API - nhibernate

How to convert the following hql to Criteria API
var criteria = this.Session.CreateQuery("select m, m.Attachments.size from AdvanceMessage m");
?
Thanks

Your question is a little bit vague but it's something like this :
IList<AdvanceMessage> list = session.CreateCriteria(typeof(AdvanceMessage))
.List<AdvanceMessage>();

Related

How to get many terms matched using Hibernate Search query DSL?

When I search for "cars blue" I get every result that matches "cars" or "blue", but I need to match them both. I've read about setting some defaultOperator to AND but I can't find where to do that,
Also I can't use PhraseQuery because the order of the terms in the search query is irrelevant,
This is my code so far, thanks!
// create the query using Hibernate Search query DSL
QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity(Articulo.class).get();
// a very basic query by keywords
BooleanJunction<BooleanJunction> bool = queryBuilder.bool();
bool.must(queryBuilder.keyword()
.onFields("description")
.matching(text)
.createQuery()
);
Query query = bool.createQuery();
FullTextQuery jpaQuery =
fullTextEntityManager.createFullTextQuery(query, Articulo.class);
return jpaQuery.getResultList();
Note: I'm using Hibernate Search 5.6.4
I think you're looking for the Simple query string feature.
See http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#_simple_query_string_queries for more details about it.
You have an example with .withAndAsDefaultOperator():
Query luceneQuery = mythQB
.simpleQueryString()
.onField("history")
.withAndAsDefaultOperator()
.matching("storm tree")
.createQuery();
This blog post explaining the rationale of this feature might be helpful too: http://in.relation.to/2017/04/27/simple-query-string-what-about-it/ .

Convert SQL request to Hibernate Criteria

I would like to convert this SQL into either Criteria. I am sorry I don't know which one to use since I'm new to Hibernate. I've done some research, and it looks like both are needed to achieve what I wanted.
My Sql Request
select *
from change.pade pade, change.pade_etat rdp, par.safsit safsit, par.saf saf
where pade.sir = "1245454"
and pade.id_safsit = "1"
and pade.id_safsit = safsit.id
and safsit.cd_s in ("12", "45")
and safsit.fk_saf = saf.id
and saf.cd_ur in "124"
and rdp.fk_pade = pade.id
and rdp.id_etat in "444"
You can use the Hibernate properties instead of writing HQL like this
session.createSQLQuery(String sqlQuery)
see an example here

Using built-in SQL functions with SequelizeJS

Can I make Sequelize JS to include combined field to the result set, to get results like for following query?
SELECT id, NOW()-timestamp as recordAge FROM myTable
I wouldn't use raw query for this task, prefer to resolve it with model paradigm.
As there was no answer but someone has starred my question I'm posting the answer I've found myself:
The solution is to use Sequelize.literal() function. As for example in the question, the answer is following:
options = {};
options.attributes = ['id', sequelize.literal('(NOW() - timestamp) as recordAge')];
MyTable.find(options).success(success);

How to get negetive of a complex where clause in mongo db

I have a mongo db complex filter generated dynamically which might look like
where_condition = {"$and":[{"column_3": "Offer"}, {"column_2":"MSN"}]}
collection.find(where_condition)
The condition might have unknown depth in $and and $or
Is it possible to find the negative of the where_condition
This does not work
not_condition = {"$not": where_condition}
You'll want to use $nor. Something like this:
where_condition = {"$and":[{"column_3": "Offer"}, {"column_2":"MSN"}]}
not_condition = {"$nor":[{"column_3": "Offer"}, {"column_2":"MSN"}]}
You can find some more info on the mongodb doc for $nor.

Can I use SQL functions in NHibernate QueryOver?

I have been searching the internet and can't find an example on how to use the queryover of nhibernate 3.0
For example I would like to use the string functions on the where clause of the queryover
ex:
var item = Query.Where(x => x.Name.ToLower() == name.ToLower()).FirstOrDefault();
But this doesn't work, because nhibernate can't understand the ToLower, so how can extend the dialect in a way that this becomes possible?
session.QueryOver<Foo>()
.Where(Restrictions.Eq(
Projections.SqlFunction("lower", NHibernateUtil.String,
Projections.Property<Foo>(x => x.Name)),
name.ToLower()))
should get you SQL like where lower(Name) = #p0
I believe it works at least in the build I am using (version 3.0.0.4000)... below is my example...
var reasons = _session.Query<Reason>();
var myReason = (from r in reasons
where r.IsCritical
&& r.ReasonCode.ToUpper() == reasonCode.ToUpper()
select r).FirstOrDefault();
Give it a shot and let me know if it works for you...