I am trying to write some basic MongoDB custom queries in Kotlin.
I've seen nearly everywhere that people use a method addCriteria:
val query: Query = Query()
query.addCriteria(Criteria.where("field1").exists(true)))
But it seems there is no method addCriteria in Query which I use: org.springframework.data.mongodb.repository.Query
I am very confused. Cannot find any explanations on how to write custom MongoDB queries in Kotlin with Spring Data except using this method.
So, I was very close to the problem's solution. I really used wrong Query, despite it's path looks really logical. I needed this one: org.springframework.data.mongodb.core.query.Query.
I used org.springframework.data.mongodb.repository.Query, which is Query annotation, not the class
Related
I am learning about higher order functions[HOF] and lambda in Kotlin.
I checked the Kotlin docs but didn't understand it, I found one benefit of HOF:
You can perform any operations on functions that are possible for other non-function values.
so, What are 'non-functional values'?
and What are those 'operations'?
In a higher order function if a lambda is taking two parameters and returning a value, then can't we just use a function for it?
and what is the real scenario when we have return a function?
I have seen real programs in Kotlin, but I haven't seen any use of lambda or HOF in them.
I want to understand why, else many of the features would just go unused.
It's just a part of the Kotlin syntax that makes it more concise and understandable.
For example, try to imagine this code without using lambdas and HOF like map, filter etc:
val sum = listOfInts.filter{it % 2 == 0}.map{it*it}.sumOf{it % 10}
Type-safe builders is a cool thing too.
These functions are widely used in many libraries and frameworks, like Compose by Google. The first thing I remembered - State hoisting pattern.
I am trying to form below final kotlin code
val participants: List<AbstractParty>
I tried to use below code in kotlinpoet but it shows error, I think it is not correct, but don't know how should I fix it. Any one can help? Thanks.
PropertySpec.builder("participants", List<ClassName("AbstractParty">)
Depending on whether you have a reference to a class or if you need to create its name from Strings, you can do either this:
PropertySpec.builder("participants",
ParameterizedTypeName.get(List::class, AbstractParty::class)
).build()
Or this:
PropertySpec.builder("participants",
ParameterizedTypeName.get(
List::class.asClassName(),
ClassName("some.pckg.name", "AbstractParty"))
).build()
A hint to finding out these sorts of things: KotlinPoet has pretty extensive tests, you can find examples of almost anything in there.
You can use parameterizedBy() extension:
PropertySpec.builder(
"participants",
List::class.asClassName().parameterizedBy(ClassName("some.pckg.name", "AbstractParty")
).build()
https://square.github.io/kotlinpoet/1.x/kotlinpoet/kotlinpoet/com.squareup.kotlinpoet/-parameterized-type-name/-companion/parameterized-by.html
I have a page where I have implemented an af:query and a result table. The problem is that I can't retrieve within a managed bean method the exact query that is used at runtime by this component.
Is there any way to get the query string that is being executed within the af:query component?
Thanks
I found the answer here https://community.oracle.com/thread/3516102 I have to override the executeQueryForCollection(), and, before super call, I can print out the result of getQuery() method.
The detailed solution is explained here: Get SQL Query and Parameters from af:search
I'm writing a simple student scores manager for practicing programming on PyQt (I don't want to use terrible Visual Basic anymore). But I had a big problem on choose data models.
I found QSqlTableModel first, it is a great model with auto-updating. The trouble is, I need to use a lot of SQL (JOIN, WHERE) to select data from database. QSqlTableModel has select() and filter() only.
Then I found QSqlQueryModel, but it is read only. So I rewrite its setData() method. So it is read-write now. Unfortunately, QSqlQueryModel less usable features than QSqlTableModel.
As you see, if I can using SQL with QSqlTableModel, I can resolve all my problems.
So...?
QSqlTableModel has the setQuery method, which you can use to set a custom query, something like:
model = QSqlTableModel()
query = QSqlQuery(your_query)
model.setQuery(query)
However, the Qt documentation states:
This function simply calls QSqlQueryModel::setQuery(query). You should normally not call it on a QSqlTableModel. Instead, use setTable(), setSort(), setFilter(), etc., to set up the query.
I've seen this opportunity reported at least half a dozen times with about as many responses.
My problem is, I've got a MySQL database function defined, we'll call it "my_func(int val) returns int", which works fine if I test directly on the database.
I've also gotten it to work with a direct SQL passthrough my repository implementation, which is okay, but I'd rather route it through Hql, for some god-awful reason...
So... I've got a MySQL5Dialect setup to register the function and I'm having some difficulty parsing through the expected conventions.
My understanding is that I need to prefix the function name with "dbo." at some point during the function registration?
Something like this,
//...
RegisterFunction("my_func", new SQLFunctionTemplate(NHibernateUtil.Int32, "my_func(?1)"));
//...
And then through my repository,
var value = repository.FindByHQL<int>("select my_func(2)").Single();
Where FindByHQL returns an IList.
Any thoughts why this wouldn't work.
I'm running the latest WAMP (2.1e I think).
Enough info? Let me know if I can provide any further details.
Thanks,
Michael
select my_func(2)
is not valid HQL, regardless of whether the function is registered or not.
You can use SQL instead if that's your use case.
Post full exception with stack trace if it's not and this was just a simplified example.