java - 使用 Hibernate JDBC 连接连接到 Azure SQL Server

标签 java hibernate azure jdbc azure-sql-database

Hibernate 连接与本地 postgresql 配合良好,它会自动创建表,但是当我们想要使用 Azure SQL Server(Microsoft SQL Server)并且更改驱动程序名称等时,我们没有收到任何错误,但它没有工作。 (没有创建表)

jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.databaseurl=jdbc:sqlserver://xxxx.database.windows.net:1433;database=xxxx
jdbc.password=xxxx
jdbc.user=xxx@xxxx
jdbc.encrypt=true
jdbc.hostNameInCertificate=*.database.windows.net
jdbc.loginTimeout=30;
jdbc.dialect=org.hibernate.dialect.SQLServerDialect

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/jdbc.properties" >
</bean>

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.user}"
    p:password="${jdbc.password}">
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan">
        <list>
            <value>com.xxx.entity</value>
        </list>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.c3p0.min_size">5</prop>
            <prop key="hibernate.c3p0.max_size">20</prop>
            <prop key="hibernate.c3p0.idle_test_period">60</prop>
            <prop key="hibernate.c3p0.max_statements">100</prop>
            <!--<prop key="hibernate.hbm2ddl.auto">${database.hbm2ddl.auto}</prop>-->
            <!--<prop key="hibernate.show_sql">${database.show_sql}</prop>-->
            <!--<prop key="hibernate.format_sql">${database.format_sql}</prop>-->
        </props>
    </property>
</bean>

最佳答案

问题出在 jdbc 连接 URL 中,属性 database=test1 应该是 databaseName=test1。阅读JDBC specificationsSQL Server Driver spec了解更多详细信息。

当在连接 URL 中找不到 databaseName 时,驱动程序会尝试写入 master 数据库,这在 Azure SQL Server 中是不允许的。

我在 Spring Hibernate 应用程序中通过 JDBC 驱动程序连接到 Azure SQL Server 时遇到权限问题,日志如下

2016-02-26 19:22:37.958 ERROR 11424 --- [lication.main()] org.hibernate.tool.hbm2ddl.SchemaExport:HHH000389:失败:创建表用户(id numeric(19,0) 身份不为空,电子邮件 varchar(255) 不为空,名称 varchar(255) 不为空,主键 (id)) 2016-02-26 19:22:37.964 错误 11424 --- [lication.main()] org.hibernate.tool.hbm2ddl.SchemaExport:数据库“master”中的 CREATE TABLE 权限被拒绝。

Azure 门户为您提供自动创建的连接 URL(这非常酷),我得到了如下一个,

jdbc:sqlserver://dp-test1.database.windows.net:1433;database=test1;user=admin@dp-test1;password={your_password_here};encrypt=true;trustServerCertificate=false; hostNameInCertificate=.database.windows.net;loginTimeout=30;*

如您所见,此网址存在问题。属性 database=test1 应为 databaseName=test1。我将向 Azure 团队通报此问题的最新情况,希望他们能够尽快修复该问题。

更改后,我将本地应用程序连接到 Azure SQL Server DB。

关于java - 使用 Hibernate JDBC 连接连接到 Azure SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23266614/

相关文章:

hibernate - jpa hibernate : detached entity passed to persist

java - 具有额外列的多对多+获取独特的对象

python - Azure Webapp 轮子 --find-links 不起作用

azure - 使用 Microsoft Azure 限制网站在特定区域打开

azure - 是否有 VSTS 构建任务可以从注册表中删除未使用的 Docker 容器?

java - 一个 Java 文件中的多个枚举类

java - Eclipse RCP 案例研究

java - 如何将 "mvn spring-boot:run"重命名为 "mvn someName:run"?

java - Hibernate,当我使 id 列自动递增时,sql 出现错误

java - 足球数据 API JSON 解析错误