2015-09-28 26 views
5

jhipsterを使用しています。私はうまく動作しているアプリケーションを作成しました。 'A双方向の1対多の関係' 。それもうまくいきましたが、生成されたエンティティからオーナーの画面からすべての車がどのように表示されるかを理解できませんでした。車の画面から「所有者」を選択すると、関連する所有者が表示されます。所有者IDを選択すると、所有者画面と同様に、自分の車のリストを表示します。しかし、私の生成したエンティティ画面から、私はこの機能を見つけられませんでした。しかし、jhipsterの文書によれば、 "私たちは双方向の関係を持っていました。車のインスタンスから所有者を見つけることができ、所有者のインスタンスからすべての車を得ることができます"。車の画面で私は所有者のためのフィールドを持っていますが、所有者の画面では上記の "所有者のインスタンスからすべての車を得ることができる特定の所有者のすべての車を表示するリンクはありません"。どうすれば達成できますか?ドキュメントから、私はこの機能がjhipster生成エンティティから構築されていると感じていますが、それを理解することができませんでした。いずれかの方法でAngular jsとSpring Restコールのサンプルコードを与えて、すなわち、http://localhost:8080/#/ownersから)。jhipsterが生成したエンティティから1対多(オーナー - >車)のリンクを得ることができません

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


} 

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

    } 

基本的なCRUD操作は、所有者のために正常に動作しているOwnerRepository.java

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


} 

。しかし、今私はOwnerResource.javaに1つの残りのコールエントリを追加し、OwneRepository.javaにメソッドエントリを追加する必要があるために、特定の所有者のすべての車を取得する必要があります。私はさまざまな方法で試しましたが、多くのエラーが発生していて動作していません。以下は私が試みたものです。 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)); 
    } 

でOwnerRepository.java

Owner findAllByOwnerId(Long id);//But eclipse shows error here for this method 

私はこのために修正する必要があります。

+0

あなたがこれをもう一度尋ねたことに気づいたので、私はその質問に答えました。私は同じ質問をここに掲載しました。なぜなら、どちらが重複とみなされるのか、そしてSTOがそれについて何を決めているのかわからないからです。 –

+0

このサンプルのjittersterアプリケーションhttps://github.com/jhipster/jhipster-sample-appを見ることができます。これは、1対多の双方向関係をカバーし、ビュー側に子を持つ親を表示します。 – ismail

答えて

4

あなたは、私は新たなんだ

@ManyToOne 
@JoinColumn(name = "owner_id") 
@JsonIgnoreProperties({"cars"}) 
private Owner owner; 

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

とsimilaryにowner.java変更では、この

を試みることができます春も同様です私が同じような問題を抱えていたときに助けてくれました。

関連する問題