SPRING JPA-批量插入2个数据库(azure和h2)

时间:2019-02-12 15:12:40

标签: java hibernate azure h2 bulk

我对Spring Data JPA / Hibernate有问题。 我使用2个数据库,Azure SQL用于Dev / Prod,H2用于测试和参数:

  • spring.jpa.hibernate.ddl-auto =创建
  • spring.datasource.initialization-mode =始终

我有一个很大的data.sql,它有很多约束。所以我需要禁用外键。

目前,我使用2个data.sql,但是它带来了维护问题:

(样本) azure-data.sql

DECLARE @enable_constraints bit = 0

--Don't change anything below this line.
DECLARE @schema_name SYSNAME
DECLARE @table_name  SYSNAME

DECLARE table_cursor CURSOR FOR
  SELECT
    schemas.name,
    tables.name
  FROM
    sys.tables
      INNER JOIN sys.schemas ON tables.schema_id = schemas.schema_id

OPEN table_cursor
FETCH NEXT FROM table_cursor INTO @schema_name, @table_name

DECLARE @cmd varchar(200)
WHILE @@FETCH_STATUS = 0
BEGIN
  SET @cmd = 'ALTER TABLE ' + QUOTENAME(@schema_name) + '.' + QUOTENAME(@table_name) + ' '
  SET @cmd = @cmd + (CASE WHEN @enable_constraints = 1 THEN 'CHECK' ELSE 'NOCHECK' END) + ' CONSTRAINT ALL'

  PRINT @cmd
  EXEC( @cmd )

  FETCH NEXT FROM table_cursor INTO @schema_name, @table_name
END

CLOSE table_cursor
DEALLOCATE table_cursor


SET IDENTITY_INSERT Rec.dev.address ON  ;
INSERT INTO  Rec.dev.address (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, city, label, comment, end_date, line1, line2, line3, postal_code, start_date, id_country) VALUES (1, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, 'Strasbourg', 'Addresse', null, null, '1 Place Brant', null, null, '67000', null, 1);
INSERT INTO  Rec.dev.address (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, city,  label, comment, end_date, line1, line2, line3, postal_code, start_date, id_country) VALUES (2, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, 'Strasbourg','Addresse de facturaction',  null, null, '1 Place Brant', null, null, '67000', null, 1);
INSERT INTO  Rec.dev.address (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, city,  label, comment, end_date, line1, line2, line3, postal_code, start_date, id_country) VALUES (3, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, 'Strasbourg', null,  null, null, '1 Place Brant', null, null, '67000', null, 1);

SET IDENTITY_INSERT Rec.dev.address OFF  ;
SET IDENTITY_INSERT Rec.dev.country ON  ;

INSERT INTO Rec.dev.country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (1, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 1, 1, 'FR', 'France');
INSERT INTO Rec.dev.country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (2, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 1, 1, 'GE', 'Allemagne');
INSERT INTO Rec.dev.country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (4, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 0, 0, 'ABW', 'Aruba');
INSERT INTO Rec.dev.country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (5, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 0, 0, 'AFG', 'Afghanistan');
INSERT INTO Rec.dev.country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (6, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 0, 0, 'AGO', 'Angola');
INSERT INTO Rec.dev.country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (7, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 0, 0, 'AIA', 'Anguilla');

SET IDENTITY_INSERT Rec.dev.country OFF  ;

DECLARE @enable_constraints bit = 0

h2-data.sql

SET REFERENTIAL_INTEGRITY false;


INSERT INTO address (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, city, label, comment, end_date, line1, line2, line3, postal_code, start_date, id_country) VALUES (1, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, 'Strasbourg', 'Addresse', null, null, '1 Place Brant', null, null, '67000', null, 1);
INSERT INTO address (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, city,  label, comment, end_date, line1, line2, line3, postal_code, start_date, id_country) VALUES (2, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, 'Strasbourg','Addresse de facturaction',  null, null, '1 Place Brant', null, null, '67000', null, 1);
INSERT INTO address (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, city,  label, comment, end_date, line1, line2, line3, postal_code, start_date, id_country) VALUES (3, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, 'Strasbourg', null,  null, null, '1 Place Brant', null, null, '67000', null, 1);
INSERT INTO application (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, label, owner_id, company_id) VALUES (1, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, null, 1, 1);
INSERT INTO country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (1, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 1, 1, 'FR', 'France');
INSERT INTO country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (2, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 1, 1, 'GE', 'Allemagne');
INSERT INTO country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (4, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 0, 0, 'ABW', 'Aruba');
INSERT INTO country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (5, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 0, 0, 'AFG', 'Afghanistan');
INSERT INTO country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (6, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 0, 0, 'AGO', 'Angola');
SET REFERENTIAL_INTEGRITY true;

如何对Azure SQL和H2使用1个单个文件?使用SQL还是其他格式? 插入后,弹簧/休眠状态是否可以更改表?

0 个答案:

没有答案