These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Problem resaving/updating a loaded entity
PostPosted: Mon Apr 25, 2011 9:09 am 
Newbie

Joined: Sun Apr 24, 2011 12:28 pm
Posts: 6
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.


Top
 Profile  
 
 Post subject: Re: Problem resaving/updating a loaded entity
PostPosted: Mon Apr 25, 2011 12:04 pm 
Newbie

Joined: Sun Apr 24, 2011 12:28 pm
Posts: 6
I seem to have misstaken myself.
Now that I have the city x and y in the building table it will instead set x and y to null, even though IVe given them values. And when I set the fields to not null, Im getting a constraint violation excation saying that x cannot be null.


Top
 Profile  
 
 Post subject: Re: Problem resaving/updating a loaded entity
PostPosted: Mon Apr 25, 2011 4:34 pm 
Newbie

Joined: Sun Apr 24, 2011 12:28 pm
Posts: 6
As I mentioned in the first post I had a already simular functioning way of doing this with other entities as well, that is in my player object.. So I ended up implementing the same solution for this problem after 2 almost 3 days of trying to solve this crap problem. Its still effing anoying that I cant do it as I wanted to.. So if someone just happens to have a solution to the above mentioned problem, then I would be really happy.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.