Convert SQL Query to play Query Builder Syntax - java

I need help with converting an SQL-Query to something I can use inside play!
The original Query looks like this:
SELECT * FROM `triplestore`
WHERE `owning_admin_id` IS NOT NULL
AND `is_public` IS true;
My goal is to get something like
Triplestore.find.where()
.isNotNull("owningAdmin")
.is("is_public", true)
.findList();
Is this even possible or do I have to use JPQL? What would that Query look like?
Thanks in advance,
Hagen

You can use the and() method:
Triplestore.find.where().and(Expr.isNotNull("owningAdmin"),Expr.eq("is_public", true)).findList();
Don't forget to add the com.avaje.ebean.Expr import.

In Hibernate Criteria:
List<Triplestore> triplestores =
(List<Triplestore>) session.createCriteria(Triplestore.class)
.add( Restrictions.isNotNull("owning_admin_id") )
.add( Restrictions.eq("is_public", Boolean.TRUE) ).list();
Regards,

You said "something I can use inside play!", so I'm assuming any third party library is OK. Here's a solution using jOOQ (disclaimer: I work for the vendor).
I'm assuming you're using jOOQ's code generator here:
Result<TriplestoreRecord> result =
ctx.selectFrom(TRIPLESTORE)
.where(TRIPLESTORE.OWNING_ADMIN_ID.isNotNull())
.and(TRIPLESTORE.IS_PUBLIC)
.fetch();

Related

Using SQLExpression from QueryDsl

I want to use the SQLExpressions from QueryDsl. I have a Q-object called qMyClass. Now I want to use the listagg function of the oracle database. Therefore i want to initialize a WithinGroup-object.
WithinGroup<Object>.OrderBy withinGroup = SQLExpressions.listagg(qMyClass.attributeName, "/").withinGroup().orderBy(qMyClass.attributeName);
The first part
SQLExpressions.listagg(qMyClass.attributeName, "/")
already gives me the error:
unknown operation with operator LISTAGG and args
[myClass.attributeName, /]
Does anyone know how to use listagg? Didn't find any helpful information on the web. I am using version 4.0.2 of QueryDsl. Thanks!
Try something like this:
select(
qMyClass.id,
SQLExpressions.listagg(qMyClass.attributeName,"/")
.withinGroup()
.orderBy(qMyClass.attributeName.asc())
.getValue()
.as("foo"))
.from(qMyClass)
.groupBy(qMyClass.id);

JPA CriteriaBuilder value like column

I'm trying to create a query using CriteriaBuilder where I need to have a predicate where the value of the predicate is like the value in the database.
Basically, I need to be able to do the following:
WHERE myTestValue LIKE columnValue
In native queries, it is an option to do that, but using the CriteriaBuilder or NamedQueries, it does not seem to work.
String myValue = "foo#bar.com";
cb.where(cb.like(myValue, root.get(Entity_.email));
Is there an option in JPA to do it like this? Or should I fall back to native queries?
EDIT
I need to be able to check if a given value matches a wildcard entry in database. So the database has a record %#bar.com%, and I need to check if my given value foo#bar.com matches to that record.
I think your params should be other way round:
cb.where(cb.like(root.get(Entity_.email),myValue);
Aditionally you may need to use add this to the second param:
cb.where(cb.like(root.get(Entity_.email),"%"+myValue+"%");
Chris found the answer. First I need to "generate" a parameter.
ParameterExpression<String> senderEmailParameter = cb.parameter(String.class, "senderEmailParameter");
Path<String> senderEmailPath = root.get(Entity_.senderEmail);
Predicate predEmail = cb.like(senderEmailParameter, senderEmailPath);
And then I need to fill the parameter in the query.
q.where(predEmail);
return em.createQuery(q).setParameter("senderEmailParameter", senderEmail).getSingleResult();
This works! Thanks Chris!

Java QueryDsl for "update myTable where myColumn in ('interesting', 'values')"?

I'm trying to translate this query in QueryDsl:
update myThings set firstColumn = 'newValue' where secondColumn in ('interesting', 'stuff')
I spent hours looking for documentation but the java fu is just not strong enough in this one... :( I can find all kinds of QueryDsl example, but I cant find any for this. I will probably need SimpleExpression.eqAny(CollectionExpression), but I can't figure out how to build such a CollectionExpression around my simple list of strings.
List<String> interestingValues = Arrays.asList("interesting", "stuff");
queryFactory.update(myThings)
.set(myThings.firstColumn, "newValue")
// .where(myThings.secondColumn.in(interestingValues)
// 'in' will probably try to look in table "interestingValues"?
// .where(myThings.secondColumn.eqAny(interestingValues)
// 'eqAny' seems interesting, but doesn't accept a list
.execute();
All I can find is API definitions, but then I get lost in generics any other "new" java concepts which I still have trouble understanding. An example would be very much appreciated.
You have to use new JPAUpdateClause(session, myThings):
JPAUpdateClause<myThings> update = new JPAUpdateClause(session, myThings);
update.set(myThings.firstColumn, "newValue")
.where(myThings.secondColumn.in(interestingValues))
.execute();
If you are using hibernate, use HibernateUpdateClause() instead;

hibernate criteria api how to get Path from fetched records?

I have a query written in criteria api that goes like this:
CriteriaQuery<Application> query = builder.get().createQuery(Application.class).distinct(true);
Root<Application> root = query.from(Application.class);
root.fetch(Application_.answerSets, JoinType.LEFT);
I need to get Path for answerSet.createDate field, but have no idea how to achieve it. I need it to make a query for applications where answer sets are older than X days.
Try query.add(Restrictions.eq("answerSet.createDate", YOURVALUE)). However I am not if createQuery is the same as createCriteria.

jOOQ Update query with Where clause

How to add Where clause with Update query in jOOQ?
AccountPaymentRecord aacntPaymentRec = new AccountPaymentRecord();
aacntPaymentRec.setReceiptNumber(PaymentNumberFrom);
aacntPaymentRec.setPaymentComment(ReasonFrom);
transfeeTransfer.update(aacntPaymentRec);
I have to add Where clause as well. How to do it?
Since you're operating on UpdatableRecord, you might want to follow what's documented here, in the manual. Another place to look for information are the jOOQ manual's sections about the UPDATE statement.
A possible solution for you:
With the code from which you have started, one possible solution would be to use DSLContext.executeUpdate(R, Condition):
AccountPaymentRecord aacntPaymentRec = new AccountPaymentRecord();
aacntPaymentRec.setReceiptNumber(PaymentNumberFrom);
aacntPaymentRec.setPaymentComment(ReasonFrom);
DSL.using(configuration)
.executeUpdate(aacntPaymentRec, ACCOUNT_PAYMENT.ID.eq(123));
Thanks #Lukas in my case i have to use like this
AccountPaymentRecord aacntPaymentRec = transfeeTransfer.fetchOne(AccountPayment.ACCOUNT_PAYMENT,
AccountPayment.ACCOUNT_PAYMENT.PAYMENT_NUMBER.eq(PaymentNumberTo));
aacntPaymentRec.setReceiptNumber(PaymentNumberFrom);
aacntPaymentRec.setPaymentComment(ReasonFrom);
aacntPaymentRec.update();

Categories

Resources