子表中具有双向“一对多”关系的JHipster外键为Null

时间:2018-09-22 04:27:33

标签: java spring-boot jhipster

jdl文件:

entity Box{
boxNum String maxlength(40)
}

entity BoxInventory{
item String maxlength(40)
quantity BigDecimal
}

relationship OneToMany {
Box to BoxInventory{box}
} 

此jdl文件生成以下类。我修改了生成的类,以在生成的模型类中添加CascadeType.ALL和FetchType.LAZY。

@Entity
@Table(name = "box")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "box")
public class Box implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;

@Size(max = 40)
@Column(name = "box_num", length = 40)
private String boxNum;

@OneToMany(mappedBy = "box", cascade = CascadeType.ALL, orphanRemoval = true)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<BoxInventory> boxInventories = new HashSet<>();

// jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getBoxNum() {
    return boxNum;
}

public Box boxNum(String boxNum) {
    this.boxNum = boxNum;
    return this;
}

public void setBoxNum(String boxNum) {
    this.boxNum = boxNum;
}

public Set<BoxInventory> getBoxInventories() {
    return boxInventories;
}

public Box boxInventories(Set<BoxInventory> boxInventories) {
    this.boxInventories = boxInventories;
    return this;
}

public Box addBoxInventory(BoxInventory boxInventory) {
    this.boxInventories.add(boxInventory);
    boxInventory.setBox(this);
    return this;
}

public Box removeBoxInventory(BoxInventory boxInventory) {
    this.boxInventories.remove(boxInventory);
    boxInventory.setBox(null);
    return this;
}

public void setBoxInventories(Set<BoxInventory> boxInventories) {
    this.boxInventories = boxInventories;
}
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    Box box = (Box) o;
    if (box.getId() == null || getId() == null) {
        return false;
    }
    return Objects.equals(getId(), box.getId());
}

@Override
public int hashCode() {
    return Objects.hashCode(getId());
}

@Override
public String toString() {
    return "Box{" +
        "id=" + getId() +
        ", boxNum='" + getBoxNum() + "'" +
        "}";
}
}

@Entity
@Table(name = "box_inventory")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "boxinventory")
public class BoxInventory implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;

@Size(max = 40)
@Column(name = "item", length = 40)
private String item;

@Column(name = "quantity", precision = 10, scale = 2)
private BigDecimal quantity;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "box_id", nullable = false)
@JsonIgnoreProperties("boxInventories")
private Box box;

// jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getItem() {
    return item;
}

public BoxInventory item(String item) {
    this.item = item;
    return this;
}

public void setItem(String item) {
    this.item = item;
}

public BigDecimal getQuantity() {
    return quantity;
}

public BoxInventory quantity(BigDecimal quantity) {
    this.quantity = quantity;
    return this;
}

public void setQuantity(BigDecimal quantity) {
    this.quantity = quantity;
}

public Box getBox() {
    return box;
}

public BoxInventory box(Box box) {
    this.box = box;
    return this;
}

public void setBox(Box box) {
    this.box = box;
}
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    BoxInventory boxInventory = (BoxInventory) o;
    if (boxInventory.getId() == null || getId() == null) {
        return false;
    }
    return Objects.equals(getId(), boxInventory.getId());
}

@Override
public int hashCode() {
    return Objects.hashCode(getId());
}

@Override
public String toString() {
    return "BoxInventory{" +
        "id=" + getId() +
        ", item='" + getItem() + "'" +
        ", quantity=" + getQuantity() +
        "}";
}
}

@SuppressWarnings("unused")
@Repository
public interface BoxRepository extends JpaRepository<Box, Long> {
}

public interface BoxSearchRepository extends ElasticsearchRepository<Box, Long> {
}

