简体   繁体   English

Spring Data JPA不会一对多填充子表

[英]Spring Data JPA not populating child table in one to many

I have a JSON like this: 我有一个像这样的JSON:

{
  "pcSignatureHash": "String",
  "pcSignature": "String",
  "infectedBy": "String",
  "agent": {
    "userCode": 0
  },
  "pc": {
    "name": "String",
    "userName": "String",
    "osType": "String",
    "domainName": "String",
    "cpuId": "String",
    "osVersion": "String",
    "macId": "String",
    "vm": true,
    "friendlyName": "String",
    "statuses": [
      {
        "firewall": true,
        "firewallTestDate": "String"
      }
    ]
  }
}

It's not storing the statuses in the database. 它没有将状态存储在数据库中。

The infections has 1 to 1 relationship with pcs and pcs has 1 to Many relationship with the statuses . 感染pc具有1对1的关系,而pc与状态之间具有1对1的关系。

The models are properly set like 1 have Infections in PCs and have Set<Statuses> in the PCs and Statuses has PCs in its model. 正确设置模型,例如1在PCs具有InfectionsPCs具有Set<Statuses> ,而Statuses在其模型中具有PCs

Is there any other way to store the data? 还有其他方法可以存储数据吗? If I store the statuses separately that doesn't make a sense it stores the PCs twice because we need to pass the PCs object as a foreign key. 如果我单独存储状态没有意义,那么它将存储两次PC,因为我们需要将PC对象作为外键传递。

You missunderstood the Relational Mapping. 您没有正确理解关系映射。 In a OneToMany Relationship you have one side with @OneToMany and one with @ManyToOne . 在OneToMany关系中,一侧与@OneToMany ,另一侧与@OneToMany @ManyToOne

Your entities should be like below . 您的实体应如下所示。 please modify accordingly. 请进行相应的修改。

Cart entity 购物车实体

@Entity
@Table(name="CART")
public class Cart {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="cart_id")
private long id;

@Column(name="total")
private double total;

@Column(name="name")
private String name;

@OneToMany(mappedBy="cart")
private Set<Items> items;

// Getter Setter methods for properties

} }

Item Entity 项目实体

@Entity
@Table(name="ITEMS")
public class Items {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private long id;

@Column(name="item_id")
private String itemId;

@Column(name="item_total")
private double itemTotal;

@Column(name="quantity")
private int quantity;

@ManyToOne
@JoinColumn(name="cart_id", nullable=false)
private Cart cart;
// Getter Setter methods for properties

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Data JPA一对多关系不保存子表 - Spring Data JPA One to Many relationship not saving child table 无法使用JPA一对多映射将子数据与父表一起插入子表中 - Not able to insert child data along with parent inside child table using JPA one to many mapping 外键未在spring boot jpa中的多对一映射中的子表中更新 - foreign key not updating in child table in many to one mapping in spring boot jpa Spring Data JPA 中的一对多关系 - One-To-Many Relationship in Spring Data JPA 一对多映射不起作用Spring数据JPA - One to Many mapping not working Spring data JPA 子实体元素不会在休眠和弹簧数据jpa的一对多映射中持续存在 - Child entity elements not persisting in one to many mapping with hibernate and spring data jpa Spring Data Jpa一对多的关系。 无法插入子记录:外键为NULL - Spring Data Jpa one to many relationship. Not able to insert child records: foreign key is coming as NULL 如何使用 Spring Boot Data JPA 在一对多映射的子实体中设置 parentId - How to set parentId in Child Entity of one to many mapping using Spring Boot Data JPA Spring Boot JPA:如何删除多对一关系的子实体 - Spring Boot JPA : How to delete child entity of a Many to One relation Spring 数据 JPA - 将@Query 与多对多关系/联接表一起使用 - Spring Data JPA - Using @Query with Many to Many Relationships / Join Table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM