使用Hibernate连接到SQL Server

时间:2022-01-30 01:41:08

How to connect to the Sql Server database by using Hibernate in Netbeans 6.7.1? Please advice.

如何在Netbeans 6.7.1中使用Hibernate连接到Sql Server数据库?请指教。

The dialect is disabled for me.

方言对我来说是禁用的。

2 个解决方案

#1


2  

For SQL Server 2000 and 2005, you should use SQLServerDialect (that should be preferred over SybaseDialect that will be deprecated).

对于SQL Server 2000和2005,您应该使用SQLServerDialect(这应该优先于不推荐使用的SybaseDialect)。

I can't verify how NetBeans Hibernate wizard behave with SQL Server but, according to this tutorial:

我无法验证NetBeans Hibernate向导如何与SQL Server一起运行,但是,根据本教程:

When you create a Hibernate configuration file using a wizard you specify the database connection by choosing from a list of database connection registered with the IDE. When generating the configuration file the IDE automatically adds the connection details and dialect information based on the selected database connection.

使用向导创建Hibernate配置文件时,可以通过从IDE注册的数据库连接列表中进行选择来指定数据库连接。生成配置文件时,IDE会根据所选的数据库连接自动添加连接详细信息和方言信息。

So, my question is: did you register a database connection for your SQL Server database? If not, go to Services, add a New Driver (right click on the Drivers node) for your SQL Server JDBC driver and add a New Connection (right click on the Databases node) with the right URL for your database.

所以,我的问题是:您是否为SQL Server数据库注册了数据库连接?如果没有,请转到“服务”,为SQL Server JDBC驱动程序添加“新驱动程序”(右键单击“驱动程序”节点),然后使用正确的数据库URL添加“新建连接”(右键单击“数据库”节点)。

#2


1  

For anyone using Hibernate with SQL Server – our customized dialect may help: http://www.componentix.com/blog/5/improved-hibernate-dialect-for-microsoft-sql-server

对于任何使用Hibernate和SQL Server的人 - 我们的定制方言可能有所帮助:http://www.componentix.com/blog/5/improved-hibernate-dialect-for-microsoft-sql-server

public class SQLServerDialect extends org.hibernate.dialect.SQLServerDialect {

   /**
    * Initializes a new instance of the {@link SQLServerDialect} class.
    */
    public SQLServerDialect() {
        registerColumnType(Types.BIGINT, "bigint");
        registerColumnType(Types.BIT, "bit");
        registerColumnType(Types.CHAR, "nchar(1)");
        registerColumnType(Types.VARCHAR, 4000, "nvarchar($l)");
        registerColumnType(Types.VARCHAR, "nvarchar(max)");
        registerColumnType(Types.VARBINARY, 4000, "varbinary($1)");
        registerColumnType(Types.VARBINARY, "varbinary(max)");
        registerColumnType(Types.BLOB, "varbinary(max)");
        registerColumnType(Types.CLOB, "nvarchar(max)");
    }
}

#1


2  

For SQL Server 2000 and 2005, you should use SQLServerDialect (that should be preferred over SybaseDialect that will be deprecated).

对于SQL Server 2000和2005,您应该使用SQLServerDialect(这应该优先于不推荐使用的SybaseDialect)。

I can't verify how NetBeans Hibernate wizard behave with SQL Server but, according to this tutorial:

我无法验证NetBeans Hibernate向导如何与SQL Server一起运行,但是,根据本教程:

When you create a Hibernate configuration file using a wizard you specify the database connection by choosing from a list of database connection registered with the IDE. When generating the configuration file the IDE automatically adds the connection details and dialect information based on the selected database connection.

使用向导创建Hibernate配置文件时,可以通过从IDE注册的数据库连接列表中进行选择来指定数据库连接。生成配置文件时,IDE会根据所选的数据库连接自动添加连接详细信息和方言信息。

So, my question is: did you register a database connection for your SQL Server database? If not, go to Services, add a New Driver (right click on the Drivers node) for your SQL Server JDBC driver and add a New Connection (right click on the Databases node) with the right URL for your database.

所以,我的问题是:您是否为SQL Server数据库注册了数据库连接?如果没有,请转到“服务”,为SQL Server JDBC驱动程序添加“新驱动程序”(右键单击“驱动程序”节点),然后使用正确的数据库URL添加“新建连接”(右键单击“数据库”节点)。

#2


1  

For anyone using Hibernate with SQL Server – our customized dialect may help: http://www.componentix.com/blog/5/improved-hibernate-dialect-for-microsoft-sql-server

对于任何使用Hibernate和SQL Server的人 - 我们的定制方言可能有所帮助:http://www.componentix.com/blog/5/improved-hibernate-dialect-for-microsoft-sql-server

public class SQLServerDialect extends org.hibernate.dialect.SQLServerDialect {

   /**
    * Initializes a new instance of the {@link SQLServerDialect} class.
    */
    public SQLServerDialect() {
        registerColumnType(Types.BIGINT, "bigint");
        registerColumnType(Types.BIT, "bit");
        registerColumnType(Types.CHAR, "nchar(1)");
        registerColumnType(Types.VARCHAR, 4000, "nvarchar($l)");
        registerColumnType(Types.VARCHAR, "nvarchar(max)");
        registerColumnType(Types.VARBINARY, 4000, "varbinary($1)");
        registerColumnType(Types.VARBINARY, "varbinary(max)");
        registerColumnType(Types.BLOB, "varbinary(max)");
        registerColumnType(Types.CLOB, "nvarchar(max)");
    }
}