gpt4 book ai didi

angularjs - 无法从 jhipster 生成的实体获得一对多(所有者 -> 汽车)链接

转载 作者:行者123 更新时间:2023-12-04 15:20:32 33 4
gpt4 key购买 nike

使用 jhipster 我创建了运行良好的应用程序,然后我创建了“双向一对多关系”,所有者到汽车。这也工作正常,但我无法弄清楚如何从生成的实体的所有者屏幕中显示所有汽车。如果我选择“车主”,则会从汽车屏幕显示相关车主。同样,如果我选择 Owner Id,则在 Owner 屏幕中,我想显示他的汽车列表。但是从我生成的实体屏幕中我没有找到这个功能。然而,jhipster 文档说“ 我们有一个双向关系:从 Car 实例你可以找到它的主人,从一个 Owner 实例你可以得到它的所有汽车 ”。在汽车屏幕中,我有一个车主字段,但在车主屏幕中,我没有任何链接来显示特定车主的所有汽车,如上所述“ 从车主实例中,您可以获得其所有汽车 ”。我怎样才能实现它?从文档中我觉得这个功能是由 jhipster 生成的实体构建的,但是我无法弄清楚,任何人都可以提供 Angular js 和 Spring Rest 调用的示例代码,以显示来自车主页面的特定车主的所有汽车(即,来自 http://localhost:8080/#/owners )。

所有者.java

@Entity
@Table(name = "OWNER")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Owner implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

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

@Column(name = "age")
private Integer age;

@OneToMany(mappedBy = "owner")
@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Car> cars = new HashSet<>();


}

所有者资源.java
    @RestController
@RequestMapping("/api")
public class OwnerResource {
private final Logger log = LoggerFactory.getLogger(OwnerResource.class);
@Inject
private OwnerRepository ownerRepository;

@RequestMapping(value = "/owners",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<Owner> create(@RequestBody Owner owner) throws URISyntaxException {
log.debug("REST request to save Owner : {}", owner);
if (owner.getId() != null) {
return ResponseEntity.badRequest().header("Failure", "A new owner cannot already have an ID").body(null);
}
Owner result = ownerRepository.save(owner);
return ResponseEntity.created(new URI("/api/owners/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert("owner", result.getId().toString()))
.body(result);
}

@RequestMapping(value = "/owners",
method = RequestMethod.PUT,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<Owner> update(@RequestBody Owner owner) throws URISyntaxException {
log.debug("REST request to update Owner : {}", owner);
if (owner.getId() == null) {
return create(owner);
}
Owner result = ownerRepository.save(owner);
return ResponseEntity.ok()
.headers(HeaderUtil.createEntityUpdateAlert("owner", owner.getId().toString()))
.body(result);
}

@RequestMapping(value = "/owners",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<List<Owner>> getAll(@RequestParam(value = "page" , required = false) Integer offset,
@RequestParam(value = "per_page", required = false) Integer limit)
throws URISyntaxException {
Page<Owner> page = ownerRepository.findAll(PaginationUtil.generatePageRequest(offset, limit));
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/owners", offset, limit);
return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}

@RequestMapping(value = "/owners/{id}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<Owner> get(@PathVariable Long id) {
log.debug("REST request to get Owner : {}", id);
return Optional.ofNullable(ownerRepository.findOne(id))
.map(owner -> new ResponseEntity<>(
owner,
HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

}

所有者存储库.java
/**
* Spring Data JPA repository for the Owner entity.
*/
public interface OwnerRepository extends JpaRepository<Owner,Long> {


}

基本的 crud 操作对 Owner 来说工作正常。但是现在我需要获取特定所有者的所有汽车,为此我需要在 OwnerResource.java 中添加一个休息调用条目以及 OwneRepository.java 中的方法条目.我尝试了不同的方法,但遇到了很多错误并且无法正常工作。以下是我尝试过的。

在 OwnerRepository.java 中
Owner findAllByOwnerId(Long id);//But eclipse shows error here for this method

在 OwnerResource.java 中
//Get All Cars
@RequestMapping(value = "/{id}/cars",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<Owner> getAll(@PathVariable Long id) {
log.debug("REST request to get All Cars of the Owner : {}", id);
return Optional.ofNullable(ownerRepository.findAllByOwnerId(id))
.map(owner -> new ResponseEntity<>(
owner,
HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

我需要解决这个问题。

最佳答案

你可以试试这个

在 owner.java 更改

@OneToMany(mappedBy = "owner")
@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Car> cars = new HashSet<>();


@OneToMany(mappedBy = "owner", fetch = FetchType.EAGER)
@JsonIgnoreProperties({"owner"})
private Set<Car> cars = new HashSet<>();

和 car.java 中的相似
@ManyToOne
@JoinColumn(name = "owner_id")
@JsonIgnoreProperties({"cars"})
private Owner owner;

我也是 spring 的新手,但是当我遇到类似的问题时,这对我有帮助。

关于angularjs - 无法从 jhipster 生成的实体获得一对多(所有者 -> 汽车)链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32830569/

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