[英]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
具有Infections
在PCs
具有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.