@RestController
@RequestMapping("/api")
public class BoxResource {

private final Logger log = LoggerFactory.getLogger(BoxResource.class);

private static final String ENTITY_NAME = "modelBox";

private final BoxService boxService;

public BoxResource(BoxService boxService) {
    this.boxService = boxService;
}

/**
 * POST  /boxes : Create a new box.
 *
 * @param box the box to create
 * @return the ResponseEntity with status 201 (Created) and with body the new box, or with status 400 (Bad Request) if the box has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/boxes")
@Timed
public ResponseEntity<Box> createBox(@Valid @RequestBody Box box) throws URISyntaxException {
    log.debug("REST request to save Box : {}", box);
    if (box.getId() != null) {
        throw new BadRequestAlertException("A new box cannot already have an ID", ENTITY_NAME, "idexists");
    }
    Box result = boxService.save(box);
    return ResponseEntity.created(new URI("/api/boxes/" + result.getId()))
        .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
        .body(result);
}
}

问题是,当我使用以下输入调用createBox时:

{
  "boxInventories": [
    {
      "item": "I1",
      "quantity": 10
    }
  ],
  "boxNum": "B2"
}

记录同时插入到Box和Box_Inventory表中。但是,Box_Inventory表中的box_id列为空。

这是我在日志中看到的:

2018-09-22 12:10:41.375 DEBUG 1596 --- [  XNIO-2 task-1] c.s.inventory.aop.logging.LoggingAspect  : Enter: com.sb.inventory.web.rest.BoxResource.createBox() with argument[s] = [Box{id=null, boxNum='B2'}]
2018-09-22 12:10:41.389 DEBUG 1596 --- [  XNIO-2 task-1] com.sb.inventory.web.rest.BoxResource    : REST request to save Box : Box{id=null, boxNum='B2'}
2018-09-22 12:10:41.412 DEBUG 1596 --- [  XNIO-2 task-1] c.s.inventory.aop.logging.LoggingAspect  : Enter: com.sb.inventory.service.impl.BoxServiceImpl.save() with argument[s] = [Box{id=null, boxNum='B2'}]
2018-09-22 12:10:41.425 DEBUG 1596 --- [  XNIO-2 task-1] c.s.i.service.impl.BoxServiceImpl        : Request to save Box : Box{id=null, boxNum='B2'}
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
2018-09-22 12:10:42.341 DEBUG 1596 --- [  XNIO-2 task-1] c.s.inventory.aop.logging.LoggingAspect  : Exit: com.sb.inventory.service.impl.BoxServiceImpl.save() with result = Box{id=2401, boxNum='B2'}
Hibernate: insert into box (box_num, id) values (?, ?)
Hibernate: insert into box_inventory (box_id, item, quantity, id) values (?, ?, ?, ?)
2018-09-22 12:10:42.505 DEBUG 1596 --- [  XNIO-2 task-1] c.s.inventory.aop.logging.LoggingAspect  : Exit: com.sb.inventory.web.rest.BoxResource.createBox() with result = <201 Created,Box{id=2401, boxNum='B2'},{Location=[/api/boxes/2401], X-modelApp-alert=[A new modelBox is created with identifier 2401], X-modelApp-params=[2401]}>

我阅读了很多关于stackoverflow和其他博客的资源。所有人都认为所采用的方法应该有效(这意味着应该已经捕获了外键)。但是,以某种方式,外键被捕获为null。并且包装盒库存记录未链接到包装盒记录。

1 个答案:

答案 0 :(得分:0)

我能够将其修改为以下方法来工作:

@PostMapping("/boxes")
@Timed
public ResponseEntity<Box> createBox(@Valid @RequestBody Box box) throws URISyntaxException {
    log.debug("REST request to save Box : {}", box);
    if (box.getId() != null) {
        throw new BadRequestAlertException("A new box cannot already have an ID", ENTITY_NAME, "idexists");
    }
    Set<BoxInventory> boxinventories  = box.getBoxInventories();
    log.debug("box inventories : " + Arrays.toString(boxinventories.toArray()));
    for (BoxInventory boxInventory : boxinventories) {
        log.debug("adding box inventory : " + boxInventory);
        box.addBoxInventory(boxInventory);
    }
    log.debug("added all box inventories : " + box);
    Box result = boxService.save(box);
    return ResponseEntity.created(new URI("/api/boxes/" + result.getId()))
        .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
        .body(result);
}