使用JPA的Java SpringBoot中的列X二进制(255)的值太长

时间:2017-04-13 15:47:24

标签: java sql jpa

我正在尝试创建一个拥有多个网站的游戏,例如金字塔或寺庙。我得到了所有网站的相同错误,所以我只是用一个例子 - Temple。我想要做的是初始化游戏板,创建新的网站并将它们分配给游戏,反之亦然。在站点类中设置游戏工作正常,但在父“Game.java”中设置站点会引发以下错误:

  

2017-04-13 17:23:10.183警告5764 --- [主要]   o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:22001,SQLState:   22001 2017-04-13 17:23:10.183 ERROR 5764 --- [主要]   o.h.engine.jdbc.spi.SqlExceptionHelper:列的值太长   “TEMPLE BINARY(255)”:   “X'aced00057372002d63682e757a682e6966692e7365616c2e736f707261667331372e656e746974792e73697465732e54656d706c65bfa968665c9a87790200 ...   (2722)“; SQL语句:更新游戏集burial_chamber =?,   current_player = ?, market = ?, name =?,obelisk = ?, ownerid =?,pyramid = ?,   造船厂=?,状态=?,寺庙=? game_id =? [22001-191] 2017-04-13   17:23:10.185 INFO 5764 --- [主要]   o.h.e.j.b.internal.AbstractBatchImpl:HHH000010:发布时   批处理它仍然包含JDBC语句

import javax.persistence.*;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Temple implements Serializable {

    @Column
    private boolean isDockEmpty = true;

    @Id
    @GeneratedValue
    @Column(name = "id", updatable = false, nullable = false)
    private long id;

    @ElementCollection
    private List<Color> stones = new ArrayList<Color>();

    public List<Color> getStones (){
        return stones;
    }

    @Column (name = "name")
    private String name = "Temple";

    @OneToOne
    @JoinColumn (name = "game_id")
    private Game game;

    @OneToOne
    @JoinColumn(name = "SHIP_ID")
    private Ship ship;

    public long getId(){
        return id;
    }

    public void fillDock (){isDockEmpty = false;}

    public void setId(long id) {
        this.id = id;
    }

    /*public void setStones(List<Stone> stones) {
        this.stones = stones;
    }*/

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Game getGame() {
        return game;
    }

    public void setGame(Game game) {
        this.game = game;
    }

    public Ship getShip() {
        return ship;
    }

    public void setShip(Ship ship) {
        this.ship = ship;
    }
}

此调用此处(game.setTemple(newTemple)),在BoardService.java类中抛出错误:

   private void createAndAssignSites(Game game) {

        BurialChamber newBurialChamber = new BurialChamber();
        Pyramid newPyramid = new Pyramid();
        Obelisk newObelisk = new Obelisk();
        Temple newTemple = new Temple();
        Market newMarket = new Market();

        newBurialChamber.setGame(game);
        newPyramid.setGame(game);
        newObelisk.setGame(game);
        newTemple.setGame(game);
        newMarket.setGame(game);

//        game.setBurialChamber(newBurialChamber);
//        game.setPyramid(newPyramid);
//        game.setObelisk(newObelisk);
        game.setTemple(newTemple);
//        game.setMarket(newMarket);

        gameRepository.save(game);

        burialChamberRepository.save(newBurialChamber);
        pyramidRepository.save(newPyramid);
        obeliskRepository.save(newObelisk);
        templeRepository.save(newTemple);
        marketRepository.save(newMarket);

这里的Game.java类没有getter和setter以及其他简单的方法:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;


import ch.uzh.ifi.seal.soprafs17.constant.GameStatus;
import ch.uzh.ifi.seal.soprafs17.entity.sites.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity (name = "game")
public class Game implements Serializable {

    private static final long serialVersionUID = 1L;

    private List<User> players = new ArrayList<>();

    private List<Move> moves = new ArrayList<>();

    public Game (){}

    public Game (String name, long ownerID, User player){
        this.name = name;
        this.ownerID = ownerID;
        this.status = GameStatus.PENDING;
        players.add(player);
    }

    private Long id;

    @Id
    @GeneratedValue
    @Column (name = "game_id")
    public Long getId (){
        return id;
    }

    @Column(nullable = false)
    private String name;

    @Column(nullable = false) 
    private Long ownerID;

    @Column 
    private GameStatus status;

    @Column 
    private Integer currentPlayer = 0;

    @OneToMany(mappedBy="game")
    public List<Move> getMoves(){
        return moves;
    }

    @JsonIgnore
    @LazyCollection(LazyCollectionOption.FALSE)
    @OneToMany (mappedBy="game",cascade = CascadeType.ALL)
    public List<User> getPlayers (){
        return players;
    }

    public void setPlayers (List<User> players){
        this.players = players;
    }

    @OneToOne
    private BurialChamber burialChamber;

    @OneToOne
    private Market market;

    @OneToOne
    private Obelisk obelisk;

    @OneToOne (mappedBy = "game")
    private Pyramid pyramid;

    @OneToOne
    private Shipyard shipyard;

    @OneToOne (mappedBy = "game")
    private Temple temple;

我不明白2722字符串是什么以及它在哪里生成。为什么作业在一个方向上工作而在另一个方向上却没有... 希望你们能指出我的错误来源。

由于 阿里克

1 个答案:

答案 0 :(得分:2)

问题在于您要在两个字段和方法上混合注释。

JPA提供商将通过查找@ID注释来确定您正在使用哪种策略,该注释在游戏的情况下是在该方法上。

@Id
@GeneratedValue
@Column (name = "game_id")
public Long getId (){
    return id;
}

基本上,Temple上的@OneToOne注释会被忽略,因为它位于Field:

@OneToOne (mappedBy = "game")
private Temple temple;

因此,Hibernate本质上会尝试将Temple作为二进制值保留在Game表中,因为它不知道这种关系 - 它只是将其视为一个简单的字段。

您可以混合注释,如下所述,但很少需要。在大多数情况下使用其中一种:

http://howtodoinjava.com/jpa/field-vs-property-vs-mixed-access-modes-jpa-tutorial/