假设我有两个简单的表要在我的Spring Boot应用程序中实现。这是ER图:这是我的PasswordReset课:@Data@Entitypublic 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;}}这是我的User课(部分):@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;}}现在我的问题是如何在我的Spring Boot项目中创建外键,如我的 ER 图?这是我的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>
1 回答
尚方宝剑之说
TA贡献1788条经验 获得超4个赞
mappedBy
在父实体(拥有实体)CascadeType.PERSIST
,CascadeType.ALL
根据一个请求保存一个孩子和一个父母(你可以根据需要找到其他的CascadeType)@JoinColumn
在我们必须加入的列的子实体
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;
添加回答
举报
0/150
提交
取消