I am working on a java project that uses Gradle, Spring, Spring Security, JPA and an H2 database. I am trying to use JPA persistence to implement database tables for role, user and usecase-table.
This is my applications.properties
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.url=jdbc:h2:~/testdb;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
Here is my @Entity User
@Entity
@Table(name = "user")
public class User {
public User() {
}
public User(Long id, String email, String password, String fullname, boolean enabled){
this.id = id;
this.email=email;
this.password=password;
this.fullname=fullname;
this.enabled=enabled;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String email;
private String password;
private String fullname;
private boolean enabled;
@ManyToMany
@JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
Here is my @Entity Role
@Entity
@Table(name = "role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String role;
@ManyToMany(mappedBy = "roles")
private Set<User> users;
Here is one of my repositories for User, the others are pretty much the same:
public interface UserRepository extends JpaRepository<User, Long> {
User findByEmail(final String email);
}
Here is MyCapecApp.java
@SpringBootApplication
public class MyCapecApp {
private static final Logger log = LoggerFactory.getLogger(MyCapecApp.class);
public static void main(String[] args) {
SpringApplication.run(MyCapecApp.class, args);
}
@Bean
CommandLineRunner init(RoleRepository roleRepository) {
return args -> {
Role adminRole = roleRepository.findByRole("ADMIN");
if (adminRole == null) {
Role newAdminRole = new Role();
newAdminRole.setRole("ADMIN");
roleRepository.save(newAdminRole);
}
Role userRole = roleRepository.findByRole("USER");
if (userRole == null) {
Role newUserRole = new Role();
newUserRole.setRole("USER");
roleRepository.save(newUserRole);
}
log.info("Roles found with findll():");
log.info("----------------------------------");
for(Role rol : roleRepository.findAll()){
log.info((roleRepository.toString()));
}
log.info("");
};
}
And these are the errors I am getting.
2020-04-21 16:34:21.668 WARN 16560 --- [ main] o.h.t.s.i.ExceptionHandlerLoggedImpl : GenerationTarget encountered exception accepting command : Error executing DDL "drop table role if exists" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "drop table role if exists" via JDBC Statement
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Cannot drop "ROLE" because "FKT4V0RRWEYK393BDGT107VDX0X" depends on it; SQL statement:
drop table role if exists [90107-200]
And this is the next error.
2020-04-21 16:34:21.678 WARN 16560 --- [ main] o.h.t.s.i.ExceptionHandlerLoggedImpl : GenerationTarget encountered exception accepting command : Error executing DDL "create table user (id bigint not null, email varchar(255), enabled boolean not null, fullname varchar(255), password varchar(255), primary key (id))" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table user (id bigint not null, email varchar(255), enabled boolean not null, fullname varchar(255), password varchar(255), primary key (id))" via JDBC Statement
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "USER" already exists; SQL statement:
create table user (id bigint not null, email varchar(255), enabled boolean not null, fullname varchar(255), password varchar(255), primary key (id)) [42101-200]
The errors are the same for all three tables. I connected my database 'testdb' in the database window in intellij. Previously, i had created the three tables in the database window for user, role and usecase-table, of the same name, but i still got errors, and i was under the impression that JPA did that for you when you run the program. If that is the problem then perhaps i just made the table wrong? If so can someone please tell me the right way to do it? When i boot the project on localhost everything runs, but user input is not being stored in the database tables.
This is the first time i have ever done any project using spring or databases or of such complexity, so i apologize for my ignorance in advance. I am a student, and I really want to learn this. Please let me know if there is anything else i should include. Thanks again and I really appreciate any help you guys can offer me, and I am very grateful for your patience and your time.