title: Create foreign key in Spring boot - H2 database
body: <p>Lets assume, I have two simple tables to implement in my <code>Spring Boot</code> application.</p>
  
  <p>Here is the ER diagram :</p>
  
  <p><a href="https://i.stack.imgur.com/dLCSt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dLCSt.png" alt="er dia for foreign key in spring+h2"></a></p>
  
  <p>Here is my <code>PasswordReset</code> class :</p>
  
  <pre><code>@Data
  @Entity
  public class PasswordReset {
  @Id @GeneratedValue Long passwordResetID;
  String eMail;
  String token;
  String createdAt;
  
  
  PasswordReset()
  {
  
  }
  public PasswordReset(String eMail,String token,String createdAt)
  {
      this.eMail=eMail;
      this.token=token;
      this.createdAt=createdAt;
  }
  }
  </code></pre>
  
  <p>And here is my <code>User</code> class <em>(partially)</em> :</p>
  
  <pre><code>@Data
     @Entity
     public class User {
  @Id @GeneratedValue Long UserID;
  String eMail;
  String createdAt;
  String updatedAt;
  
  
  User()
  {
  
  }
  public User(String eMail,String createdAt,String updatedAt)
  {
      this.eMail=eMail;
      this.createdAt=createdAt;
      this.updatedAt=updatedAt;
  }
  }
  </code></pre>
  
  <p>Now my question is how can I create foreign key like my ER diagram in my <strong>Spring Boot</strong> project?</p>
  
  <p>Here is my <code>pom.xml</code> :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;2.1.6.RELEASE&lt;/version&gt;
      &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  &lt;/parent&gt;
  &lt;groupId&gt;com.mua&lt;/groupId&gt;
  &lt;artifactId&gt;cse616&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;cse616&lt;/name&gt;
  &lt;description&gt;Project for CSE-616&lt;/description&gt;
  
  &lt;properties&gt;
      &lt;java.version&gt;11&lt;/java.version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web-services&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.h2database&lt;/groupId&gt;
          &lt;artifactId&gt;h2&lt;/artifactId&gt;
          &lt;scope&gt;runtime&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
          &lt;artifactId&gt;lombok&lt;/artifactId&gt;
          &lt;optional&gt;true&lt;/optional&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  
  &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p></p>