I'm trying to setup a new project based on a working existing project. I haven't been able to get the database connection to work through JDBC. I'm not getting any errors, just an empty list result. I know there is something wonky with the setup of the database, because I was getting an error that the bean for the NamedParameterJdbcTemplate was not found. In the previous project, I had defined the database connection information in the application-default.properties, which was sufficient to allow spring to pick it up and use. However, to resolve the issue, I added a bean to the WebSecurity/Configuration file. This resolved that issue, allowing the database to at least connect. I know the connection is working, because if I intentionally mistype a table/column name then I get a SQL error that it can't find the table. Since it is finding the table, I can't fathom at this point what is preventing data from coming back.
I'll note that I know there are misconfigurations I have to figure out somewhere. Not only can I not define the database in the application.properties, but I also can't get cors to work either, and have been allowing all traffic for now because I've spent ages on trying to figure out cors and gotten nowhere.
Code:
Repository:
@Slf4j
@Repository
public class TestRepository {
private final String FIND_ALL_QUERY =
"SELECT " +
" test.ids AS id, " +
" test.value AS value " +
"FROM test_table test ";
@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
@Autowired
private DataSource dataSource;
@Transactional
public List<TestDto> findAll() {
log.info("running query...");
return this.namedParameterJdbcTemplate.query(FIND_ALL_QUERY, BeanPropertyRowMapper.newInstance(TestDto.class));
}
}
Web Security:
@Slf4j
@PropertySource("classpath:application.properties")
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
log.info("In securityFilterChain");
http.csrf().disable().authorizeHttpRequests().anyRequest().permitAll();
return http.build();
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("oracle.jdbc.OracleDriver");
ds.setUrl("jdbc:oracle:thin:@localhost:1521");
ds.setUsername("c##redacted");
ds.setPassword("redacted");
return ds;
}
@Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate() {
return new NamedParameterJdbcTemplate(dataSource());
}
}
application.properties:
spring.datasource.url=jdbc:oracle:thin:@localhost:1521
spring.datasource.username=c##redacted
spring.datasource.password=redacted
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.hikari.maxLifetime=60000
Service Implementation:
@Slf4j
@RequiredArgsConstructor
@Service
public class TestServiceImpl implements TestService {
@Autowired
private TestRepository testRepository;
public List<TestDto> findAll() {
List<TestDto> test = testRepository.findAll();
log.info("test.size(): ", test.size());
log.info("test.get(0): ", test.get(0));
return test;
}
}
Database Table:
Output:
2025-05-24 13:16:05 INFO campaign.controller.TestController - findingAll...
2025-05-24 13:16:05 INFO campaign.repository.TestRepository - running query...
2025-05-24 13:16:05 INFO c.serviceimpl.TestServiceImpl - test.size():
2025-05-24 13:16:05 INFO c.serviceimpl.TestServiceImpl - test.get(0):
Edit: This may still be an issue with CORS. I don't know how given the reconfiguration I did to WebSecurity to remove everything related to authentication and cors. However, I'm getting this error in the web console:
Access to XMLHttpRequest at 'http://127.0.0.1:8080/archlands/api/test/value' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
TestDto (shared by request from comment):
@Data
@ToString
@RequiredArgsConstructor
@AllArgsConstructor
@Entity
public class TestDto {
private Long id;
private String value;
@Override
public boolean equals(Object checkObject) {
Boolean usersAreEqual = false;
if (this == checkObject) {
usersAreEqual = true;
} else if (checkObject == null || Hibernate.getClass(this) != Hibernate.getClass(checkObject)) {
usersAreEqual = false;
} else { // correct type, check values
TestDto checkUser = (TestDto) checkObject;
if (id != null) {
usersAreEqual = Objects.equals(id, checkUser.id);
}
}
return usersAreEqual;
}
@Override
public int hashCode() {
return getClass().hashCode();
}
}
You mentioned:
"No 'Access-Control-Allow-Origin' header is present..."
Update your SecurityConfig
to explicitly enable CORS:
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.cors(Customizer.withDefaults()) // Enable CORS
.csrf().disable()
.authorizeHttpRequests()
.anyRequest().permitAll();
return http.build();
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(List.of("http://localhost:4200")); // Your frontend
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
config.setAllowedHeaders(List.of("*"));
config.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return source;
}
}
Other Problem : @RequiredArgsConstructor
with fields not final
Your fields id
and value
are not final
, but you have, Remove @RequiredArgsConstructor annotation.
This has no effect unless fields are final
or annotated with @NonNull
.