Using jhipster, I created an application that works fine, then I created βOne-to-Many Bidirectional Relationships,β Owner for a Car. βIt also works great, but I couldnβt figure out how all cars from Ownerβs screen would be displayed objects. On the Cars screen, if I select Owner, the corresponding owner is displayed. Similarly, from the Owner screen, if I select Owner ID, I want to display a list of his cars. But from the screens of the created entities I did not find this function, however in the document those jhipster said: "We had two-way relationship: from an instance of Car you can find the owner, and from the instance owner, you can get all the cars" on the car screen, I have a field for the owner, but the owner of the screen I do not have any. links to display all the cars of a particular owner, as described above, β from the instance of the Owner, you can get all your cars .β How can I achieve this? From the document, I believe that this function is built from jhipster created objects, but I could not understand if anyone could give a code example for Angular js and Spring Rest call to display all cars of a specific owner from the owners page (i.e. from 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<>(); }
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)); } }
OwnerRepository.java
public interface OwnerRepository extends JpaRepository<Owner,Long> { }
The main crud operation works great for the owner. But now I need to get all the cars of a certain owner, because I need to add one record of the rest call in OwnerResource.java and a method record in OwneRepository.java . I tried different ways, but getting a lot of errors and not working. The following is what I tried.
In OwnerRepository.java
Owner findAllByOwnerId(Long id);//But eclipse shows error here for this method
In 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)); }
I need to fix this.