Reputation: 2702
My problem is when i use this code to remove a Ship
entity i get this message
08-Mar-2017 13:01:36.504 INFO [http-nio-8080-exec-1] org.hibernate.event.internal.DefaultDeleteEventListener.deleteTransientEntity HHH000114: Handling transient entity in delete processing
However after commit, my entity still exists in the database
I use
Spring 4.2.1.RELEASE
Hibernate 4.3.5.Final
Mysql 5.1.30
Ship :
@JsonIgnoreProperties({"player", "game", "version"})
@Entity
@Table( name = "ship_tbl",
uniqueConstraints = { @UniqueConstraint( columnNames = { "ship_name" , "game_id"}, name = "UK_ship_player") } )
public class Ship extends AbstractShip {
private Player player;
private Game game;
private Long version;
@Version
@Column(name = "ship_version")
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
@Override
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name = "ship_id")
public Long getId() {
return super.getId();
}
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Player.class)
@JoinColumn(name="player_id", nullable = false)
public Player getPlayer() {
return player;
}
@Override
@OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, targetEntity = Cargo.class)
@JoinColumn(name="cargo_id", nullable = false)
public AbstractCargo getCargo() {
return super.getCargo();
}
@Override
@OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, targetEntity = ShipWay.class)
@JoinColumn(name="shipway_id", nullable = false)
public AbstractShipWay getShipWay() {
return super.getShipWay();
}
@Override
@ManyToOne(fetch = FetchType.EAGER, targetEntity = ShipKind.class)
@JoinColumn(name="shipkind_id", nullable = false)
public AbstractShipKind getShipKind() {
return super.getShipKind();
}
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Game.class)
@JoinColumn(name = "game_id", nullable = false)
public Game getGame() {
return game;
}
public void setGame(Game game) {
this.game = game;
}
@Override
@Column(name = "ship_name", nullable = false)
public String getName() {
return super.getName();
}
public void setPlayer(Player player) {
this.player = player;
}
public Ship(Long id, String name, AbstractCargo cargo, AbstractShipKind shipKind, AbstractShipWay shipWay, Player player, Game game) {
super(id, name, cargo, shipKind, shipWay);
this.player = player;
this.game = game;
}
public Ship(String name, AbstractCargo cargo, AbstractShipKind shipKind,AbstractShipWay shipWay) {
this(null, name, cargo, shipKind, shipWay,null,null);
}
public Ship() {
this(null,null,null,null);
}
}
Dao
This one remove the ship :
private void removeShip(Session session, Ship ship) {
Ship s = new Ship();
s.setId(ship.getId());
session.delete(s);
}
Remove the ship and add to the player is gold for the ship :
private void sellShip(Session session, Player player, Ship ship) throws ShipNotExistsException {
removeShip(session, ship);
updatePlayerGold(session, player, ship.getShipKind().sellShipAmount());
}
Do the transaction plus return int error :
@Override
public int sellShip(Player player, Ship ship) {
int error;
Session session = null;
Transaction transaction = null;
try{
session = openSession();
transaction = session.beginTransaction();
transaction.setTimeout(5);
sellShip(session, player, ship);
transaction.commit();
error = Response.ERROR_NONE_ERRORS;
}catch(RuntimeException e) {
if(e instanceof ShipNotExistsException) {
error = Response.ERROR_SHIP_NOT_EXISTS_OR_MATCH;
} else {
e.printStackTrace();
//TODO add else if stament and else insetent this
//already in transtaction
error = Response.ERROR_ALREADY_IN_TRANSACTION;
}
//if(!(e instanceof LockAcquisitionException)) {
try {
transaction.rollback();
} catch (RuntimeException re) {
re.printStackTrace();
}
//}
//throw e;
}finally{
if(session!=null){
session.close();
}
}
return error;
}
I am on this problem 3 hours and i will grateful for any help or explanation !
Thank you for reding.
as @Maciej Kowalski said in hes answer. i change the new Ship(), by merge.
more then, i will forget to tell you about the Player
entity as show here :
public class Player extends AbstractPlayer {
...
@Override
@OneToMany(fetch = FetchType.EAGER,cascade = {CascadeType.ALL}, targetEntity = Ship.class, mappedBy = "player")
public Set<AbstractShip> getShips() {
return super.getShips();
}
...
}
well the one-to-many
is the problem when exsits 'fetch = FetchType.EAGER'
solve one from this two option :
managedShip.getPlayer().getShips().remove(managedShip);
equal to parentEntity.collection.remove(childEntity)
as i doUpvotes: 3
Views: 7768
Reputation: 802
I think you need to change order of statement in your method as below:
private void sellShip(Session session, Player player, Ship ship) throws ShipNotExistsException {
updatePlayerGold(session, player, ship.getShipKind().sellShipAmount());
removeShip(session, ship);
}
Upvotes: 0
Reputation: 26542
In your delete method you should perform a merge before performing the delete operation. Im not sure why you create a new instance of Ship class, i think that can be omitted:
private void removeShip(Session session, Ship ship) {
Ship managedShip = session.merge(ship);
session.delete(managedShip);
}
Before you remove an entity you have to make sure it is managed by the persistence provider.
Upvotes: 3