简体   繁体   中英

Hibernate can't create a table: org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table PSQLException

I have 5 entities: Field, Game, Move, Player and User. A game has two players and one player is only ever associated with one game; a user is associated with many players, a player can make many moves, while each move can only have one player; a game has two players and many moves and many fields.

Hibernate creates tables and constraints for 4 of them with no issues. However when it tries to create a table for the Move entity it throws an exception:

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

and then later

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

several times.

I don't understand what's wrong witht the Move entity. This is the code I have:

Field :

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

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

User :

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

Hibernate should give you a real error message, not just tell you which command failed. Is it not doing that? Hibernate is very popular (but not with me personally), so I doubt it can be so broken that it can't return real error messages, so I think you just aren't reading them correctly. In any event, you can look in PostgreSQL's log file to find the real error message

If I take the statement from the message you show and run it in psql, I get:

create table moves (id int4 not null, column int4 not null, row int4 not null, game_id int4, player_id int4, primary key (id));
ERROR:  syntax error at or near "column"

The problem here is that column is a reserved word in PostgreSQL. You can't use it as the name of a column, unless you put it in double quotes. I don't know how to tell hibernate to put it in double quotes, but I'm sure there is a way. But it would be better just to name it something else like board_column . row is not a fully reserved word in PostgreSQL, so you can use it, but I would not do so. Rename it to something like board_row .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM