У меня стандартное Rest Spring Bott Application, в котором пока что только одна таблица. Я сразу подключил flyway, чтобы создавать ее автоматически заполнять данными. Для начала подключил зависимость в pom.xml:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
<version>9.3.0</version>
</dependency>
После этого в resources создал структуру папок db.migration и добавил свой скрипт, который назвал V1__Data.sql:
CREATE TABLE sensors(
id BIGINT NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
model VARCHAR(15),
range_from INT,
range_to INT,
type VARCHAR(11),
unit VARCHAR(7),
location VARCHAR(40),
description VARCHAR(200),
PRIMARY KEY(id)
);
INSERT INTO sensors(name, model, range_from, range_to, type, unit, location, description) VALUES
('Sensor 1', 'PC33-56', 0, 16, 'Pressure', 'bar', 'Room1', 'This sensor has number one'),
('Sensor 2', 'EH-567', -25, 25, 'Voltage', 'voltage', 'Room1', 'This sensor has number two'),
('Sensor 3', 'TER-21', -70, 50, 'Temperature', '°С', 'Room2', 'This sensor has number three'),
('Sensor 4', 'H94', 0, 100, 'Humidity', '%', 'Room3', 'This sensor has number four'),
('Sensor 5', 'GD-23', 0, 30, 'Temperature', '°С', 'Room4', 'This sensor has number five'),
('Sensor 6', 'LC-11', 5, 10, 'Voltage', 'voltage', 'Room3', 'This sensor has number six'),
('Sensor 7', 'RFD-84', 15, 75, 'Pressure', 'bar', 'Room2', 'This sensor has number seven'),
('Sensor 8', 'QQ-12', 40, 80, 'Temperature', '°С', 'Room1', 'This sensor has number eight'),
('Sensor 9', 'LL_3', -3, 10, 'Temperature', '°С', 'Room1', 'This sensor has number nine'),
('Sensor 10', 'PC33-56', -5, 50, 'Voltage', 'voltage', 'Room2', 'This sensor has number ten'),
('Sensor 11', 'H94', 5, 10, 'Pressure', 'bar', 'Room4', 'This sensor has number eleven'),
('Sensor 12', 'EH-567', 0, 50, 'Humidity', '%', 'Room4', 'This sensor has number twelve'),
('Sensor 13', 'PC33-56', 3, 10, 'Humidity', '%', 'Room4', 'This sensor has number thirteen'),
('Sensor 14', 'GD-23', 0, 100, 'Voltage', 'voltage', 'Room1', 'This sensor has number fourteen'),
('Sensor 15', 'TER-21', 0, 30, 'Pressure', 'bar', 'Room1', 'This sensor has number fifteen'),
('Sensor 16', 'LL_3', 3, 10, 'Humidity', '%', 'Room2', 'This sensor has number sixteen'),
('Sensor 17', 'EH-567', 0, 30, 'Humidity', '%', 'Room3', 'This sensor has number seventeen'),
('Sensor 18', 'H94', -25, 25, 'Temperature', '°С', 'Room4', 'This sensor has number eighteen'),
('Sensor 19', 'GD-23', -70, 50, 'Pressure', 'bar', 'Room1', 'This sensor has number nineteen'),
('Sensor 20', 'EH-567', 0, 100, 'Voltage', 'voltage', 'Room2', 'This sensor has number twenty'),
('Sensor 21', 'LL_3', 3, 10, 'Humidity', '%', 'Room4', 'This sensor has number twenty one'),
('Sensor 22', 'TER-21', -25, 25, 'Pressure', 'bar', 'Room2', 'This sensor has number twenty two'),
('Sensor 23', 'PC33-56', -70, 50, 'Temperature', '°С', 'Room3', 'This sensor has number twenty three'),
('Sensor 24', 'H94', -25, 25, 'Voltage', 'voltage', 'Room1', 'This sensor has number twenty four'),
('Sensor 25', 'QQ-12',0, 30, 'Pressure', 'bar', 'Room4', 'This sensor has number twenty five'),
('Sensor 26', 'EH-567', 0, 100, 'Humidity', '%', 'Room4', 'This sensor has number twenty six');
После этого добавил entity:
package com.example.labinventtaskbackend.entities;
import jakarta.persistence.*;
import lombok.*;
@Entity
@Table(name = "sensors")
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SensorEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String model;
@Column(name = "range_from")
private Integer rangeFrom;
@Column(name = "range_to")
private Integer rangeTo;
private String type;
private String unit;
private String location;
private String description;
}
И в properties добавил запись
**здесь становил url, user, password для подключения к db
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=validate
После этого запускаю приложение и получаю ошибки:
Error creating bean with name 'entityManagerFactory' defined in class path resource
[PersistenceUnit: default] Unable to build Hibernate SessionFactory;
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [sensors]
При этом таблица миграций в базе данных создается