Dobry wieczór,
robię prosty prosty formularz rejestracyjny w spring boot używając security i jpa,
podczas odpalania Spring Boota wywala mi szereg błędów związanych najpewniej z DDL, poniżej wrzucam spis błędów,
Trochę je skróciłem żeby nie dorzucać wam do przeglądania >300 linijek:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table user (id int8 generated by default as identity, email varchar(255), > first_name varchar(255), lastt_name varchar(255), password varchar(255), primary key (id))" via JDBC Statement
Caused by: org.postgresql.util.PSQLException: BŁĄD: błąd składni w lub blisko "user"
Pozycja: 14
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table if exists user_roles add constraint FK55itppkw3i07do3h7qoclqd4k >foreign key (user_id) references user" via JDBC Statement
Caused by: org.postgresql.util.PSQLException: BŁĄD: błąd składni w lub blisko "user"
Pozycja: 110
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Users/Kiano/Desktop/Registration/target/classes/data.sql]: insert into user_role(role, description) values ("ROLE_USER", "default role for user"); nested exception is org.postgresql.util.PSQLException: BŁĄD: kolumna "ROLE_USER" nie istnieje
Pozycja: 50
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Users/Kiano/Desktop/Registration/target/classes/data.sql]: insert into user_role(role, description) values ("ROLE_USER", "default role for user"); nested exception is org.postgresql.util.PSQLException: BŁĄD: kolumna "ROLE_USER" nie istnieje
Pozycja: 50
Caused by: org.postgresql.util.PSQLException: BŁĄD: kolumna "ROLE_USER" nie istnieje
Tyle by było z błędu, teraz wrzucę wam encje także w skróconej wersji bez getterów i setterów, zachodzi tutaj relacja wiele do wielu między użytkownikiem a jego rolą np. w firmie
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lasttName;
@NotEmpty
private String email;
@NotEmpty
private String password;
@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
private Set<UserRole> roles = new HashSet<>();
```@Entity
public class UserRole {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String role;
private String description;
Teraz czas na skrypt SQL który znajduję się w resources a ma po prostu dodawać dane do encji,
insert into user_role(role, description) values ("ROLE_USER", "default role for user");
application.properties
spring.datasource.url=jdbc//localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=update
spring.datasource.initialization-mode=always
Liczę na waszą pomoc.