gpt4 book ai didi

java - 使用 JPA 的 Java Spring Boot 中的 Column X Binary (255) 值太长

转载 作者:行者123 更新时间:2023-12-02 02:53:54 25 4
gpt4 key购买 nike

我正在尝试创建一个具有多个站点的游戏,例如金字塔或寺庙。我对所有网站都遇到相同的错误,因此我仅使用 Temple 作为示例。我想做的是通过创建新站点并将其分配给游戏来初始化游戏板,反之亦然。在站点类中设置游戏工作正常,但在父“Game.java”中设置站点会引发以下错误:

2017-04-13 17:23:10.183 WARN 5764 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 22001, SQLState: 22001 2017-04-13 17:23:10.183 ERROR 5764 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : Value too long for column "TEMPLE BINARY(255)": "X'aced00057372002d63682e757a682e6966692e7365616c2e736f707261667331372e656e746974792e73697465732e54656d706c65bfa968665c9a87790200... (2722)"; SQL statement: update game set burial_chamber=?, current_player=?, market=?, name=?, obelisk=?, ownerid=?, pyramid=?, shipyard=?, status=?, temple=? where game_id=? [22001-191] 2017-04-13 17:23:10.185 INFO 5764 --- [ main] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements

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

BoardService.java 类中的此处调用 (game.setTemple(newTemple)) 会引发错误:

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

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

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个字符串是什么以及它是在哪里生成的。以及为什么作业在一个方向有效,但在另一个方向却无效......希望大家能指出错误来源。

谢谢阿里克

最佳答案

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

JPA 提供程序将通过查找 @ID 注释(在 Game 的情况下位于方法上)来确定您使用的策略。

@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/

关于java - 使用 JPA 的 Java Spring Boot 中的 Column X Binary (255) 值太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43396634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com