Hibernate - Extract parameter values from SQL query - java

I have a class that populates a Hibernate SQL query's parameters with different values depending on some input criteria. I would like to write tests for the resulting query. As of yet, I have not found a way to get the parameter values that were set on a query. I looked at the resulting Query implementation (in this case, SQLQueryImpl) and there is a method called getParameterMetadata() that seems like it might give me something, but I haven't been able to find anything.
I would like to be able to do something like:
assertEquals("some value", query.getParameterValue(parameterName));
Is there a way to do this?
EDIT:
I just looked a little more, and I found a
query.getQueryParameters(map).getNamedParameters()
which returns a Map. I have to assume that the map contains the parameters and their values. Correct me if I'm wrong.

I actually didn't consider the fact that because I'm testing, I can simply mock the query and expect specific parameters.

Related

SpringData - Get single column querying by method's name

I created a method which through spring data automatically create the query. The problem is about the return param, because dismatch from the name definition.
In fact by specifying only one parameter, it return 4 parameters.
The springData method is that:
Optional<Comunicazioni> getCommIDByExtIDAndCommSAndCommT(
BigDecimal extID, String commS, String commT);
and I access to the type like so:
getCommIDByExtIDAndCommSAndCommT(extId, commS, commT).get().getCommID()
how can I retrieve only the column I need?
Thank you
Unfortunately, this is not possible with the current implementation of Spring Data JPA (i.e. Using method name only).
Instead, the current solution is to use #Query to define the return values. You can find an example of that here.
However, if your Entity object is not too large, you would be able to achieve the result in the example you posted by simply retrieving the entire entity:
Optional<Comunicazioni> findByExtIDAndCommSAndCommT(BigDecimal extID, String commS, String commT);
and then calling it as
repo.findByExtIDAndCommSAndCommT(extId, commS, commT).get().getCommID();
It would be nice to see this functionality in the future, but for now, it is not so difficult to work around.

Can I add condition with spring data standard interface methods

Can I add condition to provided interface method like "findAll" of spring data? For example, if the table has columns "name" and "deleted" - I can create a query asfindByNameAndDeletedIsNull which will give all names which have not been deleted. I tried "findAllAndDeletedIsNull" but this does not work - is this possible?
I know it can be achieved with #query, but was curios as how can we augment the standard methods with conditions.
You can do "findByDeletedIsNull"
See here for more info on what key words can be used in a method name.
According to the documentation, there's a specific way to name the method to have it parsed into the correct SQL. Look at this section: Spring Data JPA reference - query.
If you have the query ready, you can easily reverse-engineer which method name may help you get the same result. You can compose quite complex queries by building up from those foundation bricks. However, you cannot just create a string representation of the "where" clause as a named method.
From the document, if you want to get all records where a field is null:
IsNull | findByAgeIsNull | … where x.age is null
will mean a method name such as findByDeletedIsNull.

JOOQ - convert fetchOne().into() to fetchValue(query)

In this answer, the author mentions that to avoid NPE the fetchValue(query) method can be used. The problem is that how exactly can the OP's code be converted into a query? I have similar code, pasted below, and would like to turn it into a query also.
return jooqDSLContext.select()
.from(CL_LOGIN)
.join(CL_USERS)
.on(CL_LOGIN.CL_USER_ID.eq(CL_USERS.CL_USER_ID))
.where(CL_USERS.EMAIL1.eq(email))
.fetchOne().into(CL_LOGIN);
JOOQ is very powerful and has many capabilities, but unfortunately everything I have tried to make a standalone query object with a join does not even compile.
EDIT: The answer provided did help me side-step the need to have a query object. But for those that want to know how to get a query object you can use the getQuery() method... see example below.
SelectQuery<Record1<String>> query = jooqDSLContext.select(USER_LOGIN.ACCOUNT_STATUS)
.from(USER_LOGIN)
.where(USER_LOGIN.USER_ID.eq(userId))
.getQuery();
Observe the signature of the method DSLContext.fetchValue(ResultQuery<R>), where R extends Record1<T>. This means that the expected row type of the query is Record1<T> with any arbitrary <T> type. In other words, you must project exactly one column in your SELECT clause.
You seem to want to project the entire record of type CL_LOGIN, so fetchValue() is not applicable to your use-case.
But note, there's also ResultQuery.fetchOneInto(Table), which is a convenience method wrapping that null check and the into() call. So, just write:
return jooqDSLContext.select()
.from(CL_LOGIN)
.join(CL_USERS)
.on(CL_LOGIN.CL_USER_ID.eq(CL_USERS.CL_USER_ID))
.where(CL_USERS.EMAIL1.eq(email))
.fetchOneInto(CL_LOGIN);

How to handle different data types when creating a dynamic mongodb query in Mule

I am looking to create a dynamic mongodb query in Mule and have modified the Java Transformer code from this post to work with MongoDB: Mule-Creating dynamic where condition for sql query through DB connector
My query is what is the best way to handle different data types coming in as query parameters for the WHERE clause e.g. a string will have '' and boolean will be without quotes.
I am thinking that I will need to add an if statement which determines whether to use quotes or not based on the field names.
I just wanted to know if there is a better way because it feels like I am hard coding the values which is something I try to avoid.
Thanks
You would be passing a Document type to find() method anyway right and you might already be using <mongo:query-attribute>
you can specify bson types for each of your query attribute. An example is shown below.
<mongo:query-attribute key="_id">
#[new org.bson.types.ObjectId('4c55576a5a42d6606cfa8267')]
</mongo:query-attribute>
you can get a full list of bson types here https://docs.mongodb.com/manual/reference/bson-types/

How to group a result set into a list from SQL statement?

Suppose I want I have a SQL like select * from game;, but I want only one SQL to execute to group different games result into Java List.
Say we have game1, game2, game3 in the content of column 'game' in the resultset, so I need a List whose length is 3 and each element inside is a SQL ResultSet(if I can have a OR Mapping, that will be great) so that I can parse them later.
SpringFramework contains a class called JdbcTemplate which contains a method called query, this is will take a SQL statement with relevant parameters and return a List with your results but it will require some work to set it up. I think you're looking for something a bit more 'out of the box' which just does it in a single line, but I don't think this is possible. There are other methods in the class which do the same thing but take different parameters based on what you have available in your code.
See the JdbcTemplate api for more details if you're interested.

Categories