简体   繁体   English

spring 开机一对一映射单向

[英]spring boot one to one mapping unidirectional

I am confused between how to setup relation between two entities.我对如何设置两个实体之间的关系感到困惑。 I have one entity called equipment which stored all equipment profile.我有一个名为设备的实体,它存储了所有设备配置文件。 This is independent of everything.这是独立于一切的。

I have another entity called Analytics which should contain equipment.我有另一个名为 Analytics 的实体,它应该包含设备。 Note that equipment does not depend on analytics.请注意,设备不依赖于分析。 Many analytics can contain the same equipment.许多分析可以包含相同的设备。 That is if a user deletes analytics, nothing should happen to the actual equipment.也就是说,如果用户删除分析,实际设备不会发生任何事情。 What I am trying to do is when an analytic object is created I want to store which equipment it references.我想做的是在创建分析 object 时,我想存储它引用的设备。

My Equipment.java我的设备.java


@Entity
@Table(name = "km_equipment")
public class Equipment {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @Column(name = "name", unique = true)
    @NotNull
    private String name;
    private String model;

My Analytics.java我的分析.java

@Entity
@Table(name = "km_roi")
public class ROIAnalysis {


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @Column(name = "name", unique = true)
    @NotNull
    private String name;
    private String description;

    @OneToOne()
    @JoinColumn(name="id", referencedColumnName = "id", insertable = false, updatable = false)    
    Equipment equip;

I am not able to save this relationship correctly.我无法正确保存这种关系。 What am I doing wrong?我究竟做错了什么? I believe it is still one-to-one relationship because one analytics object can only contain one equipment object.我相信它仍然是一对一的关系,因为一个分析 object 只能包含一个设备 object。

This is the controller POST API这是 controller POST API

@RequestMapping(value = ControllerConstants.ADD_ROI, method = RequestMethod.POST)
    public ResponseEntity<?> addROI(@RequestBody ROIAnalysis roi) {
...
...
roiDao.save(roi);
...

I made a post call where my JSON payload is我打了一个电话,我的 JSON 有效载荷是

{
description: "132"
equipment: {id: 5, name: "June 8 equip roi", model: "tst"}
name: "123"
}

I dont see any errors.我没有看到任何错误。 In DB I dont see any column for equipment in analytics table.在数据库中,我在分析表中看不到任何设备列。 I was expecting may be the id of equipment would be stored in analytics table because when if I try to get the analytics, I want information of the equipment as well我期望设备的 ID 可能会存储在分析表中,因为当我尝试获取分析时,我也想要设备的信息

You defined the relationship as @JoinColumn(name="id", referencedColumnName = "id", insertable = false, updatable = false) Why did you define that as insertable=false, updatable=false ?您将关系定义为@JoinColumn(name="id", referencedColumnName = "id", insertable = false, updatable = false)为什么将其定义为insertable=false, updatable=false If you do so;如果你这样做; any modification made to the relationship will not be reflected to the database by hibernate. hibernate 不会将对该关系所做的任何修改反映到数据库中。 Please remove insertable = false, updatable = false part in order the relationship update to be reflected to the database.请删除insertable = false, updatable = false部分,以便将关系更新反映到数据库中。

Another thing that needs to be fixed is @JoinColumn(name="id", referencedColumnName = "id") .需要修复的另一件事是@JoinColumn(name="id", referencedColumnName = "id") This is weird;这很奇怪; join column should be sth like equipment_id !加入列应该是equipment_id ID之类的东西!

One more thing is you need to create a new ROIAnalysis entity and find and set the equipment field of that new entity to Equipment entity with id 5;还有一件事是您需要创建一个新的 ROIAnalysis 实体并找到该新实体的设备字段并将其设置为 ID 为 5 的设备实体; which you can fetch from DB and set in regards to the example you have provided.您可以从数据库中获取并根据您提供的示例进行设置。

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

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