我正在尝试创建一个具有多个站点的游戏,例如金字塔或圣殿。我在所有站点上都遇到相同的错误,因此我仅以a为例-Temple。我正在尝试通过创建新站点并将其分配给游戏来初始化游戏板,反之亦然。在站点类中设置游戏可以正常工作,但是在父级“ Game.java”中设置站点会引发以下错误:
2017-04-13 17:23:10.183 WARN 5764 --- [主]
o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:22001,SQLState:
22001 2017-04-13 17:23:10.183错误5764 --- [main]
o.h.engine.jdbc.spi.SqlExceptionHelper:列的值太长
“ TEMPLE BINARY(255)”:
“ X'aced00057372002d63682e757a682e6966692e7365616c2e736f707261667331372e656e746974792e73697465732e54656d706c65bfa968665c9a87790200 ...
(2722)“; SQL语句:更新游戏设置burial_chamber = ?,
current_player = ?、 market = ?、 name = ?、方尖碑= ?、 ownerid = ?、金字塔= ?、
shipyard = ?,状态= ?, temple =?其中game_id =? [22001-191] 2017-04-13
17:23:10.185 INFO 5764 --- [main]
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;
}
}
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);
这里的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字符串是什么以及在哪里生成。以及为什么分配在一个方向上起作用而在另一方向上却不起作用...
希望你们能指出错误源。
谢谢
阿里克
最佳答案
问题是您在字段和方法上混合了注释。
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/