nap*_*hol 50 java mysql spring hibernate spring-boot
当我启动时,我无法让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
Run Code Online (Sandbox Code Playgroud)
这是我的Application.java:
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(final String[] args){
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个示例实体:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
我有什么想法我做错了吗?
bor*_*s86 58
有几种可能的原因:
@EnableAutoConfiguration.
那个如果不是那么你的spring应用程序看不到它们因此不会在db中创建任何东西检查你的配置,似乎你正在使用一些特定于hibernate的选项,尝试用以下内容替换它们:
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=test
spring.datasource.password=
Run Code Online (Sandbox Code Playgroud)你application.properties
必须在src/main/resources
文件夹中.
如果你没有正确指定方言,它可能会尝试默认与boot in-memory数据库捆绑在一起(和我一样)我可以看到它尝试连接到本地HSQL
(请参阅控制台输出)实例并且在更新时失败架构.
lio*_*mon 46
你尝试过运行它:
spring.jpa.generate-ddl=true
Run Code Online (Sandbox Code Playgroud)
然后
spring.jpa.hibernate.ddl-auto = create
Run Code Online (Sandbox Code Playgroud)
默认情况下,DDL执行(或验证)将延迟到ApplicationContext启动为止.还有一个spring.jpa.generate-DDL标志,但如果Hibernate的自动配置是活动的,因为DDL-自动设置更精细的不使用它.
Ar *_*maj 14
@SpringBootApplication
@EnableConfigurationProperties
@EntityScan(basePackages = {"com.project.ppaa.model"}) // scan JPA entities
public class Application {
private static ConfigurableApplicationContext applicationContext;
public static void main(String[] args) {
Application.applicationContext = SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
它应该自动工作,但如果没有,你可以输入基础包
@EntityScan(basePackages = {"com.project.ppaa.model"}) // scan JPA entities manually
Run Code Online (Sandbox Code Playgroud)
你只需createDatabaseIfNotExist=true
像这样添加
spring.datasource.url=jdbc:mysql://localhost:3306/test?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true
Run Code Online (Sandbox Code Playgroud)
到您的 application.properties 文件
可悲的是,对我来说,上面给出的答案都不起作用,因为我后来发现问题来自我的pom
文件。我使用了 spring boot starter 项目,并添加了另一种 spring jpa,但它不起作用。最初我有这个,
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我用这个替换它:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
记下spring-boot-starter-data-jpa。希望这可以帮助某人。检查您的 pom 文件并确保您的依赖项匹配。
我用这个解决方案解决了我的问题。刚刚在application.properties文件的spring.datasource.url属性上插入了一个新参数createDatabaseIfNotExist=true,如下所示:
spring.datasource.url=jdbc:mysql://localhost:3306/minhasenha?autoReconnect=true&useSSL=false&createDatabaseIfNotExist=true
Run Code Online (Sandbox Code Playgroud)
我有带有 DDL的src/main/resources/Schema.sql来创建数据库架构。我确实使用过飞行器来创建和维护表格。
我在这里创建了这个解决方案: 原始答案
小智 6
如果您的实体类与主类不在同一个包中,则可以@EntityScan
在主类中使用注释,指定要保存或打包的实体.喜欢你的模型包.
关于:
spring.jpa.hibernate.ddl-auto = create
Run Code Online (Sandbox Code Playgroud)
您可以使用该选项update
.它不会删除任何数据,并会以相同的方式创建表.
使用以下两个设置确实有效.
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
Run Code Online (Sandbox Code Playgroud)
您需要考虑您的 Spring Boot 版本以及基于该版本下载的库的版本来提供配置。
仅当您的 Spring Boot 设置已下载 Hibernate v4 时才使用以下内容。
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
Hibernate 5 不支持以上。
如果您的 Spring Boot 安装程序已下载 Hibernate v5.x,则首选以下定义:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
重要提示:在 Spring Boot 应用程序开发中,您应该更喜欢使用注释:@SpringBootApplication
它已被超级注释:@SpringBootConfiguration and @EnableAutoConfiguration
现在如果您的实体类与主类所在的包位于不同的包中,Spring Boot 将不会扫描这些包。
因此,您需要显式定义注释:@EntityScan(basePackages = { "com.springboot.entities" })
此注释扫描基于 JPA 的注释实体类(以及其他类似 MongoDB、Cassandra 等)
注意:
“com.springboot.entities”是自定义包名称。
以下是我在 application.properties 中定义基于 Hibernate 和 JPA 的属性来创建表的方法:-
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3333/development?useSSL=true spring.datasource.username=admin
spring.datasource.password =spring.jpa.open-in-view=false
spring.jpa.hibernate.ddl-auto=创建
spring.jpa.generate-ddl=true
spring.jpa.hibernate.use-new-id-generator-mappings=true
弹簧。 jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.show-sql=true
spring.jpa .properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.format_sql=true
我可以使用上述配置创建表。
请参考它并在适用的地方更改您的代码。
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
Run Code Online (Sandbox Code Playgroud)
MySQL5Dialect 是有技巧的,以前我使用的是“MySQLDialect”
这是我在阅读上述所有答案后所做的。
spring.jpa.hibernate.ddl-auto=update
其他简单属性添加到application.properties我通过添加解决了这个问题
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.defer-datasource-initialization=true
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
169208 次 |
最近记录: |