【发布时间】:2021-01-14 16:38:45
【问题描述】:
我正在练习/学习一些 Spring Boot + JPA 应用程序,并在尝试保存到数据库表时遇到问题。
看起来 JPA 是自动生成表名,即使我提供了 @Table 注释。
我有一个简单的 POJO,用 @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
}
扩展 JPA CrudRepository 的接口
import org.springframework.data.repository.CrudRepository;
public interface songRepo extends CrudRepository<SongList,Integer> {
}
设置数据源属性的属性 yml 文件
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
最后是测试类:
@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);
}
}
在运行测试类时,我的测试失败并出现错误:
ERROR:
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: java.sql.SQLSyntaxErrorException: Table 'songdb.song_list' doesn't exist
我的数据库表已经存在。数据库称为 Songdb(不是 songdb),表是 SongList。
我不确定表名“song_list”在哪里注入以及为什么我的数据库名称是“songdb”而不是 url 中提供的“Songdb”。
我在这里缺少什么?请帮忙!
【问题讨论】:
标签: java spring-boot spring-data-jpa