jOOQ Update query with Where clause - java

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();

Related

Jooq update return value

I try to update rows in table and get results after.
If I do
dsl.update(TABLE)
.set(TABLE.ROW, newRow)
.where(TABLE.ROW_2.eq(
dsl.select(ANOTHER_TABLE.ID)
.from(ANOTHER_TABLE)
.where(ANOTHER_TABLE.GUID.eq(guid))
)).execute()
it returns 1. But if I do
dsl.update(TABLE)
.set(TABLE.ROW, newRow)
.where(TABLE.ROW_2.eq(
dsl.select(ANOTHER_TABLE.ID)
.from(ANOTHER_TABLE)
.where(ANOTHER_TABLE.GUID.eq(guid))
)).returningResult(TABLE.ROW_3).fetchOne()
it returns empty result. But I want to get TABLE.ROW_3 after update. Whats the problem?
Vertica doesn't support UPDATE .. RETURNING like PostgreSQL, as can be seen in the Vertica docs:
https://www.vertica.com/docs/9.2.x/HTML/Content/Authoring/SQLReferenceManual/Statements/UPDATE.htm
The jOOQ documentation of UpdateReturningStep::returningResult reflects this by not listing SQLDialect.VERTICA in its #Support annotation:
#Support({AURORA_POSTGRES,COCKROACHDB,DB2,FIREBIRD,ORACLE,POSTGRES,SQLSERVER})
There's currently no workaround for this. If you want to avoid using such API, you could use the jOOQ-checker module in your build to produce compilation errors whenever you use API that is not supported by VERTICA

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);

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;

IntelliJ IDEA code inspection: HQL custom dialect & registered functions

My question is about
using registered functions for date/time manipulations in Hibernate Query Language and
IntelliJ IDEA's code inspection for these registered functions in HQL.
I'm using Hibernate 4.2.5 with Java 7, and SQL Server 2008 R2 as the database, and IntelliJ IDEA 12.1.6.
In an HQL query I need to perform the TSQL DATEADD function - or the equivalent HQL date operation. This doesn't seem to exist.
Here's what I'd like to achieve:
update MyTable set startTime = GETDATE(), targetTime = DATEADD(HOUR, allocatedTime, GETDATE()), endTime = null where faultReport.faultReportId = :faultReportId and slaTypeId = :slaTypeId
Searching for answers online has been disappointingly no help, and the most common advice (like the comment seen here: https://stackoverflow.com/a/18150333/2753571) seems to be "don't use date manipulation in hql." I don't see how I can get around performing the operation in the SQL statement in the general case (e.g. when you want to update one column based on the value in another column in multiple rows).
In a similar fashion to the advice in this post: Date operations in HQL, I've subclassed a SQLServerDialect implementation and registered new functions:
registerFunction("get_date", new NoArgSQLFunction("GETDATE", StandardBasicTypes.TIMESTAMP)); // this function is a duplication of "current_timestamp" but is here for testing / illustration
registerFunction("add_hours", new VarArgsSQLFunction(TimestampType.INSTANCE, "DATEADD(HOUR,", ",", ")"));
and added this property to my persistence.xml:
<property name="hibernate.dialect" value="my.project.dialect.SqlServerDialectExtended" />
and then I'm testing with a simple (meaningless, admitted) query like this:
select x, get_date(), add_hours(1, get_date()) from MyTable x
The functions appear to be successfully registered, and that query seems to be working because the following SQL is generated and the results are correct:
select
faultrepor0_.FaultReportSLATrackingId as col_0_0_,
GETDATE() as col_1_0_,
DATEADD(HOUR,
1,
GETDATE()) as col_2_0_,
... etc.
But I now have this problem with IntelliJ IDEA: where get_date() is used in the HQL, the code inspection complains "<expression> expected, got ')'". This is marked as an error and the file is marked in red as a compilation failure.
Can someone can explain how to deal with this, please, or explain what a better approach is? Am I using the incorrect SQLFunction template (VarArgsSQLFunction)? If yes, which is the best one to use?
I'd like the usage of the registered function to not be marked as invalid in my IDE. Ideally, if someone can suggest a better way altogether than creating a new dialect subclass, that would be awesome.

Convert SQL Query to play Query Builder Syntax

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();

Categories

Resources