title: How to get Many To One object displayed in JSON format
body: <p>I have two entities - BaseEvent (sport event) and Team:</p>
  
  <p>Team:</p>
  
  <pre><code>@Entity
  @Table(name = "teams")
  @EntityListeners(AuditingEntityListener.class)
  public class Team extends AbstractPersistable&lt;Long&gt; {
      private Long id;
  
      @NotBlank
      private String nameOfTeam;
  
      // we will create one transient field for leagueId
      @Transient
      private Long leagueId;
  
      @ManyToOne
      @JoinColumn(name = "league_id")
      @JsonBackReference
      private League league;
  
      // team promenlivata ot player
      @OneToMany(targetEntity = Player.class, mappedBy = "team", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
      @JsonManagedReference
      private Set&lt;Player&gt; players = new HashSet&lt;&gt;();
  
  
  
      public Team(String nameOfTeam) {
          this.nameOfTeam = nameOfTeam;
      }
  
      public Team() {
      }
  
  
      public League getLeague() {
          return league;
      }
  
      public void setLeague(League league) {
          this.league = league;
      }
  
      public String getNameOfTeam() {
          return nameOfTeam;
      }
  
      public void setNameOfTeam(String nameOfTeam) {
          this.nameOfTeam = nameOfTeam;
      }
  
      public Long getLeagueId() {
          return leagueId;
      }
  
      public void setLeagueId(Long leagueId) {
          this.leagueId = leagueId;
      }
  
      public Set&lt;Player&gt; getPlayers() {
          return players;
      }
  
      public void setPlayers(Set&lt;Player&gt; players) {
          this.players = players;
      }
  
      @Override
      public Long getId() {
          return id;
      }
  
      @Override
      public void setId(Long id) {
          this.id = id;
      }
  }
  </code></pre>
  
  <p>BaseEntity:</p>
  
  <pre><code>  @Entity
    @Table(name = "base_events")
    @EntityListeners(AuditingEntityListener.class)
    public class BaseEvent extends AbstractPersistable&lt;Long&gt; {
  
      private Long id;
  
  
      @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy HH:mm:ss")
      @NotNull(message = "Start time cannot be empty")
      @Column
      private Date startTime;
  
  
      // we will create one transient field for teamId
      @Transient
      private Long firstTeamId;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "first_team_id")
      @JsonBackReference
      private Team firstTeam;
  
      public BaseEvent(){}
  
      public BaseEvent(Date startTime) {
          this.startTime = startTime;
      }
  
      public Date getStartTime() {
          return startTime;
      }
  
      public void setStartTime(Date startTime) {
          this.startTime = startTime;
      }
  
      public Long getFirstTeamId() {
          return firstTeamId;
      }
  
      public void setFirstTeamId(Long firstTeamId) {
          this.firstTeamId = firstTeamId;
      }
  
      public Team getFirstTeam() {
          return firstTeam;
      }
  
      public void setFirstTeam(Team firstTeam) {
          this.firstTeam = firstTeam;
      }
  
      @Override
      public Long getId() {
          return id;
      }
  
      @Override
      public void setId(Long id) {
          this.id = id;
      }
  }
  </code></pre>
  
  <p>I have BaseEventDAO and BaseEventController:</p>
  
  <pre><code>// DATA ACCESS OBJECT
  @Service
  public class BaseEventDAO {
  
      @Autowired
      private BaseEventRepository baseEventRepository;
  
      /* to save an baseEvent in DB */
  
      public BaseEvent save(BaseEvent baseEvent) {
          return this.baseEventRepository.save(baseEvent);
      }
  
      /* search all baseEvents */
  
      public List&lt;BaseEvent&gt; findAll() {
          return this.baseEventRepository.findAll();
      }
  
      /* get an baseEvent */
  
      public BaseEvent findOne(Long baseEventId) {
          return this.baseEventRepository.findOne(baseEventId);
      }
  
      /* delete an baseEvent */
  
      public void delete(BaseEvent baseEvent) {
          this.baseEventRepository.delete(baseEvent);
      }
  }
  </code></pre>
  
  <p>BaseEventController:</p>
  
  <pre><code>@RestController
  @RequestMapping("/sportapp")
  public class BaseEventController {
  
      @Autowired
      BaseEventDAO baseEventDAO;
  
      /* to save an baseEvent */
      @PostMapping("/baseEvent")
      public BaseEvent createBaseEvent(@Valid @RequestBody BaseEvent baseEvent) {
          return this.baseEventDAO.save(baseEvent);
      }
  
      /* get all baseEvents */
      @GetMapping("/baseEvents")
      public List&lt;BaseEvent&gt; getAllBaseEvents() {
          return this.baseEventDAO.findAll();
      }
  
      /* get an baseEvent by ID */
      @GetMapping("/baseEvents/{id}")
      public ResponseEntity&lt;BaseEvent&gt; getBaseEventById(@PathVariable(value = "id") Long baseEventId) {
          BaseEvent baseEvent = this.baseEventDAO.findOne(baseEventId);
          if (baseEvent == null) {
              return ResponseEntity.notFound().build();
          }
          return ResponseEntity.ok().body(baseEvent);
      }
  
      /* update an baseEvent */
      // TO DO SET THE NEW PROPERTIES OBJECT
      @PutMapping("/baseEvents/{id}")
      public ResponseEntity&lt;BaseEvent&gt; updateBaseEvent(@PathVariable(value = "id") Long baseEventId,
                                                       @Valid @RequestBody BaseEvent baseEventDetails) {
          BaseEvent baseEvent = this.baseEventDAO.findOne(baseEventId);
          if (baseEvent == null) {
              return ResponseEntity.notFound().build();
          }
  
          baseEvent.setStartTime(baseEventDetails.getStartTime());
  
          BaseEvent updateBaseEvent = this.baseEventDAO.save(baseEvent);
          return ResponseEntity.ok().body(updateBaseEvent);
  
      }
  
      /* delete an baseEvent */
      @DeleteMapping("/baseEvents/{id}")
      public ResponseEntity&lt;BaseEvent&gt; deleteBaseEvent(@PathVariable(value = "id") Long baseEventId) {
  
          BaseEvent baseEvent = this.baseEventDAO.findOne(baseEventId);
          if (baseEvent == null) {
              return ResponseEntity.notFound().build();
          }
          this.baseEventDAO.delete(baseEvent);
  
          return ResponseEntity.ok().build();
      }
  
  }
  </code></pre>
  
  <p>When i do PostMapping in Postman (/baseEvent):</p>
  
  <pre><code>{
      "startTime" : "01/01/2019 22:22:22",
      "firstTeamId" : 1
  }
  </code></pre>
  
  <p>Object BaseEvent is being saved to the DB (response):</p>
  
  <pre><code>{
      "id": 2,
      "startTime": "01/01/2019 22:22:22",
      "firstTeamId": 1,
      "new": false
  }
  </code></pre>
  
  <p>But when i do GetMapping to get this object (/baseEvents/2),This is the response:</p>
  
  <pre><code>{
      "id": 2,
      "startTime": "01/01/2019 22:22:22",
      "firstTeamId": null,
      "new": false
  }
  </code></pre>
  
  <p>I think what i need is:</p>
  
  <pre><code>{
  "id": 2,
  "startTime": "01/01/2019 22:22:22",
  "firstTeamId": 1,
  "firstTeam" : {
      "id" : 1,
      "nameOfTeam": "Barca",
      "leagueId": null,
      "players": [],
      "new": false
  },
  "new": false
  }
  </code></pre>
  
  <p>I have two problems I can't solve. First one is ("firstTeamId": null) should say 1 not "null". Second one I'm not getting the mapped Team object (firstTeam) so the front-end can use it.</p>
  
  <p>First time asking a question, please excuse me and tell me if you need more details.</p>