Java 如何为sql server配置hibernate配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3585544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:12:17  来源:igfitidea点击:

how to configure hibernate config file for sql server

javasql-serverhibernateormhibernate.cfg.xml

提问by Lalchand

Here is the config file for MySQL:

这是 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:

为 SQL Server 2005 指定什么?我是这样做的:

<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:

在 MySQL 中,我曾经这样做过:

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

采纳答案by Pascal Thivent

Properties that are database specificare:

特定于数据库的属性是:

  • 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.Dialectwhich allows Hibernate to generate SQL optimized for a particular relational database.
  • hibernate.connection.driver_class:JDBC驱动类
  • hibernate.connection.url: JDBC 网址
  • hibernate.connection.username: 数据库用户
  • hibernate.connection.password: 数据库密码
  • hibernate.dialect: Hibernate 的类名,org.hibernate.dialect.Dialect它允许 Hibernate 生成针对特定关系数据库优化的 SQL。

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 Dialectused by Hibernate to talk to the database
  1. 在类路径上为数据库提供适当的 JDBC 驱动程序,
  2. 更改 JDBC 属性(驱动程序、网址、用户、密码
  3. 更改DialectHibernate 使用的与数据库对话的方式

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

有两个驱动程序可以连接到 SQL Server;开源jTDS和微软的。驱动程序类和 JDBC URL 取决于您使用的是哪一个。

With the jTDS driver

使用 jTDS 驱动程序

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

驱动程序类名称是net.sourceforge.jtds.jdbc.Driver.

The URL format for sqlserver is:

sqlserver 的 URL 格式为:

 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 配置看起来像(注意你可以跳过hibernate.属性中的前缀):

<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:

使用 Microsoft SQL Server JDBC 3.0:

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

驱动程序类名称是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 配置看起来像:

<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

参考

回答by KeatsPeeks

The connection URL should look like this for SQL Server:

SQL Server 的连接 URL 应如下所示:

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

Examples:

例子:

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

回答by fguespe

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

不要忘记在 SQL SERVER 配置工具中启用 tcp/ip 连接

回答by rjha

We also need to mention default schema for SQSERVER: dbo

我们还需要提及 SQSERVER 的默认架构:dbo

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

Tested with hibernate 4

用休眠 4 测试

回答by Pritam Banerjee

Finally this is for Hibernate 5in Tomcat.

最后这是为了Hibernate 5in 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 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>

回答by Arjun Das

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

将 jar 文件保存在 web-inf lib 下,以防您包含 jar 并且它无法识别 .

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

它适用于我的情况,一切正常,但无法加载驱动程序类。