I have a spring boot application with two classes – Load and Location. The relationship between location and load is oneToMany.
In my application.properties
file I have set spring.jpa.hibernate.ddl-auto=update
.
When I initially tried to start the application, hibernate could not establish the foreign key relationship between the tables due to a syntax error. I added it manually and now I get the error:
2024-04-25T09:58:13.743+02:00 ERROR 12760 --- [putaway] [ main] com.zaxxer.hikari.HikariConfig : Failed to load driver class com.mysql.cj.jdbc.Driver from HikariConfig class classloader jdk.internal.loader.ClassLoaders$AppClassLoader@1d44bcfa
2024-04-25T09:58:13.744+02:00 WARN 12760 --- [putaway] [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource
[org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0: Error creating bean with name 'dataSource' defined in class path resource
[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message:
Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class loader or Thread context classloader
These are my Entity classes:
Location.java
@Entity
@Data
public class Location implements Comparable<Location> {
@Id
private String id;
private LocationCapacity capacity;
@OneToOne
@JoinColumn(name = "merchandise_id")
private Merchandise merchandise;
@OneToMany
private List<Load> loads;
@Override
public int compareTo(Location o) {
return 0;
}
public Integer getCount() {
int count = 0;
for (Load l : loads) {
count += l.getNumberOfCases();
}
return count;
}
public static class Comparators {
public static Comparator<Location> caseCount = Comparator.comparing(Location::getCount);
}
}
Load.java
@Data
@Entity
public class Load {
@Id
private Integer loadNumber;
@OneToOne
@JoinColumn(name = "merchandise_crc")
private Merchandise merchandise;
private Integer numberOfCases;
}
I also have a Merchandise entity which is referenced in both the Location and Load classes:
@Entity
@Data
public class Merchandise {
@Id
private String crc;
private String name;
private MerchandiseSize size;
@OneToMany(mappedBy = "id")
private List<Location> locations;
}
This application has run and returned responses to Postman prior to adding the Load entity.