[java] how to configure hibernate config file for sql server

Here is the config file for MySQL:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">zgy01</property>
    <property name="hibernate.connection.pool_size">100</property>
    <property name="show_sql">false</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Mapping files -->
    <mapping resource="model.hbm.xml"/>

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

What to specify for SQL Server 2005? I did it like this:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
    <property name="hibernate.connection.url">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hibernate.connection.password">lal</property>
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

    <property name="hibernate.connection.pool_size">100</property>        
    <property name="show_sql">false</property>

    <!-- Mapping files -->
    <mapping resource="model.hbm.xml"/>

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

My question more precisely is how to specify the database that I have to connect to?

In MySQL I used to do like this:

<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property> 

This question is related to java sql-server hibernate orm hibernate.cfg.xml

The answer is


The connection URL should look like this for SQL Server:

jdbc:sqlserver://serverName[\instanceName][:port][;databaseName=your_db_name]

Examples:

jdbc:sqlserver://localhost
jdbc:sqlserver://127.0.0.1\INGESQL:1433;databaseName=datatest
...

We also need to mention default schema for SQSERVER: dbo

<property name="hibernate.default_schema">dbo</property>

Tested with hibernate 4


Keep the jar files under web-inf lib incase you included jar and it is not able to identify .

It worked in my case where everything was ok but it was not able to load the driver class.


Finally this is for Hibernate 5 in Tomcat.

Compiled all the answers from the above and added my tips which works like a charm for Hibernate 5 and SQL Server 2014.

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
   org.hibernate.dialect.SQLServerDialect
</property>
<property name="hibernate.connection.driver_class">
   com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="hibernate.connection.url">  
jdbc:sqlserver://localhost\ServerInstanceOrServerName:1433;databaseName=DATABASE_NAME 
</property>
<property name="hibernate.default_schema">theSchemaNameUsuallydbo</property>
<property name="hibernate.connection.username">
   YourUsername
</property>
<property name="hibernate.connection.password">
   YourPasswordForMSSQL
</property>

Don't forget to enable tcp/ip connections in SQL SERVER Configuration tools


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to sql-server

Passing multiple values for same variable in stored procedure SQL permissions for roles Count the Number of Tables in a SQL Server Database Visual Studio 2017 does not have Business Intelligence Integration Services/Projects ALTER TABLE DROP COLUMN failed because one or more objects access this column Create Local SQL Server database How to create temp table using Create statement in SQL Server? SQL Query Where Date = Today Minus 7 Days How do I pass a list as a parameter in a stored procedure? SQL Server date format yyyymmdd

Examples related to hibernate

Hibernate Error executing DDL via JDBC Statement How does spring.jpa.hibernate.ddl-auto property exactly work in Spring? Error creating bean with name 'entityManagerFactory' defined in class path resource : Invocation of init method failed JPA Hibernate Persistence exception [PersistenceUnit: default] Unable to build Hibernate SessionFactory Disable all Database related auto configuration in Spring Boot Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] HikariCP - connection is not available Hibernate-sequence doesn't exist How to find distinct rows with field in list using JPA and Spring? Spring Data JPA and Exists query

Examples related to orm

How to select specific columns in laravel eloquent Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] How to query between two dates using Laravel and Eloquent? Laravel - Eloquent "Has", "With", "WhereHas" - What do they mean? How to Make Laravel Eloquent "IN" Query? How to auto generate migrations with Sequelize CLI from Sequelize models? How to fix org.hibernate.LazyInitializationException - could not initialize proxy - no Session Select the first 10 rows - Laravel Eloquent How to make join queries using Sequelize on Node.js What is Persistence Context?

Examples related to hibernate.cfg.xml

how to configure hibernate config file for sql server