gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:47:22 24 4
gpt4 key购买 nike

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 和其他博客上阅读了很多资源。所有这些都表明所采取的方法应该有效(意味着外键应该已被捕获)。然而,不知何故,外键被捕获为空。并且箱子库存记录没有链接到箱子记录。

最佳答案

我能够通过修改 createBox 方法来使其工作:

@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);
}

关于java - JHipster 具有双向一对多关系的子表中的外键为 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52453487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com