How to write join query using hibernate criteria - sql

I am new to hibernate criteria API. I have written the below query in SQL. How to write it in the Hibernate criteria?
select * from Employee ju, Address ua where
ua.loginname=ju.loginname
and ua.countryname='USA'
and ju.siteprimcontact=1;

Here is a tutorial on the JPA Criteria API that you can use to learn how to write such queries: https://docs.oracle.com/javaee/6/tutorial/doc/gjivm.html

Related

Filter by related document in RavenDB

How can I translate the following SQL query to RQL?
Select bike.* from Bike
inner join Appointment on Bike.BikeID = Appointment.BikeID and
Appointment.Status != 'Booked'
I know that in Mongo it is possible to filter by related documents.
However, RavenDB documentation says that:
It's important to remember that the load clause is not a join; it's applied after the query has already run and before we send the interim results to the projection for the final result. Thus, it can't be used to filter the results of the query by loading related documents and filtering on their properties.
So what is the correct way to run such a query in RavenDB?

Jpa query with Predicate

How select one column by predicate in jpa?
For exemple like this
#Query("SELECT SUM (u.sum) from User u")
Double findSums(Predicate predicate);
Spring Data JPA does not have direct support for this kind of query.
Therefore the solution is to fall back to plain JPA in a custom method for your repository.
In that custom method you'd use the Criteria API to create the SELECT SUM (u.sum) from User u apply the predicate to it and execute it using the EnterpriseManager.

ebean in play 2 framework with SQL dialect

I'm trying to establish friendship between Teradata 13.10 and play 2 framework using ebean ORM layer. My app does try to query DB:
select t0.workflow_id c0, t0.CHNL_TYPE_CD c1, t0.WORKFLOW_NAME c2, t0.INFO_SYSTEM_TYPE_CD c3, t0.FOLDER_NAME c4 from ETL_WORKFLOW t0 order by name limit 11
The problem is... that Teradata does know nothing about LIMIT Is there any possibility to find implementation/override something and make underlying ORM work with Teradata?
UPD:
Seems like I have to do something with tese classes:
http://www.avaje.org/static/javadoc/pub/index.html
I'm looking for samples:
1. Set proper SQL dialect for ebean or make it work in SQL ANSI mode.
2. Override classes for ebean and write own implementation of LIMIT functionality.
Teradata supports both the TOP n operator and SAMPLE clause in your SELECT statement. TOP n being an extension to the ANSI SQL 2008 standard is the closest equivalent to the LIMIT n operator you are looking for.
TOP n is processed after all the other clauses in your SELECT statement have been satisfied. It is the functional replacement of using the QUALIFY ROW_NUMBER() or QUALIFY RANK() window aggregates to accomplish the same task providing at worst equivalent performance of the window aggregate functions.
SAMPLE provides you some additional flexibility by allowing you to returned multiple sample sets within a single result. It can also be used for simple, random samples from a resultset as well. Given the options that are available with SAMPLE you are best served with referring to the SQL Data Manipulation Language manual for Teradata for all of the details. The Teradata manuals can be downloaded from here. Just select which version of Teradata you wish to download the manuals for.
Edit:
Using the RawSQL feature with Ebean you may be able use either the SAMPLE or TOP n operators in your SQL explicitly and not allow Ebean to add expressions such as the LIMIT OFFSET clause automatically. Have you tried this approach yet?

SQL query to criteria nhibernate

Hello. I have a problem with the creation of a Hibernate Criteria object. I'm new to Hibernate.
Can someone help me with the creation of a complex Criteria object and explain how this is done? Here is the sample SQL select statement to emulate:
select * from Company join Employees on Company.IDCompany = Employees.IDCompany;
If you user NH3 you can use QueryOver instead of ICriteria, as for me QueryOver expressions are better than ICriteria strings.
Session.QueryOver<Company>()
.JoinQueryOver(company => company.Employees)
.Where(...) // some restrictions
.List<Company>();
http://nhforge.org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx

Inner join fetch in Linq query

Is it possible to modify a query (or the mapping) so that the call to the .Fetch() extension method of NHibernate results in a inner join in the generated SQL query?
In HQL this is easy, you just have to write inner join fetch instead of left join fetch, but I couldn't find that for Linq.
Since this isn't possible yet, I have created a Jira issue for it: NH-2790
UPDATE 2:
https://www.nuget.org/packages/NHibernate.Linq.InnerJoinFetch
Enjoy!!!
UPDATE:
I finally managed to do this and sent the followign pull request to the NH team
https://github.com/nhibernate/nhibernate-core/pull/210
In Fact, it is possible but you need to hack NHibernate, the line to change is this one
tree.AddFromClause(tree.TreeBuilder.LeftFetchJoin(join, tree.TreeBuilder.Alias(alias)));
into
tree.AddFromClause(tree.TreeBuilder.FetchJoin(join, tree.TreeBuilder.Alias(alias)));
at the ProcessFetch.cs file
It seems the behavior is hardcoded for Linq and I think its because its using the extension methods in order to send what to use for the DefaultQueryProvider and re-linq processing, I still need to figure out how to specify the kind of join you want to do on eager fetching using the linq api but I'm working on it, hopefully I'll send a pull request to the NH team so they can put this into the codebase, for now in my project I'll use my custom NH version
Not sure if you can control the JOIN type in LINQ but you can still have strongly typed queries controlling the JOIN type using the QueryOver API.
var ordersWithCustomers = Session.QueryOver<Order>()
.JoinQueryOver(o => o.Customer,JoinType.InnerJoin) //You can specify join type
.List();
Also try adding not-null="true" in your mapping, although I just tried this and it didn't make a difference to the join type in the query.
Wierdly this works for the inner join (using the old ANSI SQL joining syntax) but doesn't fetch the Customer.
var ordersWithCustomers =
from o in Session.Query<Order>()
join c in Session.Query<Customer>() on o.Customer.Id equals c.Id
select o;