Reputation: 15
I'm trying to create a Game that has several sites, such as a Pyramid or a Temple. I get the same error for all the sites, so I'll just use on a as an example - Temple. What I'm trying to do is to initialize the gameboard, by creating new sites and assigning them to the game, and vice versa. Setting the game in the site classes works fine, but setting the sites in the parent "Game.java" throws following error:
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;
}
}
This call here (game.setTemple(newTemple)), in the BoardService.java class throws the error:
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);
Here the Game.java class without getters and setters and other trivial methods:
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;
I don't understand what that 2722 character String is and where it is generated. And why the assignment works in one direction but not in the other... Hope you guys can point me to the error source.
Thanks Arik
Upvotes: 0
Views: 4903
Reputation: 23246
The issue is that you are mixing annotations on both fields and methods.
The JPA provider will determine which strategy you are using by looking for the @ID
annotation which in the case of Game is on the method.
@Id
@GeneratedValue
@Column (name = "game_id")
public Long getId (){
return id;
}
Essentially then the @OneToOne
annotation on Temple is ignored as it is on the Field:
@OneToOne (mappedBy = "game")
private Temple temple;
So Hibernate essentially tries then to persist Temple as a Binary value in the Game table as it is not aware of the relationship - it simply sees it as a simple field.
You can mix the annotations as detailed below but that is rarely required. Use one or the other in most cases:
http://howtodoinjava.com/jpa/field-vs-property-vs-mixed-access-modes-jpa-tutorial/
Upvotes: 2