JPA Hibernate Spring Repository pgsql Java Engineering (ii): SQL file import data, test data

Source: Internet
Author: User

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
    1. 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
    1. 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);
    2.                 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);   
    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 (false, ' 610826 ', ' 102000 ', 4);   
    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);   
    5.                  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,
    6. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.