how to hql "left join fetch" and with - left-join

how do when I'm using fetch? "with" does not work.
ISession session = NHibernateHelper.GetSession();
string sql = "FROM Filial fi " +
"left join fetch fi.LstUsuario usr " +
"with usr.NumSequencial = :idUsr " +
"order by fi.Nome";
IQuery query = session.CreateQuery(sql);
query.SetParameter("idUsr", usuario.NumSequencial, NHibernateUtil.Int64);
lstFilial = (List<Filial>)query.List<Filial>();
Error message: unexpected token: with [FROM Entidade.Filial fi left join fetch fi.LstUsuario usr with usr.NumSequencial = :idUsr order by fi.Nome]
Code in SQL
select *
from TFILIAL fi
left join TPERMIS_USR_FILIAL usrFi
on fi.anum_sequ_filial = usrfi.anum_sequ_filial
and usrfi.anum_sequ_usuario = 2;
Thank you in advance.

Related

Cannot figgure out PSQL where issue in complicated clause

I have a native query
"SELECT *, point(?2, ?1) <#> point(lng,lat) as distance FROM workers " +
" LEFT JOIN availabilities on availabilities.worker_id = workers.id " +
" WHERE workers.category_id = ?3 " +
" WHERE worker_availability.time = ?4 ORDER BY distance ASC",
nativeQuery = true
)
The error I keep getting is
org.postgresql.util.PSQLException: ERROR: syntax error at or near "WHERE"
You wrote "WHERE" 2 times. You must change this:
" WHERE worker_availability.time = ?4 ORDER BY distance ASC",
to this:
" AND worker_availability.time = ?4 ORDER BY distance ASC",

Syntax error when querying results directly to a DTO

My native query -
interface PodcastRepository: JpaRepository<Podcast, Long> {
#Query(value = "SELECT new com.krtkush.sample.modules.podcast.models.PodcastDTO" +
"(p.id, p.author, p.title, p.description c.name, c2.name) " +
"AS sub_category_name FROM podcasts p " +
"LEFT JOIN categories c ON p.podcast_category_id = c.category_id " +
"LEFT JOIN categories c2 ON p.podcast_subcategory_id = c2.category_id " +
"WHERE p.podcast_owner = :ownerId", nativeQuery = true)
fun getPodcastsByOwner(#Param("ownerId")owner: Long): List<PodcastDTO>
}
However, when I execute the function I get the following error -
org.postgresql.util.PSQLException: ERROR: syntax error at or near "." Position: 15
position 15 is . after SELECT new com
I'm following this tutorial - https://smarterco.de/spring-data-jpa-query-result-to-dto/
The difference is that I'm using SQL rather than JPQL.

An identification variable must be provided for a range variable declaration

I'm trying to use this query in my jpa but it doesn't work:
List<Object[]> query = em.createQuery("SELECT Tstat.idStatistiques, TL.codeLieu, TL.materiel, TL.zone, sum(Tstat.colis) as colis, Tstat.defaut, sum(Tstat.nbreDefaut) as nbreDefaut,"
+ " sum(Tstat.nonLu) as nonLu, sum(Tstat.multiple) as multiple, sum(Tstat.nonRecu) as nonRecu, sum(Tstat.incoherent) as incoherent, sum(Tstat.requete) as requete , "
+ "sum(Tstat.tempsFonctionnement) as tempsFonctionnement, SUM(Tstat.tempsUtilisation) as tempsUtilisation, Tstat.modeFonctionnement FROM "
+ "( SELECT CURRENT_DATE as horodatage, St.idStatistiques, St.colis, St.defaut, St.nbreDefaut, St.nonLu, St.requete, St.multiple, St.nonRecu, St.incoherent, St.tempsFonctionnement, St.tempsUtilisation, St.modeFonctionnement FROM Statistique St )"
+ " UNION "
+ "(SELECT h.horodatage, h.idStatistiques, h.colis, h.defaut, h.nbreDefaut, h.nonLu, h.nonRecu, h.requete, h.multiple, h.incoherent, h.tempsFonctionnement, h.tempsUtilisation, h.modeFonctionnement FROM Statistiqueshisto h )"
+ " Tstat "
+ "LEFT JOIN (SELECT * FROM Lieux) as TL on Tstat.idStatistiques = TL.code_VI WHERE idStatistiques like :A ").setParameter("A", 0040+"%").getResultList();
This gives me error
The expression is invalid, which means it does not follow the JPQL
grammar.

