简体   繁体   English

hql查询将数据持久化在具有多对一关系的表中

[英]hql query to persist data in table having many to one relationship

@Entity(name="card")
public class Card {

    @Id
    @GenericGenerator(name="generator",strategy="increment")
    @GeneratedValue(generator="generator")
    private int id;

    @Transient
    private int slotID;

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

    private String cardType;

    /* getter and setters */
}

@Entity(name="PhysicalPort")
public class PhysicalPort  {

    @Id
    @GenericGenerator(name="generator",strategy="increment")
    @GeneratedValue(generator="generator")
    private int id;

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

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="cardId")
    private Card cardInfo;

    /* getter and setters */
}

In repository class I am using below query to persist data 在存储库类中,我正在使用以下查询来持久化数据

sessionFactory.getCurrentSession().persist(physicalPort);

while persisting data i need to add data only in PhysicalPort table with card table id as reference key, but data is added in card table also with a new id. 在保留数据的同时,我只需要在PhysicalPort表中添加数据,并将卡表ID作为参考键,但是数据也会在卡表中添加新ID。

You save two cards because your object is passed as new object: 您保存两张卡片,因为您的对象作为新对象传递:

This is your Json: { "cardPort": "1", "interfaceName": "E1", "state": "enable", "fec": true, "systemLoopback": false, "rxBytes": 513, "rxPackets": 0, "rxErrorPackets": 571017851, "rxPacketsDrops": 1852797802, "txBytes": 1869116517, "txPackets": 154804836, "txErroPackets": 0, "txPacketsDrops": 0, "time": "23:56:35", "memoryTotal": 4278190080, "memoryFree": 4294967295, "cpuuser": 996646, "cpusys": 104071, "cardInfo": { "cardType":"TDM", "macId":"12" } } 这是您的Json: { "cardPort": "1", "interfaceName": "E1", "state": "enable", "fec": true, "systemLoopback": false, "rxBytes": 513, "rxPackets": 0, "rxErrorPackets": 571017851, "rxPacketsDrops": 1852797802, "txBytes": 1869116517, "txPackets": 154804836, "txErroPackets": 0, "txPacketsDrops": 0, "time": "23:56:35", "memoryTotal": 4278190080, "memoryFree": 4294967295, "cpuuser": 996646, "cpusys": 104071, "cardInfo": { "cardType":"TDM", "macId":"12" } }

Your cardInfo proprerty is setted in this way: 您的cardInfo属性设置如下:

"cardInfo": { "cardType":"TDM", "macId":"12" }

So, you tell at entity manager, to add a cardInfo without id (id = null) because is not specified. 因此,您告诉实体管理器添加一个没有id的cardInfo(id = null),因为未指定。

So, when you try to save PhysicalPort object the behaviour is: 因此,当您尝试保存PhysicalPort对象时,其行为是:

Check the foreign key fields (your cardInfo property), that object hasn't id valued, so that is a new object -> INSERT INTO 检查外键字段(您的cardInfo属性),该对象没有id值,因此是一个新对象-> INSERT INTO

Check PhysicalPort object and add a new row in PhysicalPort. 检查PhysicalPort对象,然后在PhysicalPort中添加新行。

If you want fix your code, when you pass the cardInfo object explicit the value of ID of your row presents in that table, as follow: 如果要修复代码,则在显式传递cardInfo对象时,将在该表中显示行ID的值,如下所示:

"cardInfo": { "id":"value of your row id", "cardType":"TDM", "macId":"12" }

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

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