使用hibernate5.40连接SQLServer2012,所有的配置都正常,但是出现无法打开数据库,同时爆出连接错误

weixin_42936484 2018-11-18 07:57:25
Customer类:
package com.sqlserver.domain;

import lombok.Getter;
import lombok.Setter;

@Getter@Setter
public class Customer {
long cust_id;
String cust_name;
String cust_source;
String cust_industry;
String cust_level;
String cust_phone;
String cust_mobie;
}

hibernate.cfg.xml配置


<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--配置SQLServer连接属性 -->
<!-- 第一步配置方言 -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=hibernate_sql</property>
<property name="connection.username">sa</property>
<property name="connection.password">123456</property>

<!--在控制台显示SQL语句 -->
<property name="show_sqlserver">true</property>

<!--根据需要自动生成、更新数据表 -->
<property name="hbm2ddl.auto">update</property>
<property name="myeclipse.connection.profile">sqlserver</property>
<!--注册所有ORM映射文件 -->
<mapping resource="com/sqlserver/domain/Customer.hbm.xml" />

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

映射文件Customer.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.sqlserver.domain.Customer" table="customer">
<!-- 主键 -->
<id name="cust_id" column="cust_id">
<generator class="native"></generator>
</id>
<!-- 普通列关联,name和column一样可以省略column -->

<property name="cust_name" column="cust_name" />
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping>

测试类hibernateTest.java中关键代码
public class HibrenateTest {
@Test
public void test1(){
// 加载配置文件
Configuration configure = new Configuration().configure();
// 创建sessionfactory 连接池
SessionFactory sessionFactory = configure.buildSessionFactory();
// 获取session jdbc连接对象
Session session = sessionFactory.openSession();

Customer customer = new Customer();
customer.setCust_name("myone");
customer.setCust_level("2");

// 保存
session.save(customer);
// 释放资源
session.close();
sessionFactory.close();
}
}

异常代码:
十一月 18, 2018 7:34:36 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.0.CR1}
十一月 18, 2018 7:34:36 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十一月 18, 2018 7:34:37 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
十一月 18, 2018 7:34:38 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
十一月 18, 2018 7:34:38 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost:1433;databaseName=hibernate_sql]
十一月 18, 2018 7:34:38 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=sa, password=****}
十一月 18, 2018 7:34:38 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
十一月 18, 2018 7:34:38 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
十一月 18, 2018 7:34:38 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
...全文
33 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