Проблемы при попытке подключения к базе данных Hibernate Java
Я пытаюсь подключиться к базе данных, и мой запрос должен был создать таблицу и добавить в нее значения. Я использую Microsoft SQL Server Management Studio и создал базу данных с именем «itstep» в папке «Databases». Подключаюсь через Windows Authentification.
------- HibernateMain.java ------
public class HibernateMain {
public static void main(String[] args) {
Color color = new Color(Arrays.asList("red", "green", "blue"),true);
Map<String,Integer> rgb = new HashMap<String,Integer>();
rgb.put("r",255);
rgb.put("g",0);
rgb.put("b",0);
color.setRgb(rgb);
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(color);
session.getTransaction().commit();
session.close();
System.exit(0);
}
}
------- HibernateUtil.java ------
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {// Create properties for Hibernate configuration
Properties hibernateProps = new Properties();
hibernateProps.put(Environment.DRIVER,
"com.microsoft.sqlserver.jdbc.SQLServerDriver");
hibernateProps.put(Environment.URL, "jdbc:mysql://localhost:3306/itstep");
hibernateProps.put(Environment.USER, "sa");
hibernateProps.put(Environment.PASS, "");
hibernateProps.put(Environment.DIALECT, "org.hibernate.dialect.SQLServerDialect");
hibernateProps.put(Environment.SHOW_SQL, "true");
hibernateProps.put(Environment.HBM2DDL_AUTO, "create");
// Build the configuration object and register entity classes
Configuration configuration = new Configuration();
configuration.setProperties(hibernateProps);
configuration.addAnnotatedClass(Color.class);
// Build the ServiceRegistry and create the SessionFactory
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
return configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}}
public static SessionFactory getSessionFactory() {
return sessionFactory; }}
------- Color.java ------
@Entity
@Data
@Table(name="color")
@NoArgsConstructor
public class Color implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private List<String> colors;
@Column
private boolean primary;
@Column
private Map<String,Integer> rgb = new HashMap<String,Integer>();
public Color(List<String> colors, boolean primary, Map<String, Integer> rgb) {
this.colors = colors;
this.primary = primary;
this.rgb = rgb;
}
public Color(List<String> colors, boolean primary) {
this.colors = colors;
this.primary = primary;
}
public void setRgb(Map<String, Integer> rgb) {
this.rgb = rgb;
}
}
------- pom.xml ------
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-
v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Lab2.0</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Archetype - Lab2.0</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.20.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.2.0.jre11</version>
</dependency>
</dependencies>
Выходит такая ошибка:
WARN: HHH000342: Could not obtain connection to query metadata : Cannot
invoke
org.hibernate.engine.jdbc.connections.internal
.DriverManagerConnectionProvi
derImpl$PooledConnections.poll()" because "this.pool" is null
Mar. 28, 2023 1:08:24 ДП org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect:
org.hibernate.dialect.SQLServerDialect
Mar. 28, 2023 1:08:24 ДП
org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl
makeLobCreatorBuilder
INFO: HHH000422: Disabling contextual LOB creation as connection was
null
Mar. 28, 2023 1:08:24 ДП
org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 08S01
Mar. 28, 2023 1:08:24 ДП
org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: The TCP/IP connection to the host localhost, port 1433 has
failed. Error: "Connection refused: no further information. Verify the
connection properties. Make sure that an instance of SQL Server is
running on the host and accepting TCP/IP connections at the port. Make
sure that TCP connections to the port are not blocked by a firewall.".
Initial SessionFactory creation
failed.org.hibernate.service.spi.ServiceException: Unable to create
requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.service.spi.ServiceException: Unable to create
requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
.......
Caused by: org.hibernate.exception.JDBCConnectionException: Error
calling Driver#connect
.......
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP
connection to the host localhost, port 1433 has failed. Error:
"Connection refused: no further information. Verify the connection
properties. Make sure that an instance of SQL Server is running on the
host and accepting TCP/IP connections at the port. Make sure that TCP
connections to the port are not blocked by a firewall.".
Не пойму в чем причина, может я неправильные данные от сервера ввожу? У меня нет пароля и логина от sql server, поскольку подключаюсь через Windows Authentification, ввел то что мне посоветовали, как данные по умолчанию