SpringJPA please suggest custom query for - spring-data-jpa

Can you suggest a custom query for
SELECT p1.*
FROM ngb.payment_bifurcation AS p1
WHERE p1.bill_id = 13528 And exists ( SELECT *
FROM ngb.payment AS p2
WHERE p2.deleted = false and p1.payment_id = p2.id
)
bill_id is a parameter.

You might need to create a native query for this. Standard Data JPA methods might not help.

Related

How to create query order by not in JPA Criteria Builder

I want to ask, how to make a query order by not in in java, this is the query, thank you:
select * from $table_name order by id not in
(select r.transaction_id from $child_table r where r.transaction_id is not null) desc;
Session session = HibernateUtil.getHibernateSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<PARENT_ENTITY> cr = cb.createQuery(<PARENT_ENTITY>.class);
Root<PARENT_ENTITY> root = cr.from(PARENT_ENTITY.class);
CriteriaQuery<PARENT_ENTITY> cr_child = cb.createQuery(<CHILD_ENTITY>.class);
Root<CHILD_ENTITY> child_root = cr_child.from(CHILD_ENTITY.class);
cr.select(root).where(cb.isNotNull(child_root.get("transaction_id")));
Please try to fill the values for entities as per requirement, hope this helps

Update PgSQL Self JOIN With Custom Values

I'm trying to use UPDATE SELF JOIN and could not seem to get the correct SQL query.
Before the query, I execute this SQL query to get the values:
SELECT DISTINCT ON (purpose) purpose FROM user_assigned_customer
sales_manager
main_contact
representative
administrator
By the time I run this query, it overwrites all the purpose columns:
UPDATE user_assigned_customer SET purpose = (
SELECT 'main_supervisor' AS purpose FROM user_assigned_customer AS assigned_user
LEFT JOIN app_user ON app_user.id = assigned_user.app_user_id
WHERE app_user.role = 'supervisor'
AND user_assigned_customer.purpose IS NULL
AND assigned_user.id = user_assigned_customer.id
)
The purpose column is now only showing when running the first query:
main_supervisor
Wondering if there is a way to query to update SQL Self JOIN with a custom value.
I think I got it with a help of a friend.
UPDATE user_assigned_customer SET purpose = 'main_supervisor'
FROM user_assigned_customer AS assigned_user
LEFT JOIN app_user ON app_user.id = assigned_user.app_user_id
WHERE app_user.role = 'supervisor'
AND user_assigned_customer.purpose IS NULL
AND assigned_user.id = user_assigned_customer.id

How to correctly use the querybuilder in order to do a subselect?

I would like to do a subselect in order to do the following postgresql query with the querybuilder:
SELECT i.* FROM internship i
WHERE EXISTS (SELECT iw.*
FROM internship_weeks iw
WHERE i.id = iw.internship)
Does anyone have an idea how to get the same result with queryBuilder? or maybe with DQL?
Thanks for the help !
As example, only for demonstrate HOW-TO use a subquery select statement inside a select statement, suppose we what to find all user that not yet have compile the address (no records exists in the address table):
// get an ExpressionBuilder instance, so that you
$expr = $this->_em->getExpressionBuilder();
// create a subquery
$sub = $this->_em->createQueryBuilder()
->select('iw')
->from(IntershipWeek::class, 'iw')
->where('i.id = iw.intership');
$qb = $this->_em->createQueryBuilder()
->select('i')
->from(Intership::class, 'u')
->where($expr->exists($sub->getDQL()));
return $qb->getQuery()->getResult();
Hope this help

Zend Framework 1 SQL query Where

Is there any way to do something like this:
SELECT * FROM table WHERE p1=1 AND p2=2 AND ( p3 like %string1% OR p3 like %string3% )
In Zend Framework 1 by Zend_Db_Select or something else ?
Not sure about how you would go about doing such complex nested query using Zend_Db_Select but you can write can consider writing query manually as follows -
$sql = 'SELECT * FROM table WHERE p1 = ? AND p2 = ? AND (p3 LIKE ? OR p3 LIKE ?)';
$db->fetchAll($sql, [$p1, $p2, "%{$p3}%", "%{$p4}%"]);
You should rather use Zend_Db_Statement, which gives flexible options for fetching result sets. You can use:
$stmt = $db->query(
'SELECT * FROM table WHERE p1 = ? AND p2 = ? AND (p3 LIKE ? OR p3 LIKE ?',
array('1', '2', '%string1%', '%string3%')
);

How to integrate WITH Recursive in a Querydsl query

I'm new to Querydsl and I'm struggling to figure out how to implement the following query:
WITH RECURSIVE results AS
(
SELECT id,
parent_id
FROM project
WHERE id = '8a3d6714-27fa-4d1f-962f-9047d616ab42'
UNION ALL
SELECT t.id,
t.parent_id
FROM project t
INNER JOIN results r ON r.parent_id = t.id
)
SELECT *
FROM results;
Its objective is to, starting from the bottom of a hierarchy, collect the anchor's parent (8a3d6714-27fa-4d1f-962f-9047d616ab42 in the above example; there is only one parent per row) all the way up to a point where there is a element without a parent. If successful, using my database, it should yield the following:
id parent_id
------------------------------------ ------------------------------------
8a3d6714-27fa-4d1f-962f-9047d616ab42 babc74e8-1b6f-49e8-a1e3-a176fce2975d
babc74e8-1b6f-49e8-a1e3-a176fce2975d 3f83c9a2-bf43-46d8-bf87-070f5b55ae5a
3f83c9a2-bf43-46d8-bf87-070f5b55ae5a 69c074c6-a329-42c3-8e2e-5da9ab0ef81e
69c074c6-a329-42c3-8e2e-5da9ab0ef81e bccab264-027c-4c4f-9ae8-3409efc6aeb3
bccab264-027c-4c4f-9ae8-3409efc6aeb3 227db39a-2219-4abb-a2a7-3d28bf47ac01
227db39a-2219-4abb-a2a7-3d28bf47ac01
Any thoughts would be much appreciated.
Regards
Rodrigo
QProjects results = new QProjects("results");
QProjects p = new QProjects("p");
QProjects t = new QProjects("t");
String id = "8a3d6714-27fa-4d1f-962f-9047d616ab42";
query.withRecursive(results, subQuery()
.unionAll(
subQuery().from(p).where(p.id.eq(id)).list(p.id, p.parentId),
subQuery().from(t).innerJoin(results).on(results.parentId.eq(t.id)).list(t.id, t.parentId)))
.from(results)
.list(results.id, results.parentId);