英文:
How to implement One to Many relationship in Spring Boot JPA using Flyway
问题
我想要为下面的实体建立一对多的关系。(membership_plan可以有多个membership_plan_features)。我正在使用Spring Boot 3.0.6。
@Entity
@Table(name = "membership_plan")
public class MembershipPlan {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long planId;
@Column(name = "org_id")
private String orgId;
@Column(name = "plan_name")
private String planName;
@Column(name = "plan_price")
private String planPrice;
@OneToMany(mappedBy = "membershipPlan")
private List<MembershipPlanFeature> planFeature;
}
第二个实体
@Entity
@Table(name = "membership_plan_feature")
@Getter
@Setter
public class MembershipPlanFeature {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "org_id")
private String orgId;
@Column(name = "feature_name")
private String featureName;
@Column(name = "feature_desc")
private String featureDesc;
@ManyToOne
@JoinColumn(name="plan_id")
private MembershipPlan membershipPlan;
}
以下是Flyway查询:
CREATE TABLE membership_plan (
plan_id INT NOT NULL,
org_id VARCHAR(255) NULL,
plan_name VARCHAR(255) NULL,
plan_price VARCHAR(255) NULL,
PRIMARY KEY (plan_id)
);
CREATE TABLE membership_plan_feature (
id INT AUTO_INCREMENT NOT NULL,
org_id VARCHAR(255) NULL,
plan_id INT NOT NULL,
feature_name VARCHAR(255) NULL,
feature_desc VARCHAR(255) NULL,
PRIMARY KEY (id)
);
ALTER TABLE membership_plan_feature
ADD CONSTRAINT fk_membership_plan_feature
FOREIGN KEY (plan_id)
REFERENCES membership_plan (plan_id);
现在,当我查询 membershipPlanRepository.findAll();
时,返回了递归数据:
[
{
"planId": 1,
"orgId": "101",
"planName": "GOLD",
"planPrice": "29",
"planFeature": [
{
"id": 1,
"orgId": "101",
"featureName": "Kanban",
"featureDesc": "Kanban",
"membershipPlan": {
"planId": 1,
"orgId": "101",
"planName": "GOLD",
"planPrice": "29",
"planFeature": [
{
"id": 1,
"orgId": "101",
"featureName": "Kanban",
"featureDesc": "Kanban",
"membershipPlan": {
"planId": 1,
"orgId": "101",
"planName": "GOLD",
"planPrice": "29",
"planFeature": [... 和后续数据
以下是异常信息:
Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError)]
请告诉我我做错了什么地方。
英文:
I want to have one-to-many relationship for the below entities.(membership_plan can have multiple membership_plan_features). I am using spring boot 3.0.6.
@Entity
@Table(name = "membership_plan")
public class MembershipPlan {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long planId;
@Column(name = "org_id")
private String orgId;
@Column(name = "plan_name")
private String planName;
@Column(name = "plan_price")
private String planPrice;
@OneToMany (mappedBy = "membershipPlan")
private List<MembershipPlanFeature> planFeature;
}
Second entity
@Entity
@Table(name = "membership_plan_feature")
@Getter
@Setter
public class MembershipPlanFeature {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "org_id")
private String orgId;
@Column(name = "feature_name")
private String featureName;
@Column(name = "feature_desc")
private String featureDesc;
@ManyToOne
@JoinColumn(name="plan_id")
private MembershipPlan membershipPlan;
}
Below are the flyway queries:
CREATE TABLE membership_plan (
plan_id INT NOT NULL,
org_id VARCHAR(255) NULL,
plan_name VARCHAR(255) NULL,
plan_price VARCHAR(255) NULL,
PRIMARY KEY (plan_id)
);
CREATE TABLE membership_plan_feature (
id INT AUTO_INCREMENT NOT NULL,
org_id VARCHAR(255) NULL,
plan_id INT NOT NULL,
feature_name VARCHAR(255) NULL,
feature_desc VARCHAR(255) NULL,
PRIMARY KEY (id)
);
ALTER table membership_plan_feature
ADD CONSTRAINT fk_membership_plan_feature
FOREIGN KEY (plan_id)
REFERENCES membership_plan (plan_id);
Now, when I query membershipPlanRepository.findAll();
it return me a recursive data:
[
{
"planId": 1,
"orgId": "101",
"planName": "GOLD",
"planPrice": "29",
"planFeature": [
{
"id": 1,
"orgId": "101",
"featureName": "Kanban",
"featureDesc": "Kanban",
"membershipPlan": {
"planId": 1,
"orgId": "101",
"planName": "GOLD",
"planPrice": "29",
"planFeature": [
{
"id": 1,
"orgId": "101",
"featureName": "Kanban",
"featureDesc": "Kanban",
"membershipPlan": {
"planId": 1,
"orgId": "101",
"planName": "GOLD",
"planPrice": "29",
"planFeature": [... and so on
below is the exception:
Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError)]
Please let me know where I am doing it wrong.
答案1
得分: 1
你必须告诉Jackson跳过为你的实体生成嵌套字段:
...
@ManyToOne
@JoinColumn(name = "plan_id")
@JsonBackReference // 添加这个
private MembershipPlan membershipPlan;
...
...
@OneToMany(mappedBy = "membershipPlan")
@JsonManagedReference // 添加这个
private List<MembershipPlanFeature> planFeature;
...
英文:
You have to tell Jackson to skip generate nested fields to your entities:
...
@ManyToOne
@JoinColumn(name = "plan_id")
@JsonBackReference // Add this
private MembershipPlan membershipPlan;
...
...
@OneToMany(mappedBy = "membershipPlan")
@JsonManagedReference // Add this
private List<MembershipPlanFeature> planFeature;
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论