如何为sql server配置hibernate配置文件 [英] how to configure hibernate config file for sql server
问题描述
以下是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>根< / 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 resource =model.hbm.xml/>
< / session-factory>
< / hibernate-configuration>
要为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 resource =model.hbm.xml/>
< / session-factory>
< / hibernate-configuration>
我的问题更精确的是如何指定我必须连接的数据库?
在MySQL中,我曾经这样做过:
< property name = hibernate.connection.url> JDBC:MySQL的://本地主机/试验< /性>
特定于数据库的属性是:
-
hibernate.connection.driver_class
:JDBC驱动程序类 -
hibernate.connection.url
:JDBC URL -
hibernate.connection。用户名
:数据库用户 -
hibernate.connection.password
:数据库密码
hibernate.dialect
:Hibernate的类名org.hibernate.dialect.Dialect
它允许Hibernate生成针对特定关系数据库优化的SQL。
要更改数据库,您必须:
- 为类路径中的数据库提供适当的JDBC驱动程序,
- 更改JDBC属性(驱动程序,url,用户,密码)
>
- 更改Hibernate使用的
Dialect
与数据库交谈
有两个驱动程序连接到SQL Server;开源的 jTDS 和微软的。驱动程序类和JDBC URL取决于您使用哪一个。
使用jTDS驱动程序
驱动程序类名是 net.sourceforge.jtds.jdbc.Driver
。
sqlserver的URL格式为:
jdbc:jtds:sqlserver://< server> [:< port>] [/< database>] [; < property> =< value> [; ...]]
看起来像(注意,你可以在属性中跳过 hibernate。
前缀):
<冬眠-结构>
< 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>
使用Microsoft SQL Server JDBC 3.0:
驱动程序类名是 com.microsoft.sqlserver.jdbc.SQLServerDriver
。
URL格式为:
jdbc:sqlserver:// [serverName [\ instanceName] [:portNumber]] [; property = value [; property =值]]
所以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>
参考文献
- Hibernate核心参考文档
- jTDS文档
- Microsoft SQL Server JDBC Driver 3.0文档
- Microsoft SQL Server JDBC Driver 2.0 支持列表对于Microsoft SQL Server JDBC驱动程序
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>
Properties that are database specific are:
hibernate.connection.driver_class
: JDBC driver classhibernate.connection.url
: JDBC URLhibernate.connection.username
: database userhibernate.connection.password
: database passwordhibernate.dialect
: The class name of a Hibernateorg.hibernate.dialect.Dialect
which allows Hibernate to generate SQL optimized for a particular relational database.
To change the database, you must:
- Provide an appropriate JDBC driver for the database on the class path,
- Change the JDBC properties (driver, url, user, password)
- 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
- 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
这篇关于如何为sql server配置hibernate配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!