我有两个实体-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;
}
}
基本实体:
@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”。第二,我没有得到Map的团队对象(firstteam),因此前端可以使用它。
第一次问问题,请原谅,如果你需要更多的细节,请告诉我。
2条答案
按热度按时间ghg1uchk1#
我在更改baseeventcontroller post方法时修复了它:
从firstteamid中删除@transient并更改了join列:
dtcbnfnu2#
您需要将fetchtype.lazy更改为fetchtype.eager,以便在hibernate执行查询以获取团队时具有更强的持久性。。