It is convenient to save query data using JPA, in addition to adding data to your code, you can import using SQL. At the moment I only have a way to centralize the data in a single SQL file.
and data in the import often has a succession of relations, need to be imported in a serial way.
First step: Configure the configuration that requires the spring boot to have create for the data to load the SQL file automatically
[Java]View PlainCopy
- Spring.jpa.hibernate.ddl-auto=create
Step two: Put the corresponding. sql file under Src/main/resources. I am using the Import.sql file, if there is more than one SQL, it seems that the system chooses a file name to sort the files before reading.
Step Three:
Execute statement in SQL write: As the reference shows, the Pgsql insert statement. nextval (' location_seq ') is the value that specifies the value of the primary key for the corresponding self-increment sequence. For JPA, the self-increment is defined
Sequence as a primary key, if the data is inserted using a custom primary key value method, and the corresponding sequence is not processed, an error occurs when the system calls the Repository method to operate.
There is no way to save, change the situation!
[SQL]View PlainCopy
- Insert INTO location (location_id,location_is_deleted,location_name,location_parent_id,location_position_code , location_town_code,location_level_ll_id) values (nextval (' location_seq '),false,' Suide County ', 39902, ' 610826 ',' 000000 ', 3);
- insert into location (location_id,location_is_deleted,location_name, location_parent_id,location_position_code,location_town_code,location_level_ll_id) VALUES (nextval ' Location_seq '), false, ' xue Jia Mao Zhen ', 40018, ' 610826 ', ' 101000 ', 4);
- insert into location (location_id,location_is_ DELETED,LOCATION_NAME,LOCATION_PARENT_ID,LOCATION_POSITION_CODE,LOCATION_TOWN_CODE,LOCATION_LEVEL_LL_ID) values (nextval (false, ' 610826 ', ' 102000 ', 4);
- insert into location (location_id,location_is_deleted,location_name, location_parent_id,location_position_code,location_town_code,location_level_ll_id) VALUES (nextval ' Location_seq '), false, ' Ding Xian Fotuoyan town ', 40018, ' 610826 ', ' 103000 ', 4);
- insert into location (location_id,location_is_ DELETED,LOCATION_NAME,LOCATION_PARENT_ID,LOCATION_POSITION_CODE,LOCATION_TOWN_CODE,LOCATION_LEVEL_LL_ID) values (nextval (false,
- INSERT INTO location (location_id,location_is_deleted,location_name,location_parent_id,location_position_ code,location_town_code,location_level_ll_id) values (nextval (' location_seq '),false,' Yihe ', 40018, ' 610826 ',' 105000 ', 4);
JPA Hibernate Spring Repository pgsql Java Engineering (ii): SQL file import data, test data