Hi, I got a problem with resaving/updating a loaded entity. The problem is kind of strange because when I create the object in my spring controller and saves it with the same method, it works, and I can also load it. However when Ive done my modifications, or even without modifications and I try to save it back to the db, it is removed instead.
Its nothing wrong with the saveOrUpdate method im using, as I can do the same thing with other objects without any problem, even other objects in the same entity.
I dont understand what the problem is and I would really appreciate help.
I got a Player entity with a Set<City>, the city has a Map<Building>. Its the resaving of the Building entity that is the problem.
My Player class:
Code:
@Entity
@Table(name = "player")
public class Player implements Serializable {
@Id
private String username = null;
....
@ElementCollection
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@JoinTable(name="player_city")
private Set<City> city = null;
....
}
My City class:
Code:
@Entity
@Table(name = "city")
@IdClass(CityId.class)
public class City implements Serializable {
@Id
private int x = 0;
@Id
private int y = 0;
// @JoinTable(name="city_building",
// joinColumns = {@JoinColumn( name="city_x"), @JoinColumn( name="city_y")},
// inverseJoinColumns = @JoinColumn( name="building_id")
// )
@MapKey(name="position")
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@JoinColumns(value= {@JoinColumn(name="x"), @JoinColumn(name="y")})
private Map<Integer, Building> building = null;
...
constructors
...
public boolean equals(Object o) {
if(!(o instanceof City)) {
return false;
}
City c = (City)o;
if(x == c.x && y == c.y) {
return true;
}
return false;
}
public int hashCode() {
int result = 17;
result = 37*result + x;
result = 37*result + y;
return result;
}
}
class CityId implements Serializable {
private int x = 0;
private int y = 0;
...
}
And my Building class:
Code:
@Entity
@Table(name = "building")
public class Building implements Serializable {
@Id
@GeneratedValue
private long id = 0;
private int level = 0;
private String buildingType = null;
private int position = 0;
private int x = 0;
private int y = 0;
...
constructors
...
public boolean equals(Object o) {
if(!(o instanceof Building)) {
return false;
}
Building b = (Building)o;
if(id == b.id) {
return true;
}
return false;
}
public int hashCode() {
int result = 17;
result = 37*result + (int)id;
return result;
}
In my city entity I first tried doing this with a relationship table city_building, but for some reason the row linking a city to a building was removed each time I tried to resave it, so I instead tried with having the foreign city keys in the building entity. But now the building row in the building table is removed each time Im trying to save..
Im using Cascade.ALL, so when I want to save this I use my playerManager.saveOrUpdate();
What makes it really confusing for me is that I also got a Map with other objects in the player entity, this Map has a Map itself containing other entities, and those are being saved and updated without any problems what so ever.
This is how I do the saving of new buildings in my Spring controller:
Pesudo code:
Code:
Player player = playerManager.getPlayer("username");
City city = player.getTheCitySomehow();
Building building = city.getBuilding().get(position); //If this is not null, then it will be removed when saving below..
if(building == null) {
building = new Building(the params goes here); //If this happens, it will save it as it should.
city.getBuilding().put(position, building);
} else {
building.setLevel(building.getLevel()+1);
}
playerManager.saveOrUpdate(player); //Now this will save the Building to the db, if building was == null, but if you got the building from the db through city.getBuilding().get(position); then it will instead remove it for some reason.. Why?
Does anyone know what this problem can be? Any hints would really be appreciated. If I have missed anything important please tell me and Ill add it.