Issue
I am a starter, and I created a simple web service app by refering this tutorial: https://spring.io/guides/gs/accessing-data-mysql/
And then I tried to do some modifications for accesing the second database and I got an error for that. I checked my database and its users, and they seem okay.
Can anyone help me with it? Thanks!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in TableReady.WebService.Repositories.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
application.properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource1.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/users
spring.datasource1.username=user_info_admin
spring.datasource1.password=(@.SjFHn8LqdL)7/
spring.datasource2.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/general
spring.datasource2.username=general_admin
spring.datasource2.password=sP7Y86.=-k<hKSgG
JpaConfig.java
@Configuration
public class JpaConfig {
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource1")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="spring.datasource2")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
MainController.java
@Controller // This means that this class is a Controller
@RequestMapping(path = "/users") // This means URL's start with /demo (after Application path)
public class MainController {
// This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
@Autowired
private UserRepository userRepository;
@Autowired
private RestaurantRepository restaurantRepository;
// something else...
// ...
// ...
}
Restaurant.java
@Entity
@Table(name = "restaurants", schema = "general")
public class Restaurant {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String address;
private String email;
private String phone;
// ...
// ...
}
User.java
@Entity
@Table(name = "users", schema = "users")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String password;
private String name;
private String email;
private String phone;
// ...
// ...
}
Both repos look like:
public interface SomeRepository extends CrudRepository<User, Integer> {
}
Solution
This issue could be caused by the database server being down or not accessible. Please check if the database server is running.
More detailed answers are available here
Answered By - Charlie
Answer Checked By - Mary Flores (JavaFixing Volunteer)