이 하루 동안 사용되었으므로 문제를 인식 할 수 없습니다. 나는 완전한 해결책을 필요로하지 않는다, 다만 저에게 지침서를주는 것은 문제이다. 그 문제에 관한 정보를 어디서 찾을 수 있는지. 나는 데이터에서 데이터를 일부 데이터를 가지고 할 수있는 부트 스트랩 클래스를 생성 한데이터베이스가 두 개의 특정 엔티티에 대한 관계를 표시하지 않습니다. - 스프링 부트 응용 프로그램
@Entity
@Data
@EqualsAndHashCode(exclude = {"teams","organizers"})
public class Event {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(nullable = false)
private String eventName;
@ManyToMany
@JoinTable(name = "event_organizer",
joinColumns = @JoinColumn(name = "event_id"),
inverseJoinColumns = @JoinColumn(name = "organizer_id"))
private Set<Organizer> organizers= new HashSet<>();
@ManyToMany
@JoinTable(name = "event_team",
joinColumns = @JoinColumn(name = "event_id"),
inverseJoinColumns = @JoinColumn(name = "team_id"))
private Set<Team> teams = new HashSet<>();
}
@Entity
@Data
@EqualsAndHashCode(exclude = {"events"})
public class Organizer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(nullable = false)
private String organizerName;
private String organizerLastName;
private String organizerEmailAddress;
private String organizerTelephoneNumber;
@ManyToMany(mappedBy = "organizers")
private Set<Event> events= new HashSet<>();
}
@Entity
@Data
@EqualsAndHashCode(exclude = {"events", "players"})
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(nullable = false)
private String teamName;
@ManyToMany
@JoinTable(name = "team_player",
joinColumns = @JoinColumn(name = "team_id"),
inverseJoinColumns = @JoinColumn(name = "player_id"))
private Set<Player> players = new HashSet<>();
@ManyToMany(mappedBy = "teams")
private Set<Event> events= new HashSet<>();
}
@Entity
@Data
@EqualsAndHashCode(exclude = {"teams"})
public class Player {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(nullable = false)
private String playerName;
private String playerLastName;
private int playerAge;
private String playerNickname;
private String playerEmailAddress;
private String playerTelephoneNumber;
@ManyToMany(mappedBy = "players")
private Set<Team> teams = new HashSet<>();
@Enumerated(value = EnumType.STRING)
private FavouritePosition favouritePosition;
}
:
는 좀 기관, 그들 중 일부는 아래 찾을 수 있습니다 (다만이 작업에 필요한 모든 필드를) 만들었습니다. SQL 파일 :@Component
public class EventBootstrap implements ApplicationListener<ContextRefreshedEvent>{
//There are some Repositories fields and constructor
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
eventRepository.saveAll(getEvent());
}
private List<Event> getEvent() {
List<Event> events = new ArrayList<>();
//Organizers
Optional<Organizer> ostrouchKolobrzeskaOrganizerOptional = organizerRepository.findByOrOrganizerLastName("Ostrouch");
if (!ostrouchKolobrzeskaOrganizerOptional.isPresent()){
throw new RuntimeException("Organizer not found !");
}
Organizer ostrouchKolobrzeskaOrganizer = ostrouchKolobrzeskaOrganizerOptional.get();
Optional<Organizer> kaimWestFootballPitchOrganizerOptional = organizerRepository.findByOrOrganizerLastName("Kaim");
if (!kaimWestFootballPitchOrganizerOptional.isPresent()){
throw new RuntimeException("Organizer not found!");
}
Organizer kaimWestFootballPitchOrganizer = kaimWestFootballPitchOrganizerOptional.get();
//Set 1 organizers
Set<Organizer> organizersSetOne = new HashSet<>();
organizersSetOne.add(kaimWestFootballPitchOrganizer);
organizersSetOne.add(ostrouchKolobrzeskaOrganizer);
//Set 2 organizers
Set<Organizer> organizerSetTwo = new HashSet<>();
organizerSetTwo.add(ostrouchKolobrzeskaOrganizer);
//Team
Optional<Team> jeppesenFootballTeamOptional = teamRepository.findByTeamName("Jeppesen Football Team");
if (!jeppesenFootballTeamOptional.isPresent()){
throw new RuntimeException("Expected football team not found");
}
Team jeppesenFootballTeam = jeppesenFootballTeamOptional.get();
Optional<Team> lufthansaFootballTeamOptional = teamRepository.findByTeamName("Lufthansa Football Team");
if (!lufthansaFootballTeamOptional.isPresent()){
throw new RuntimeException("Expected football team not found");
}
Team lufthansaFootballTeam = lufthansaFootballTeamOptional.get();
//Set 1 team
Set<Team> teamsSetOne = new HashSet<>();
teamsSetOne.add(jeppesenFootballTeam);
teamsSetOne.add(lufthansaFootballTeam);
//Set 2 team
Set<Team> teamsSetTwo = new HashSet<>();
teamsSetTwo.add(lufthansaFootballTeam);
//getPlayer
Optional<Player> przemekPlayerOptional = playerRepository.findByPlayerName("Przemyslaw");
if(!przemekPlayerOptional.isPresent()){
throw new RuntimeException("Expected player not found!");
}
Optional<Player> jakubPlayerOptional = playerRepository.findByPlayerName("Jakub");
if(!jakubPlayerOptional.isPresent()){
throw new RuntimeException("Expected player not found!");
}
Player przemekPlayer = przemekPlayerOptional.get();
przemekPlayer.setFavouritePosition(FavouritePosition.MIDFIELDER);
Player jakubPlayer = jakubPlayerOptional.get();
jakubPlayer.setFavouritePosition(FavouritePosition.ATTACKER);
Set<Player> playersSetOne = new HashSet<>();
playersSetOne.add(przemekPlayer);
playersSetOne.add(jakubPlayer);
Set<Player> playersSetTwo= new HashSet<>();
playersSetTwo.add(przemekPlayer);
//setPlayer for the team
jeppesenFootballTeam.setPlayers(playersSetOne);
lufthansaFootballTeam.setPlayers(playersSetTwo);
Event kolobrzeskaEvent = new Event();
kolobrzeskaEvent.setOrganizers(organizersSetOne);
kolobrzeskaEvent.setEventName("Kołobrzeska Event");
kolobrzeskaEvent.setTeams(teamsSetOne);
Event westEvent = new Event();
westEvent.setOrganizers(organizerSetTwo);
westEvent.setEventName("West Event");
westEvent.setTeams(teamsSetTwo);
events.add(kolobrzeskaEvent);
events.add(westEvent);
return events;
}
}
이것은 thymeleaf 및 hibernate가있는 스프링 부트 프로젝트입니다. 내가 응용 프로그램을 실행하고 H2 데이터베이스 콘솔을 볼 때 EVENT_ORGANIZER, ORGANIZER, EVENT, PLAYER, TEAM이 올바른 표를 가지고 있지만 TEAM_PLAYER는 비어 있습니다. 관계가 표시되지 않습니다. 디버거를 사용해 보았지만 아무런 효과가 없었습니다. 문제가 어디에 있습니까? 나는 모든 엔티티 클래스와 관계를 연구했다. 부트 스트랩 클래스와 같았다.
추가 정보가 필요하면 의견을 보내주십시오. 내가 한 번 더 그것을 디버깅 , 나는 그 어떤 변수 옆에 내가 가지고 예외 발견 : 는 지원
편집 주셔서 대단히 감사합니다 나는 = FetchType를 가져 오기 설정 그 후
Method threw 'org.hibernate.LazyInitializationException' exception. Cannot evaluate java.util.Optional.toString()
합니다. 심한. 그러나 흥분은 여전히 jeppesenFootballTeamOptional 근처에서 발생합니다. 그것은 데이터베이스의 관계에 문제가 될 수 있습니까?
data.sql이 정확하지 않습니다. 나는 그것을 3 번 점검했다. – przemekost