У меня есть 5 сущностей: поле, игра, ход, игрок и пользователь. В игре два игрока, и один игрок связан только с одной игрой; пользователь связан со многими игроками, игрок может сделать много ходов, в то время как каждый ход может иметь только одного игрока; В игре два игрока, много ходов и много полей.
Hibernate создает таблицы и ограничения для 4 из них без проблем. Однако, когда он пытается создать таблицу для объекта Move, он генерирует исключение:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table moves (id int4 not null, column int4 not null, row int4 not null, game_id int4, player_id int4, primary key (id))" via JDBC Statement
, а затем несколько раз
org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
WARN: GenerationTarget encountered exception accepting command : Error executing DDL "alter table moves add constraint FKjv33kkwwhe6121266nmuk7y1d foreign key (game_id) references games" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table moves add constraint FKjv33kkwwhe6121266nmuk7y1d foreign key (game_id) references games" via JDBC Statement
.
Я не знаю не понимаю, что не так с сущностью Move. Вот код, который у меня есть:
Поле :
package noughtsandcrosses.entity;
import javax.persistence.*;
@Entity
@Table(name = "fields")
public class Field {
@Id
@GeneratedValue
private int id;
private int[][] field;
@ManyToOne
@JoinColumn(name = "game_id")
private Game game;
public void update(Move move) {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int[][] getField() {
return field;
}
public void setField(int[][] field) {
this.field = field;
}
public Game getGame() {
return game;
}
public void setGame(Game game) {
this.game = game;
}
}
Игра :
package noughtsandcrosses.entity;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "games")
public class Game {
@Id @GeneratedValue
private int id;
@OneToOne
@JoinColumn(name = "crosses_player_id")
private Player crossesPlayer;
@OneToOne
@JoinColumn(name = "noughts_player_id")
private Player noughtsPlayer;
@OneToMany(mappedBy = "game")
private List<Move> moves;
@OneToMany(mappedBy = "game")
private List<Field> fields;
public Game(Player crossesPlayer, Player noughtsPlayer) {
this.crossesPlayer = crossesPlayer;
this.noughtsPlayer = noughtsPlayer;
}
public Player getCrossesPlayer() {
return crossesPlayer;
}
public void setCrossesPlayer(Player crossesPlayer) {
this.crossesPlayer = crossesPlayer;
}
public Player getNoughtsPlayer() {
return noughtsPlayer;
}
public void setNoughtsPlayer(Player noughtsPlayer) {
this.noughtsPlayer = noughtsPlayer;
}
public List<Move> getMoves() {
return moves;
}
public void setMoves(List<Move> moves) {
this.moves = moves;
}
public List<Field> getFields() {
return fields;
}
public void setFields(List<Field> fields) {
this.fields = fields;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Move :
package noughtsandcrosses.entity;
import javax.persistence.*;
@Entity
@Table(name = "moves")
public class Move {
@Id
@GeneratedValue
private int id;
@ManyToOne
@JoinColumn(name = "player_id")
private Player player;
@ManyToOne
@JoinColumn(name = "game_id")
private Game game;
private int row;
private int column;
public Move() {}
public Move(Player player, int row, int column) {
this.player = player;
this.row = row;
this.column = column;
}
public Player getPlayer() {
return player;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setPlayer(Player player) {
this.player = player;
}
public void setRow(int row) {
this.row = row;
}
public void setColumn(int column) {
this.column = column;
}
public Game getGame() {
return game;
}
public void setGame(Game game) {
this.game = game;
}
}
Player :
package noughtsandcrosses.entity;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name = "players")
public class Player {
@Id @GeneratedValue
private int id;
private enum Alignment {
NOUGHTS, CROSSES
};
private Alignment alignment;
@ManyToOne
private User user;
@OneToMany(mappedBy = "player")
private List<Move> moves;
public Player() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Alignment getAlignment() {
return alignment;
}
public void setAlignment(Alignment alignment) {
this.alignment = alignment;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<Move> getMoves() {
return moves;
}
public void setMoves(List<Move> moves) {
this.moves = moves;
}
}
Пользователь :
package noughtsandcrosses.entity;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name = "users")
@Inheritance(strategy = InheritanceType.JOINED)
public class User {
@Id
@GeneratedValue
private int id;
private String username;
private String password;
@OneToMany(mappedBy="user")
private List<Player> players;
public User() {}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<Player> getPlayers() {
return players;
}
public void setPlayers(List<Player> players) {
this.players = players;
}
}