Cas*_*sey 44 hibernate spring-data spring-data-jpa spring-boot
目前,我正在使用@SpringBootApplication具有以下属性的默认注释application.properties:
spring.datasource.url=jdbc:mysql://localhost/dbname
spring.datasource.username=X
spring.datasource.password=X
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.naming_strategy=my.package.CustomNamingStrategy
Run Code Online (Sandbox Code Playgroud)
从JPA 2.1开始,我应该可以使用这些javax.persistence.schema-generation.*属性,但是在我的application.properties中设置它们似乎没有任何效果.
我见过的例子是这样那丝了一大堆额外的豆子,但他们没有使用MySQL.无论如何,这样做需要我配置春天为我提供的许多选项.
我的目标是:
我不想:
Lib版本:
hibernate : 4.3.11.FINAL
spring framework : 4.2.5.RELEASE
spring-boot : 1.3.3.RELEASE
spring-data-jpa : 1.10.1.RELEASE // for querydsl 4 support
spring-data-commons: 1.12.1.RELEASE // for querydsl 4 support
Run Code Online (Sandbox Code Playgroud)
(使用gradle,而不是maven)
Cas*_*sey 90
啊,在我发布这个问题之后,弹簧数据文档的一部分引起了我的注意:
73.5配置JPA属性 此外,当创建本地EntityManagerFactory时,spring.jpa.properties.*中的所有属性都作为普通的JPA属性(带有前缀剥离)传递.
所以,回答我自己的问题:使用spring.jpa.properties为javax.persistence属性添加前缀:
spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=create.sql
Run Code Online (Sandbox Code Playgroud)
执行此操作后,模式文件在项目根目录中自动生成.
小智 11
这是yml特定配置,用于使spring boot在根文件夹中生成ddl创建脚本:
spring:
jpa:
properties:
javax:
persistence:
schema-generation:
create-source: metadata
scripts:
action: create
create-target: create.sql
Run Code Online (Sandbox Code Playgroud)
以下代码将允许您以独立方式(独立于 Spring Boot)为所有发现的实体生成 DDL。这允许您生成架构而无需启动主应用程序。
它使用以下依赖项:
org.hibernate:hibernate-coreorg.reflections:reflectionsimport org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.reflections.Reflections;
import org.reflections.util.ConfigurationBuilder;
import javax.persistence.Entity;
import java.util.EnumSet;
import java.util.Set;
public class SchemaGenerator {
public static void main(String... args) {
new SchemaGenerator().generateSchema();
}
private void generateSchema() {
var serviceRegistry = new StandardServiceRegistryBuilder()
.applySetting("hibernate.dialect", "<fully qualifified dialect class name>")
.build();
var entities = scanForEntities("<package1>", "<package2>");
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
entities.forEach(metadataSources::addAnnotatedClass);
Metadata metadata = metadataSources.buildMetadata();
SchemaExport schemaExport = new SchemaExport();
schemaExport.setFormat(true);
schemaExport.setOutputFile("<output file name>");
schemaExport.createOnly(EnumSet.of(TargetType.SCRIPT), metadata);
}
private Set<Class<?>> scanForEntities(String... packages) {
var reflections = new Reflections(
new ConfigurationBuilder()
.forPackages(packages)
);
return reflections.getTypesAnnotatedWith(Entity.class);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35460 次 |
| 最近记录: |