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

sma*_*987 6 spring-data-jpa angularjs spring-data-rest jhipster

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

Owner.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<>();


}
Run Code Online (Sandbox Code Playgroud)

OwnerResource.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));
        }

    }
Run Code Online (Sandbox Code Playgroud)

OwnerRepository.java

/**
 * Spring Data JPA repository for the Owner entity.
 */
public interface OwnerRepository extends JpaRepository<Owner,Long> {    


}
Run Code Online (Sandbox Code Playgroud)

基本的crud操作对所有者来说很好.但是现在我需要获得特定所有者的所有车辆,因为我需要添加一个休息呼叫条目OwnerResource.java和一个方法条目OwneRepository.java.我尝试了不同的方法,但得到了很多错误,但是没有用.以下是我的尝试.

在OwnerRepository.java中

Owner findAllByOwnerId(Long id);//But eclipse shows error here for this method
Run Code Online (Sandbox Code Playgroud)

在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));
    }
Run Code Online (Sandbox Code Playgroud)

我需要解决这个问题.

小智 6

你可以试试这个

在 owner.java 更改

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

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

和 car.java 中的相似

@ManyToOne
@JoinColumn(name = "owner_id")
@JsonIgnoreProperties({"cars"})
private Owner owner;
Run Code Online (Sandbox Code Playgroud)

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