Spring JPA repository can save but after findBy throws "table not found" error

57 Views Asked by At

I have a Spring Boot Test like so :

@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
@Transactional
public class MyIntegrationTests {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private UserRepository userRepository;
    ...

    @BeforeEach
    public void init() {
        // Mockito mocks
        User u = new User();
        u.setName("test");
        userRepository.save(u);
    }

    @Test
    void myTest() {
        mockMvc.call to controller
    }
}

I added the @Transactional to the test class because otherwise the save() would fail in the init() method.

The controller calls a service ServiceA, that eventually calls another service ServiceB, and that service calls userRepository.findUserById(userId).

But this call throws an Exception:

o.h.engine.jdbc.spi.SqlExceptionHelper : Table "user" not found Table "person" not found; SQL statement: insert into user (created_at, user_field1, user_field2, user_field3) values (?, ?, ?, ?) [42102-200] could not prepare statement; SQL [insert into user (created_at, user_field1, user_field2, user_field3)values (?, ?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement

Why is that ? The test method is annotated as @Transactional. I tried to add @Transactional to the method of ServiceB that calls the repository, but same error.

1

There are 1 best solutions below

1
Panagiotis Bougioukos On

and that service calls userRepository.findUserById(userId).

But this call throws an Exception:

o.h.engine.jdbc.spi.SqlExceptionHelper : Table "user" not found Table "person" not found; SQL statement: insert into user (created_at, user_field1, user_field2, user_field3) values (?, ?, ?, ?) [42102-200] could not prepare statement; SQL [insert into user (created_at, user_field1, user_field2, user_field3)values (?, ?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement

Your question is not correct. The error does not popup from userRepository.findUserById(userId) sinse this would not trigger an insert sql query. Probably a userRepository.save is triggering this error.

Check if and how your entities and repositories are scanned for your database to be configured correctly.