Populating some data

To be able to test the repositories that we will create, we will populate some test data into these tables. All that we need to do is include the file called data.sql with the following statements in srcmain esources:

insert into user (id, name, userid)
values (1, 'User Name 1', 'UserId1');

insert into user (id, name, userid)
values (2, 'User Name 2', 'UserId2');

insert into user (id, name, userid)
values (3, 'User Name 3', 'UserId3');

insert into user (id, name, userid)
values (4, 'User Name 4', 'UserId4');

insert into todo (id, title, description, is_done, target_date, userid)
values (101, 'Todo Title 1', 'Todo Desc 1', false, CURRENT_DATE(), 1);

insert into todo (id, title, description, is_done, target_date, userid)
values (102, 'Todo Title 2', 'Todo Desc 2', false, CURRENT_DATE(), 1);

insert into todo (id, title, description, is_done, target_date, userid)
values (103, 'Todo Title 3', 'Todo Desc 3', false, CURRENT_DATE(), 2);

These are simple insert statements. We are creating a total of four usersthe first user has 2 todos, the second user has 1 todo, and the last two users have none.

You can enable debug log to see the following logs using logging.level.org.springframework.jdbc=DEBUG in application.properties.

When you run SpringDataJpaFirstExampleApplication as a Java application again, you will see a few extra statements in the log:

ScriptUtils : Executing SQL script from URL [file:/in28Minutes/Workspaces/SpringDataJPA-Preparation/Spring-Data-JPA-Trial-Run/target/classes/data.sql]

ScriptUtils : Executed SQL script from URL [file:/in28Minutes/Workspaces/SpringDataJPA-Preparation/Spring-Data-JPA-Trial-Run/target/classes/data.sql] in 42 ms.

The log statements confirm that the data is being populated into the H2 in-memory database. Let's turn our attention to creating repositories to access and manipulate the data from the Java code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset