1

就我而言:

Rack 和 Slot 是具有自定义 ID 生成器的实体。我正在使用单向一对一映射。维度表将使用自动生成的自定义 ID 作为多个表的外键保存数据(此处以机架和插槽为例)。我的模式如下所示: Rack ------> Dimension <-----------Slot 其中 Dimension 将保存具有生成 ID 的 Rack 和 Slot 表的数据。

这里的问题是,当我像这样保存数据时: -

Rack rack = new Rack(params);
Dimension dim = new Dimension(params);
rack.setDimension(dim);
session.save(rack);

在机架和维度表中使用相同的自动生成 ID 成功保存数据。

但是当我为 Slot 表保存数据时:

Slot Slot = new Slot(params);
Dimension dim = new Dimension(params);
slot.setDimension(dim);
session.save(slot);

它显示错误消息为:-

试图从 null 一对一属性分配 id: rack

保存 Slot 和 Dimension 的数据时,我可以将动态属性名称作为“slot”传递,而在保存 Rack 和 Dimension 的数据时,我可以将动态属性名称传递为“rack”吗?

机架.java

@Entity
@Table(name="tablename")
@GenericGenerator(name = "customseq", strategy = "CustomIdGenerator")
public class Rack {
  @Id
  @GeneratedValue(generator = "customseq")
  @Column(name = "uni_id")
  private String id;
  @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  @PrimaryKeyJoinColumn
  private Dimension dimension;
  // Getters and Setters
}

插槽.java

@Entity
@GenericGenerator(name = "slot_seq", strategy = "com.slot.SlotIdGenerator")
@Table(name = "\"slot_info\"")
public class Slot implements Serializable {

  @Id
  @GeneratedValue(generator = "slot_seq")
  @Column(name = "slot_id")
  private String slotid;
  @Column(name = "rack_id")
  private String rackid;
  @Column(name = "element_id")
  private String elementid;
  @Column(name = "sr_no")
  private int srno;   
  @JoinColumn(name = "systemid", insertable = false, updatable = false)
  @PrimaryKeyJoinColumn
  private Dimension dimension;
  // getters and setters
}

维度.java

public class Dimension implements Serializable{
  @Id
  @Column(name = "systemid")
  @GeneratedValue(generator = "foreign")
  @GenericGenerator(name = "foreign", strategy = "foreign", parameters = {
  @Parameter(name = "property", value = "slot"),
  @Parameter(name = "property", value = "rack")})
  private String systemid;

  @OneToOne(mappedBy = "dimension", fetch = FetchType.LAZY)
  @PrimaryKeyJoinColumn
  private Rack rack;
  @OneToOne(mappedBy = "dimension", fetch = FetchType.LAZY)
  @PrimaryKeyJoinColumn
  private Slot slot;
  // Getters and Setters
}
4

0 回答 0