如何获取以JSON格式显示的多对一对象

时间:2018-11-11 19:29:28

标签: java mysql hibernate rest spring-boot

我有两个实体-BaseEvent(体育赛事)和Team:

团队:

@Entity
@Table(name = "teams")
@EntityListeners(AuditingEntityListener.class)
public class Team extends AbstractPersistable<Long> {
    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<Player> players = new HashSet<>();



    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<Player> getPlayers() {
        return players;
    }

    public void setPlayers(Set<Player> players) {
        this.players = players;
    }

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }
}

BaseEntity:

  @Entity
  @Table(name = "base_events")
  @EntityListeners(AuditingEntityListener.class)
  public class BaseEvent extends AbstractPersistable<Long> {

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

我有BaseEventDAO和BaseEventController:

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

BaseEventController:

@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<BaseEvent> getAllBaseEvents() {
        return this.baseEventDAO.findAll();
    }

    /* get an baseEvent by ID */
    @GetMapping("/baseEvents/{id}")
    public ResponseEntity<BaseEvent> 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<BaseEvent> 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<BaseEvent> 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();
    }

}

当我在Postman(/ baseEvent)中进行PostMapping时:

{
    "startTime" : "01/01/2019 22:22:22",
    "firstTeamId" : 1
}

对象BaseEvent被保存到数据库(响应):

{
    "id": 2,
    "startTime": "01/01/2019 22:22:22",
    "firstTeamId": 1,
    "new": false
}

但是当我执行GetMapping获取此对象(/ baseEvents / 2)时,这是响应:

{
    "id": 2,
    "startTime": "01/01/2019 22:22:22",
    "firstTeamId": null,
    "new": false
}

我认为我需要的是

{
"id": 2,
"startTime": "01/01/2019 22:22:22",
"firstTeamId": 1,
"firstTeam" : {
    "id" : 1,
    "nameOfTeam": "Barca",
    "leagueId": null,
    "players": [],
    "new": false
},
"new": false
}

我有两个无法解决的问题。第一个是(“ firstTeamId”:null)应该说1而不是“ null”。第二个,我没有获取映射的Team对象(firstTeam),因此前端可以使用它。

第一次问一个问题,请原谅并告诉我是否需要更多详细信息。

2 个答案:

答案 0 :(得分:0)

您需要将FetchType.LAZY更改为FetchType.EAGER,以便在Hibernate执行查询以获取团队时更加持久化。

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "first_team_id")
@JsonBackReference
private Team firstTeam;

答案 1 :(得分:0)

我在更改BaseEventController Post方法时将其修复:

@Autowired
BaseEventDAO baseEventDAO;


@Autowired
TeamDAO teamDAO;

/* to save an baseEvent */
@PostMapping("/baseEvent")
public BaseEvent createBaseEvent(@Valid @RequestBody BaseEvent baseEvent) {
    Team team = this.teamDAO.findOne(baseEvent.getFirstTeamId());
    BaseEvent event = new BaseEvent();
    event.setStartTime(baseEvent.getStartTime());
    event.setFirstTeam(team);
    event.setFirstTeamId(baseEvent.getFirstTeamId());
    return this.baseEventDAO.save(event);
}

并从firstTeamId中删除了@Transient并更改了连接列:

@Column(name="first_team_id")
 private Long firstTeamId;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "team_id", referencedColumnName = "id")
private Team firstTeam;