简体   繁体   中英

Create foreign key in Spring boot - H2 database

Lets assume, I have two simple tables to implement in my Spring Boot application.

Here is the ER diagram :

spring+h2中外键的er dia

Here is my PasswordReset class :

@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;
}
}

And here is my User class (partially) :

@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;
}
}

Now my question is how can I create foreign key like my ER diagram in my Spring Boot project?

Here is my pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mua</groupId>
<artifactId>cse616</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cse616</name>
<description>Project for CSE-616</description>

<properties>
    <java.version>11</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

  • mappedBy at Parent Entity (owning Entity)
  • CascadeType.PERSIST , CascadeType.ALL to save a child with a parent at one request (you may find others according to need CascadeType )
  • @JoinColumn at a child Entity on which Column we have to join

User.java

@OneToMany(mappedBy="user",fetch=FetchType.LAZY,cascade = CascadeType.PERSIST)
private List<ResetPassword> resetpassword = new ArrayList<ResetPassword>();

ResetPassword.java

@ManyToOne(fetch = FetchType.LAZY, targetEntity = User.class)
@JoinColumn(name="userId", referencedColumnName = "userId", nullable = false)
private User user;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM