A Quick Look at the Schema
Listing 5 illustrates the generated database schema.
Listing 5The Auto-Generated Schema
alter table USERS drop constraint FK4D495E8E2D9B04A; drop table USERS if exists; create table USERS ( USER_ID bigint generated by default as identity (start with 1), ADDRESS_STREET varchar(255), ADDRESS_CITY varchar(255), USER_NAME varchar(255), NEXT_USER_ID bigint, primary key (USER_ID) ); alter table USERS add constraint FK4D495E8E2D9B04A foreign key (NEXT_USER_ID) references USERS;
Listing 5 consists of four sections:
- Drop any existing constraint
- Drop the USERS table
- Create the USERS table
- Alter the USERS table
In Listing 5, a foreign key is defined in the USERS table with the line:
foreign key (NEXT_USER_ID)
This line of SQL provides for linking different rows of the table. And as you saw in Listing 4, it’s easy to create the relationship. It’s worth studying the schema and comparing it with the Java code because the essence of ORM is to bridge these two very different worlds.
Not everything happens from within Java code. Sometimes, you have to manually run some SQL, so it’s important to learn how to do this with your selected database product. Let’s see how it’s done with HSQL.