Я занимаюсь разработкой приложения Spring и использую JPA с MariaDB для своих баз данных. Когда приложение запускается, оно сначала выдает некоторые исключения о несуществующих таблицах, но создает их. Приложение не завершает работу после ошибок.
Я что-то не так делаю?
обновление:
при изменении
spring.jpa.hibernate.ddl-auto=create-drop
до
spring.jpa.hibernate.ddl-auto=update
hibernate больше не выдает исключение.
почему?
Журналы летом:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table roles_privileges drop foreign key FK5yjwxw2gvfyu76j3rgqwo685u" via JDBC Statement
...
Caused by: java.sql.SQLSyntaxErrorException: (conn=105) Table 'users.roles_privileges' doesn't exist
...
Caused by: java.sql.SQLException: Table 'users.roles_privileges' doesn't exist
...
2019-10-29 18:13:00.866 WARN 821 --- [ main] o.h.t.s.i.ExceptionHandlerLoggedImpl : GenerationTarget encountered exception accepting command : Error executing DDL "alter table roles_privileges drop foreign key FK9h2vewsqh8luhfq71xokh4who" via JDBC Statement
...
Caused by: java.sql.SQLSyntaxErrorException: (conn=105) Table 'users.roles_privileges' doesn't exist
Объект пользователя:
@Entity
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Email
private String email;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String password;
@ManyToMany
@JoinTable(
name = "users_roles",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "username"),
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "name"))
private Collection<Role> roles;
...
}
Объект роли:
@Entity
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(unique = true, nullable = false)
private String name;
@ManyToMany(mappedBy = "roles")
private Collection<User> users;
@ManyToMany
@JoinTable(name = "roles_privileges",
joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "privilege_id", referencedColumnName = "id")
)
private Collection<Privilege> privileges;
...
}
Объект привилегий:
@Entity
public class Privilege {
@Id
private long id;
@Column(unique = true, nullable = false)
private String name;
@ManyToMany(mappedBy = "privileges")
private Collection<Role> roles;
....
}
application.properties
#User datasource
spring.datasource.url=jdbc:mariadb://localhost:3306/users
spring.datasource.username=user01
spring.datasource.password=user01pass
#`hibernate_sequence' doesn't exist
spring.jpa.hibernate.use-new-id-generator-mappings=false
spring.jpa.hibernate.ddl-auto=create-drop