我想在启动时运行Liquibase,但是更改集将不会执行。
我目前正在为大学编写JavaFX应用程序。对于我的数据库(SQLite)连接,我使用Hibernate,对于我的数据库集成,我使用Liquibase。现在,我创建了一个应该执行/更新变更集的方法。但是,当我启动应用程序时,已执行零变更集,并且我的数据库中没有表(DATABASCHANGELOG除外)。
这是我的主班:
package main;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.resource.ClassLoaderResourceAccessor;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import service.i18n.Translator;
import java.net.URL;
import java.sql.Connection;
public class Main extends Application {
private static final boolean IS_MAX_SCREEN = true;
private Stage primaryStage;
private Parent root;
@Override
public void start(Stage primaryStage) throws Exception {
URL url = getClass().getClassLoader().getResource("hibernate/hibernate.cfg.xml");
String changelogfile = getClass().getClassLoader().getResource("liquibase/master.xml").getPath();
runLiquibaseWhenNeeded(url, changelogfile);
this.primaryStage = primaryStage;
root = FXMLLoader.load(getClass().getClassLoader().getResource("fxml/main/main.fxml"));
primaryStage.setTitle(Translator.getLabel("labelProgramName"));
primaryStage.setScene(new Scene(root, 1024, 768));
setSizeMax(IS_MAX_SCREEN);
primaryStage.show();
primaryStage.setOnCloseRequest(t -> {
Platform.exit();
System.exit(0);
});
}
public static void main(String[] args) {
System.setProperty("logfile.name", System.getProperty("user.dir") + "/Logging");
launch(args);
}
/**
* Defines whether the screen should have the size of the screen or not
*
* @param isMax says if the screen should be full size or not
*/
private void setSizeMax(boolean isMax) {
if (isMax) {
primaryStage.setMaximized(true);
}
}
/**
* Create Database and runs liquibase when no DB is available
*/
private static void runLiquibaseWhenNeeded(URL url, String changelog) {
StandardServiceRegistry reg = new StandardServiceRegistryBuilder().configure(url).build();
MetadataSources metadataSources = new MetadataSources(reg);
try {
Connection con = metadataSources.getServiceRegistry().getService(ConnectionProvider.class).getConnection();
JdbcConnection jdbcConnection = new JdbcConnection(con);
DatabaseChangeLog dbChangeLog = new DatabaseChangeLog(changelog);
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection);
Liquibase liquibase = new Liquibase(dbChangeLog, new ClassLoaderResourceAccessor(), database);
liquibase.update(new Contexts(), new LabelExpression());
} catch (Exception e){
e.printStackTrace();
}
}
}
我的Hibernate cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">com.enigmabridge.hibernate.dialect.SQLiteDialect</property>
<property name="connection.driver_class">org.sqlite.JDBC</property>
<property name="connection.url">jdbc:sqlite:hotelverwaltung.db</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="entities.guest.Person"/>
<mapping class="entities.guest.Company"/>
<mapping class="entities.masterdata.room.Room"/>
<mapping class="entities.masterdata.room.Tag"/>
<mapping class="entities.masterdata.Category"/>
<mapping class="entities.masterdata.extraservice.ExtraService"/>
<mapping class="entities.masterdata.Offer"/>
<mapping class="entities.invoice.Invoice"/>
</session-factory>
</hibernate-configuration>
和我的变更日志:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
<changeSet id="create Person table and Company table" author="K">
<createTable tableName="Person">
<column name="id" type="varchar(255)">
<constraints primaryKeyName="pk_person" primaryKey="true"/>
</column>
<column name="createdAt" type="varchar(255)"/>
<column name="lastUpdatedAt" type="datetime"/>
<column name="nationality" type="varchar(255)"/>
<column name="address" type="varchar(255)"/>
<column name="postalCode" type="INTEGER"/>
<column name="city" type="varchar(255)"/>
<column name="email" type="varchar(255)"/>
<column name="phoneNumber" type="varchar(255)"/>
<column name="firstName" type="varchar(255)"/>
<column name="lastName" type="varchar(255)"/>
<column name="dateOfBirth" type="varchar(255)"/>
<column name="entitystate" type="varchar(255)"/>
</createTable>
<createTable tableName="Company">
<column name="id" type="varchar(255)">
<constraints primaryKeyName="pk_company" primaryKey="true"/>
</column>
<column name="createdAt" type="varchar(255)"/>
<column name="lastUpdatedAt" type="datetime"/>
<column name="nationality" type="varchar(255)"/>
<column name="address" type="varchar(255)"/>
<column name="postalCode" type="INTEGER"/>
<column name="city" type="varchar(255)"/>
<column name="email" type="varchar(255)"/>
<column name="phoneNumber" type="varchar(255)"/>
<column name="companyName" type="varchar(255)"/>
<column name="entitystate" type="varchar(255)"/>
</createTable>
</changeSet>
<changeSet author="m" id="create invoice table">
<createTable
tableName="Invoice">
<column name="id" type="varchar(255)">
<constraints primaryKey="true"/>
</column>
<column name="createdAt" type="varchar(255)"/>
<column name="lastUpdatedAt" type="varchar(255)"/>
<column name="entitystate" type="varchar(255)"/>
<column name="safedUnder" type="varchar(255)"/>
<column name="nameOfGuest" type="varchar(255)"/>
</createTable>
</changeSet>
<changeSet author="C" id="room_table_creation">
<createTable tableName="Room">
<column name="id" type="varchar(255)">
<constraints primaryKey="true"/>
</column>
<column name="createdAt" type="varchar(255)"/>
<column name="lastUpdatedAt" type="varchar(255)"/>
<column name="name" type="varchar(255)"/>
<column name="category" type="varchar(255)"/>
<column name="entitystate" type="varchar(255)"/>
</createTable>
</changeSet>
<changeSet author="C" id="tag_table_creation">
<createTable tableName="Tag">
<column name="id" type="varchar(255)">
<constraints primaryKey="true"/>
</column>
<column name="createdAt" type="varchar(255)"/>
<column name="lastUpdatedAt" type="varchar(255)"/>
<column name="name" type="varchar(255)"/>
<column name="entitystate" type="varchar(255)"/>
</createTable>
</changeSet>
<changeSet author="C" id="category_table_creation">
<createTable tableName="Category">
<column name="id" type="varchar(255)">
<constraints primaryKey="true"/>
</column>
<column name="createdAt" type="varchar(255)"/>
<column name="lastUpdatedAt" type="varchar(255)"/>
<column name="name" type="varchar(255)">
<constraints unique="true"/>
</column>
<column name="entitystate" type="varchar(255)"/>
</createTable>
</changeSet>
<changeSet author="C" id="room_tag_table_creation">
<createTable tableName="Room_Tag">
<column name="room_id" type="varchar(255)"/>
<column name="tags_id" type="varchar(255)"/>
</createTable>
</changeSet>
<changeSet author="M" id="extraservice_table_creation">
<createTable tableName="ExtraService">
<column name="id" type="varchar(255)">
<constraints primaryKey="true"/>
</column>
<column name="createdAt" type="varchar(255)"/>
<column name="lastUpdatedAt" type="varchar(255)"/>
<column name="availability" type="INTEGER"/>
<column name="name" type="varchar(255)"/>
<column name="amount" type="INTEGER"/>
<column name="price" type="DOUBLE"/>
<column name="entitystate" type="varchar(255)"/>
</createTable>
</changeSet>
<changeSet author="N" id="offer_table_creation">
<createTable tableName="Offer">
<column name="id" type="varchar(255)">
<constraints primaryKey="true"/>
</column>
<column name="createdAt" type="varchar(255)"/>
<column name="lastUpdatedAt" type="varchar(255)"/>
<column name="availability" type="INTEGER"/>
<column name="name" type="varchar(255)"/>
<column name="amount" type="INTEGER"/>
<column name="price" type="DOUBLE"/>
<column name="entitystate" type="varchar(255)"/>
</createTable>
</changeSet>
<changeSet author="p.hertweck" id="offer_extraservice_mapping table">
<createTable tableName="Offer_Extraservice_Mapping">
<column name="offer_id" type="varchar(255)">
<constraints nullable="false" foreignKeyName="fk_offer_id" referencedTableName="Offer(id)"/>
</column>
<column name="extraservice_id" type="varchar(255)">
<constraints nullable="false" foreignKeyName="fk_extraservice_id" referencedTableName="ExtraService(id)"/>
</column>
</createTable>
</changeSet>
<changeSet id="#booking table" author="p">
<createTable tableName="Booking">
<column name="id" type="varchar(255)">
<constraints primaryKey="true"/>
</column>
<column name="booking_id" type="DOUBLE">
<constraints nullable="false" unique="true"/>
</column>
<column name="createdAt" type="varchar(255)"/>
<column name="lastUpdatedAt" type="varchar(255)"/>
<column name="invoice_id" type="varchar(255)">
<constraints nullable="false" foreignKeyName="fk_invoice_id" referencedTableName="Invoice(id)"/>
</column>
<column name="person_id" type="varchar(255)">
<constraints nullable="true"/>
</column>
<column name="company_id" type="varchar(255)">
<constraints nullable="true"/>
</column>
<column name="offer_id" type="varchar(255)">
<constraints nullable="true"/>
</column>
<column name="room_id" type="varchar(255)">
<constraints nullable="false" foreignKeyName="fk_room_id"/>
</column>
<column name="arrival_date" type="varchar(255)"/>
<column name="depature_date" type="varchar(255)"/>
<column name="entitystate" type="varchar(255)"/>
</createTable>
</changeSet>
</databaseChangeLog>
当您需要更多信息时,请发表评论。
我希望我的数据库具有我在上面的变更集中创建的表,但是却没有这些