当我启动它时,我无法让 spring boot 自动加载我的数据库模式。
这是我的application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=test
spring.datasource.password=
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy
这是我的Application.java:
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(final String[] args){
SpringApplication.run(Application.class, args);
}
}
这是一个示例实体:
@Entity
@Table(name = "survey")
public class Survey implements Serializable {
private Long _id;
private String _name;
private List<Question> _questions;
/**
* @return survey's id.
*/
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "survey_id", unique = true, nullable = false)
public Long getId() {
return _id;
}
/**
* @return the survey name.
*/
@Column(name = "name")
public String getName() {
return _name;
}
/**
* @return a list of survey questions.
*/
@OneToMany(mappedBy = "survey")
@OrderBy("id")
public List<Question> getQuestions() {
return _questions;
}
/**
* @param id the id to set to.
*/
public void setId(Long id) {
_id = id;
}
/**
* @param name the name for the question.
*/
public void setName(final String name) {
_name = name;
}
/**
* @param questions list of questions to set.
*/
public void setQuestions(List<Question> questions) {
_questions = questions;
}
}
任何想法我做错了什么?
原文由 napentathol 发布,翻译遵循 CC BY-SA 4.0 许可协议
有几个可能的原因:
您的实体类与您的实体类相同或在子包中,您使用
@EnableAutoConfiguration.
如果没有,那么您的 spring 应用程序看不到它们,因此不会在 db 中创建任何东西检查您的配置,您似乎正在使用一些特定于休眠的选项,请尝试将它们替换为:
**请注意,驱动程序类的手动加载是不必要的,因为它是自动注册的,所以不要为它烦恼
application.properties
必须在src/main/resources
文件夹中。如果您没有正确指定方言,它可能会尝试默认与启动内存数据库捆绑在一起,并且(就像我一样)我可以看到它尝试连接到本地
HSQL
(请参阅控制台输出) 实例并在更新架构时失败。