How to Configure Hibernate Config File for SQL Server

how to configure hibernate config file for sql server

Properties that are database specific are:

  • hibernate.connection.driver_class: JDBC driver class
  • hibernate.connection.url: JDBC URL
  • hibernate.connection.username: database user
  • hibernate.connection.password: database password
  • hibernate.dialect: The class name of a Hibernate org.hibernate.dialect.Dialect which allows Hibernate to generate SQL optimized for a particular relational database.

To change the database, you must:

  1. Provide an appropriate JDBC driver for the database on the class path,
  2. Change the JDBC properties (driver, url, user, password)
  3. Change the Dialect used by Hibernate to talk to the database

There are two drivers to connect to SQL Server; the open source jTDS and the Microsoft one. The driver class and the JDBC URL depend on which one you use.

With the jTDS driver

The driver class name is net.sourceforge.jtds.jdbc.Driver.

The URL format for sqlserver is:

 jdbc:jtds:sqlserver://<server>[:<port>][/<database>][;<property>=<value>[;...]]

So the Hibernate configuration would look like (note that you can skip the hibernate. prefix in the properties):

<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://<server>[:<port>][/<database>]</property>
<property name="connection.username">sa</property>
<property name="connection.password">lal</property>

<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

...
</session-factory>
</hibernate-configuration>

With Microsoft SQL Server JDBC 3.0:

The driver class name is com.microsoft.sqlserver.jdbc.SQLServerDriver.

The URL format is:

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

So the Hibernate configuration would look like:

<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://[serverName[\instanceName][:portNumber]];databaseName=<databaseName></property>
<property name="connection.username">sa</property>
<property name="connection.password">lal</property>

<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

...
</session-factory>
</hibernate-configuration>

References

  • Hibernate Core Reference Documentation

    • 3.3. JDBC connections
    • 3.4. Optional configuration properties
  • jTDS Documentation
  • Microsoft SQL Server JDBC Driver 3.0 Documentation
  • Microsoft SQL Server JDBC Driver 2.0
  • Support Matrix for Microsoft SQL Server JDBC Driver

Hibernate Configuration for SQLServer - Driver class

Try changing this:

hb_props.put("hibernate.connection.driver.class",
"com.microsoft.sqlserver.jdbc.SQLServerDriver");

To this:

hb_props.put("hibernate.connection.driver_class",
"com.microsoft.sqlserver.jdbc.SQLServerDriver");

This configuration guide from JBoss uses an _ instead of a .

How to connect with hibernate file to SQL server database?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database Connection settings -->
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Test;integratedSecurity=true</property>
<property name="hibernate.connection.username">LAPTOP-SJJ2BT5V\Konrad</property>
<property name="hibernate.connection.password"></property>

<!-- Echo SQL wyswietlenie zapytania tabeli bazy danych -->
<property name="hibernate.show_sql">true</property>

<!-- SQL Dialect implementacja silnika bazo danego -->
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>

<!-- database intialize -->
<property name="hibernate.hbm2ddl.auto">update</property>

<!-- mappings -->
<mapping class="pl.edu.wszib.model.Vehicle" />
<mapping class="pl.edu.wszib.model.User" />
<mapping class="pl.edu.wszib.model.GroupVehicle" />
<mapping class="pl.edu.wszib.model.Rent" />
<mapping class="pl.edu.wszib.model.Buy" />
</session-factory>
</hibernate-configuration>

and i must download sqljdbc_auth.dll and put this file to C:\Program Files\Java\jdk-11.0.1\bin

Connect to SQL Server 2017 using hibernate

I fixed it by changing the url.

<property name="javax.persistence.jdbc.url"
value="jdbc:sqlserver://xxx.xx.xx.xx;databaseName=processos_corretivos"/>


Related Topics



Leave a reply



Submit