jdbcTemplate and Oracle 10 - jdbctemplate

Trying to do an insert, I have:
jdbcTemplate.update("insert into....", new Object[]{foo.getId(), foo.getName()})
foo.getId() returns a long, and getName() a String.
I have "NUMBER" as the id type in Oracle, and varchar2 for the name field.
I'm getting SQLtype unknown problem.
the update method has a version where I do not have to put in the SQL types, but do I have to, and if so, how?

I'm assuming you mean the Spring Framework JdbcTemplate class. The JdbcTemplate methods will attempt to guess the java.sql.Type for value references, but apparently isn't guessing correctly in this case.
There are a couple of ways to include the type:
The JdbcTemplate.update(String, Object[]) [javadoc](http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/JdbcTemplate.html#update(java.lang.String, java.lang.Object[])) indicates that you can pass SqlParameterValue instances, consisting of the java.sql.Type and a value.
Alternatively, you can use JdbcTemplate.update(String, Object[], int[]) passing an array of java.sql.Type

Related

Passing string list to F# asp.Net api

I actually solved my problem before posting, but I wonder if there are any better solutions?
Also if there is somewhere where there is a way to use list as-is?
I am writing a simple get endpoint if F# which needs to accept a list of strings as an argument.
I take that list as the input to a query that runs as expected, I am not worried about that part.
The problem I am facing is as follows (minimal implmenetation):
When I define the endpoint as:
[<HttpGet>]
member _.Get() =
processStrings [ "test"; "test2" ]
it returns as expected.
When I change it to:
[<HttpGet>]
member _.Get([<FromQuery>] stringList: string list) = processStrings stringList
I get an error:
InvalidOperationException: Could not create an instance of type 'Microsoft.FSharp.Collections.FSharpList`1[[System.String, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Record types must have a single primary constructor. Alternatively, give the 'stringList' parameter a non-null default value.
Which doesn't make much sense to me, as I am using a list of strings, which in C# at least defaults to an empty list.
So I assume this comes down to how C# and F# interpret these signatures, but I am not sure how to resolve it.
I tried this signature and received the same error as above....
member _.Get( [<Optional; DefaultParameterValue([||]); FromQuery>] stringList: string list) = processStrings stringList
In the end using the following did solve the problem.
member _.Get( [<Optional; DefaultParameterValue([||]); FromQuery>] stringList: string seq) = processStrings stringList
I assume it was solved because seq is just IEnumerable, and then presumable list isn't just List from C# (mutable vs immutable). But is there a way to use an F# list in [FromQuery] parameters? Would [FromBody] have just worked? (No is the tested answer) Is there a way to add a type provider for an endpoint like this?
Is there something else I am missing here? Mine works now, but I am curious to the implications of the above.
I have not tested this, but I would assume that ASP.NET does not support F# lists as arguments. I would guess that taking the argument as an array would be much more likely to work:
[<HttpGet>]
member _.Get([<FromQuery>] stringList: string[]) =
processStrings (List.ofArray stringList)

Kusto nested json coming as null

I am using Application Insights Log-Explorer query window to visualize the below query.
Inside the field customDimensions.RemotePC I am string a json payload.
When I try to index the stored json via propery-indexing, i get the value as null. I tried to access it as array that turns null as well.
Could you please help me to access the FirstName property in below diagram.
Try this:
| extend todynamic(tostring(rpc)).FirstName
I believe that the issue is that rpc is string (although it looks as json). Thus you need to "cast" it to dynamic. You first need to tell the compiler though that this is a string value.

Unable to save aggregate root with UUID id in sqlite

In my project I have the following entity which can be successfully saved in Postgres:
public class Aggregate {
#Id
private UUID id;
private JsonNode data;
// getters and setters omitted
}
When I try to save the same entity in SQLite I get the following exception:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer] to type [java.util.UUID]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:322) ~[spring-core-5.3.5.jar:5.3.5]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195) ~[spring-core-5.3.5.jar:5.3.5]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175) ~[spring-core-5.3.5.jar:5.3.5]
at org.springframework.data.mapping.model.ConvertingPropertyAccessor.convertIfNecessary(ConvertingPropertyAccessor.java:120) ~[spring-data-commons-2.4.6.jar:2.4.6]
at org.springframework.data.mapping.model.ConvertingPropertyAccessor.setProperty(ConvertingPropertyAccessor.java:63) ~[spring-data-commons-2.4.6.jar:2.4.6]
at org.springframework.data.jdbc.core.JdbcAggregateChangeExecutionContext.setIdAndCascadingProperties(JdbcAggregateChangeExecutionContext.java:337) ~[spring-data-jdbc-2.1.6.jar:2.1.6]
at org.springframework.data.jdbc.core.JdbcAggregateChangeExecutionContext.populateIdsIfNecessary(JdbcAggregateChangeExecutionContext.java:305) ~[spring-data-jdbc-2.1.6.jar:2.1.6]
at org.springframework.data.jdbc.core.AggregateChangeExecutor.execute(AggregateChangeExecutor.java:52) ~[spring-data-jdbc-2.1.6.jar:2.1.6]
at org.springframework.data.jdbc.core.JdbcAggregateTemplate.store(JdbcAggregateTemplate.java:339) ~[spring-data-jdbc-2.1.6.jar:2.1.6]
at org.springframework.data.jdbc.core.JdbcAggregateTemplate.save(JdbcAggregateTemplate.java:149) ~[spring-data-jdbc-2.1.6.jar:2.1.6]
at org.springframework.data.jdbc.repository.support.SimpleJdbcRepository.save(SimpleJdbcRepository.java:60) ~[spring-data-jdbc-2.1.6.jar:2.1.6]
Table definition for SQLite:
create table aggregate
(
id text primary key,
data text not null
);
For some reason Spring Data JDBC tries to set an id of type integer in the aggregate although it's a) of another type and b) already there. I experimented with a different IdGenerator, using without rowid in the table definiton but nothing changed.
I'm using Spring Data JDBC 2.1.6 and xerial/sqlite-jdbc 3.34.0.
How can I fix this?
Update to answer Jens Schauder's question: I forgot to mention the callback that sets the UUID appropriately. I also have two converters to translate between JsonNode and PGobject (not shown here).
#Component
public class AggregateCallback implements BeforeConvertCallback<Aggregate> {
#Override
public Aggregate onBeforeConvert(final Aggregate aggregate) {
if (aggregate.getId() == null) {
aggregate.setId(UUID.randomUUID());
}
return aggregate;
}
}
Regardless of the database the AggregateCallback is called, but with SQLite the exception is thrown after the execution of the callback.
As #tammOr answered using a release after Spring Data 2021.0.0-M5 fixes the problem.
Here is the explanation why and how.
When Spring Data JDBC saves the aggregate it does the following in the old version:
It determines if the aggregate is new. There are various ways how it might do that, but the case that applies here is to check the id, if it is empty (null for object types or 0 for numeric primitives.
Since the id is null it decides the aggregate is new and an INSERT is necessary. Before performing that it triggers some events which #tammOr uses to set the id.
It then performs the Insert
Since the default strategy is to generate the id in the database, it tries to obtain the generated id from the JDBC driver after the insert. For some reason the SQLite driver actually returns a value for that, of type Integer.
Spring Data JDBC then tries to convert that to an UUID and fails.
With the later version Spring Data JDBC realises it already has an ID in step 4 and does not ask the driver for a generated one, and skips step 5, so everything is happy.
This is the pull request that solved the problem: https://github.com/spring-projects/spring-data-jdbc/pull/939
Switching to Spring Data 2021.0.0-M5 as suggested by Jens Schauder in the comments solved the problem!

Doctrine getArrayResult() returns database value while entity getter processes value

For example:
public function getField() {
return ucfirst($this->field);
}
Given that an entity has getters that do some changes on the database value before returning it, how can those changes also be applied when using the getArrayResult() method ?
For example, Laravel has accessors (http://laravel.com/docs/5.0/eloquent#accessors-and-mutators). The entity getter can be used in the same way.
When using getArrayResult(), the value for the "field" will not have the first character capitalized.
Thank you!
Well, it's the same behaviour as laravel almost :)
Take a look at Hydrators
.
Hydrators are the processors that bind your raw db output to various data types in doctrine. Thus you have Doctrine_Core::HYDRATE_RECORD which is the standard hydrator(aka the thing called when you use $query->getResult()).
If you use $query->getArrayResult(), it uses the Doctrine_Core::HYDRATE_ARRAY Hydrator.
If you need a more detailed description, please let me know.

How to call stored procedure in Entity Framework?

I'm using ASP.net Entity Framework. So I need to call a stored procedure and want to set a data to DateSet.
This is my function
public DataSet SearchEmployee(string name, string dep)
{
db.f_t_PEOPLE_SearchEmployee(name, dep);
return db.f_t_PEOPLE_SearchEmployee(name, dep);
}
but there is error and it says
Cannot implicitly convert type 'object' to 'System.Data.DataSet'. An explicit conversion exists (are you missing a cast?)
Entity Framework is a ORM. This means that EF maps the data retrieved from BD to classes (objects) wich represents your business entities. I dont know if with some arcane voodoo programming haks you can read a datatable from a SP mapped by EF but what I am sure is that you shouldn't do it. EF was building to avoid DataTables and DataSets.

Resources