SQLAlchemy ORM LEFT OUTER JOIN with parentheses

I'm trying to translate the following raw sql into SQLAlchemy ORM
Raw SQL:
objects = request.db.execute("select user_login.userlogin_id, user_friend.status,"\
" user_login.email, user_info.fname, user_info.lname"\
" from user_login "\
" LEFT OUTER JOIN user_info on "\
" user_info.userlogin_id = user_login.userlogin_id "\
" LEFT OUTER JOIN user_friend on "\
" (user_info.userlogin_id = user_friend.send_from_userlogin_id or"\
" user_info.userlogin_id = user_friend.send_to_userlogin_id )"\
" where email='%s' " %request.POST['q'])
SQLAlchemy version:
objects = request.db.query(UserLogin.email, UserLogin.userlogin_id, UserInfo.fname,
UserInfo.lname, UserFriend.status)\
.outerjoin(UserInfo, UserLogin.userlogin_id == UserInfo.userlogin_id)\
.outerjoin(UserFriend,
or_(UserLogin.userlogin_id == UserFriend.send_from_userlogin_id \
,UserLogin.userlogin_id == UserFriend.send_to_userlogin_id) )\
.filter(UserLogin.email==request.POST['q'])
I took a look at the log and it is the following which looks a bit strange to me, as it doesn't have any parentheses:
INFO [sqlalchemy.engine.base.Engine][Dummy-4] SELECT user_login.email AS
user_login_email, user_login.userlogin_id AS user_login_userlogin_id,
user_info.fname AS user_info_fname, user_info.lname AS user_info_lname,
user_friend.status AS user_friend_status FROM user_login LEFT OUTER JOIN
user_info ON user_login.userlogin_id = user_info.userlogin_id LEFT OUTER JOIN
user_friend ON
user_login.userlogin_id = user_friend.send_from_userlogin_id OR
user_login.userlogin_id = user_friend.send_to_userlogin_id
WHERE user_login.email = %(email_1)s
How should I structure the sqlalchemy such that there will be () for user_login.userlogin_id = user_friend.send_from_userlogin_id OR
user_login.userlogin_id = user_friend.send_to_userlogin_id for the last LEFT OUTER JOIN?

ADO.NET working with SQL and database

I'm getting an exception error saying missing operators can anyone help
string sql = "Select SalesPerson.Name, Item.Description, Orders.Quantity, Orders.OrderDate"
+ "From([Orders]"
+ "Inner Join[SalesPerson] On Orders.SalesPersonID=SalesPerson.SalesPersonID)"
+ "Inner Join[Item] On Orders.ItemNumber=Item.ItemNumber"
+ "Where Orders.CustomerID=#customer Order by Orders.OrderDate DESC";
You need to add some spaces at the end of each of your lines of SQL!
string sql = "SELECT SalesPerson.Name, Item.Description, Orders.Quantity, Orders.OrderDate "
+ "FROM [Orders] "
+ "INNER JOIN [SalesPerson] ON Orders.SalesPersonID = SalesPerson.SalesPersonID "
+ "INNER JOIN [Item] ON Orders.ItemNumber = Item.ItemNumber "
+ "WHERE Orders.CustomerID = #customer "
+ "ORDER BY Orders.OrderDate DESC";
Otherwise, your SQL ends up being
Select ..... Orders.OrderDateFROM([Orders]Inner Join[SalesPerson] .....
and so on - and that's just not valid SQL.
I also removed some unnecessary parenthesis around the JOIN operators - those are only needed for MS Access, but since you're saying you're using ADO.NET, I assume this is not for MS Access and therefore, those parenthesis are not needed