Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

java - hibernate does not create schema on an empty databse

I have the following hibernate.cfg.xml file

enter image description here

The problem, when I run the migration, the schema is not getting created in the database. I would like to create the schema from scratch. It only generates the script.sql file.

This I the main app file

public class DatabaseMigration {


    public static void main(String[] args){

        StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("/META-INF/hibernate.cfg.xml").build();
        Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();

        SchemaExport schemaExport = new SchemaExport();
        schemaExport.setHaltOnError(true);
        schemaExport.setFormat(true);
        schemaExport.setDelimiter(";");
        schemaExport.setOutputFile("db-schema.sql");
        schemaExport.createOnly(EnumSet.of(TargetType.SCRIPT), meta);
}
}
question from:https://stackoverflow.com/questions/65945475/hibernate-does-not-create-schema-on-an-empty-databse

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks, guys, I have found the mistake.

schemaExport.createOnly(EnumSet.of(TargetType.SCRIPT), meta); was the problem

the target only specifies SCRIPT.

I have replaced it with DATABASE and it works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...