Hibernate persists Java 8 DateTimeLocal as VARBINARY in SQL Server - sql-server

I'm trying to make use of the Java 8 date & time API in a new application which is backed by SQL Server database. All the resources on the Internet state that Hibernate 5.2+ support this API and Java 8 out of the box and suggest a simple approach as:
#Entity
#Table(name="...", schema="...")
public class DepositDetails {
// ... id, other fields ...
private LocalDateTime createdOn;
// ... other fields, getters/setters ...
}
In my case, however, the column which is created in the database (using hibernate-jpamodelgen), is created as VARBINARY(255) instead of DATETIME/DATETIME2.
I've also tried specifying the datatype explicitly (columnDefinition = "DATETIME") - in this case the column type is created correctly as specified, but when I try to persist data in the table, I get an exception stating that varbinary data cannot be converted to datetime...
Some more details about the application setup that may be related to the issue:
Hibernate is 5.2.12.Final
SQL Server is Microsoft SQL Server Developer Edition (64-bit) 14.0.900.75 (in a Docker container)
MSSQL JDBC driver is 6.2.2.jre8

The problem was, as suggested by #Alan Hay, that Hibernate 5.2 was replaced in the dependency tree by Hibernate 5.0, which is used by default by Spring Boot.
Moreover, I had a property in my child pom.xml which was set to 5.2.12.Final, but on a parent level the same property was set (again by Spring Boot) to 5.0.x. In this way the parent application, which should have taken the 5.2 dependency from the child module, was actually taking the old version from Spring Boot.
The solution was to set the property in the parent pom.xml.

Related

Intellij embedded H2 database tables do not appear

I'm creating a Spring Boot application and I'm using Intellij's embedded h2 database.
I have added the following lines in my application.properties file:
spring.datasource.url=jdbc:h2:~/testdb;MV_STORE=false;AUTO_SERVER=TRUE
This is my data source configuration
Although the connection is successful and I can query the database using Intellij's query console, the tables do not appear in the Database tab.
Succeeded
DBMS: H2 (ver. 2.1.210 (2022-01-17))
Case sensitivity: plain=upper, delimited=exact
Driver: H2 JDBC Driver (ver. 2.1.210 (2022-01-17), JDBC4.2)
Ping: 16 ms
When I refresh the connection or go to the schemas tab of the data source configuration, I get the following error:
[42S02][42102] org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "INFORMATION_SCHEMA_CATALOG_NAME" not found; SQL statement: select CATALOG_NAME from INFORMATION_SCHEMA.INFORMATION_SCHEMA_CATALOG_NAME [42102-210].
By going to the advanced tab of the data source and clicking on expert options, we are presented with a checkbox labeled "Introspect using JDBC metadata"
By checking that box, the tables successfully appear in the Database tab
Regarding why this works, this is taken from the official documentation:
https://www.jetbrains.com/help/datagrip/data-sources-and-drivers-dialog.html
Introspect using JDBC metadata
Switch to the JDBC-based introspector. Available for all the databases.
To retrieve information about database objects (DB metadata), DataGrip uses the following introspectors:
A native introspector (might be unavailable for certain DBMS). The native introspector uses DBMS-specific tables and views as a source of metadata. It can retrieve DBMS-specific details and produce a more precise picture of database objects.
A JDBC-based introspector (available for all the DBMS). The JDBC-based introspector uses the metadata provided by the JDBC driver. It can retrieve only standard information about database objects and their properties.
Consider using the JDBC-based intorspector when the native introspector fails or is not available.
The native introspector can fail, when your database server version is older than the minimum version supported by DataGrip.
You can try to switch to the JDBC-based introspector to fix problems with retrieving the database structure information from your database. For example, when the schemas that exist in your database or database objects below the schema level are not shown in the Database tool window.

Connection string Error when trying to make Installation

I made EntityFramework (6) CodeFirst WinForms application
On my machine it works fine but when I'm trying to make Installation (with VS 2015 Installing projects) of my project and to run it on another machine, I get Expansion of Data Directory failed
I'm really dont understand- when I'm trying to change db name in ConnectionString to some wrong name- it still works.
My connection string in app config:
I found what was the problem. It is not enough to pass the name of connection string via Entity context constructor :base(ConnectionString). Entity context name MUST be the same as Connection string in app config. Other way EF create some another db.

Can I use SignalR with SQL Server Backplace on an existing entity code first database?

According to Scaleout with SQL Server you can use SignalR.SqlServer to keep SignalR synced on a load balancing setup. I have an existing MVC 5 website with its own database created using Entity Code First. The article seems to use a dedicated database with service broker enabled and says not to modify the database.
So do I need to have a separate database for this? I know Entity can be picky if the database schema doesn't match and I worry that if I try to use the SignalR Sql Server package with the existing database that the tables it creates will cause a context changed error.
Also can someone provide me with more information about using the Microsoft.AspNet.SignalR.SqlServer package. The article I linked doesn't give a ton of detail and I don't know if I need to change anything in my hub and groups or if it is all handled automatically.
You should be able to, though you'd likely want to separate your entity framework definitions from signalR. You can either put SignalR in a separate database, or give the two a separate schema.
In terms of configuration, you'll need to make an addition to the Startup class of your web project:
public class Startup
{
public void Configuration(IAppBuilder app)
{
var sqlConnectionString = "connection string here";
GlobalHost.DependencyResolver.UseSqlServer(sqlConnectionString);
this.ConfigureAuth(app);
app.MapSignalR();
}
}

SimpleMembership with SQL Server Express database

I have recently discovered that ASP.NET MVC 4 uses SimpleMembership rather than the traditional ASP.NET Membership provider.
I am looking to use SimpleMembership but I also wish to use an Sql Server express database (I will be using a 'real' Sql Server db later on).
I am used to using aspnet_regsql to setup my sql server databases for application services and therefore inserting the necessary membership tables.
However, this application will not setup a DB to work with SimpleMembership.
Can anyone tell me how to do this?
You will need to comment out the WebSecurity.InitializeDatabaseConnection line of code (otherwise the first time you reach the login form the application will attempt to create the simple membership tables on the defined web.config connection string).
Then you will need to update your account controller code since its was written for simpleMembership use to reflect your chosen membership provider.
Recently I did this by creating an MVC3 project then upgrading it to MVC4 (read the how to upgrade mvc3 to mvc4 section). Not the cleanest but it did prevent me from having to update the account controllers code.
I would recommend to look into InitializeSimpleMembershipAttribute, which is in the folder "Filters"
There is a command to initialize the database. It checks itself, if the tables are already created.
WebSecurity.InitializeDatabaseConnection("connStringName", "UserProfile", "UserId", "UserName", autoCreateTables: true);
The tables are:
UserProfile => in my case, because of the initializer
webpages_OAuthMembership
webpages_Membership
webpages_OAuthToken
webpages_Roles
webpages_UsersInRoles
You can check out the source-code of the aspnetwebstack project

how to display the database name and change the database properties in java (spring framework)

I have a requirement to display the database name on the screen that is connected by the web application (which is configured through the datasource on weblogic server with spring data xml having all the configurations) and also is there any way to switch from current database to different datasource (database server) while working/running on the web application (with user screen).
Thanks.
As far as I know, there is no straight-forward way to know this info. One way I can think of doing this is to parse the spring config XML file using an XML parser for the desired element - even that too will only give you the JNDI name of the datasource your app would be using. I hope you have a mechanism to determine which JDBC JNDI name maps to which database. If you don't have that information, you would have to use JMX (MBeans) to connect to the Weblogic environment to get that info.

Resources