I am practicing/learning some Spring Boot + JPA application and keep running into issues when attempting to save to database table.
It seems JPA is auto generating table name, even though I have provided the @Table annotation.
I have a simple POJO , marked with @Entity
@Entity
@Table(name="SongList")
public class SongList {
@Id
private Integer id;
private String album;
private String artist;
private String title;
//Getter and Setter Methods
}
An Interface that extends JPA CrudRepository
import org.springframework.data.repository.CrudRepository;
public interface songRepo extends CrudRepository<SongList,Integer> {
}
A properties yml file that sets datasource properties
spring:
datasource:
driverClassName : com.mysql.cj.jdbc.Driver
url : jdbc:mysql://localhost:3306/Songdb
username : root
password : learning
jpa:
hibernate.ddl-auto : update
generate-ddl : false
show-sql : true
And finally Test class :
@SpringBootTest
@RunWith(SpringRunner.class)
class DataJpaApplicationTests {
@Autowired
ApplicationContext context;
@Test
void saveSongs() {
songRepo repo = (songRepo) context.getBean(songRepo.class);
SongList songs = new SongList();
songs.setId(4);
songs.setTitle("High Hopes");
songs.setAlbum("Panic! At the Disco");
repo.save(songs);
}
}
Upon running the Test class , my test fails with error :
ERROR:
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: java.sql.SQLSyntaxErrorException: Table 'songdb.song_list' doesn't exist
My database table already exists. The database is called Songdb (not songdb) and table is SongList.
I am not sure where is the table name 'song_list' being injected and why my database name is 'songdb' instead of 'Songdb' as provided in the url.
What am I missing here ? Please help!