我的问题是,当我使用此代码删除 Ship
实体时,我收到此消息
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
但是在提交后,我的实体仍然存在于数据库中
我用
Spring 4.2.1.RELEASE
hibernate 4.3.5.Final
Mysql 5.1.30
代码:
运送:
@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);
}
}
道
这个移除飞船:
private void removeShip(Session session, Ship ship) {
Ship s = new Ship();
s.setId(ship.getId());
session.delete(s);
}
移除飞船并为玩家添加飞船的金币:
private void sellShip(Session session, Player player, Ship ship) throws ShipNotExistsException {
removeShip(session, ship);
updatePlayerGold(session, player, ship.getShipKind().sellShipAmount());
}
做交易加上返回int错误:
@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;
}
我在这个问题上花了 3 个小时,如果有任何帮助或解释,我将不胜感激!
感谢您的转发。
解决方案:
正如@Maciej Kowalski 在他的回答中所说。我通过合并更改了新的 Ship()。
此外,我会忘记告诉您有关 Player
实体的信息,如下所示:
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();
}
...
}
one-to-many
是退出 'fetch = FetchType.EAGER' 时的问题
从这两个选项中解决一个:
- 将抓取更改为“懒惰”,因为我没有这样做。
- 在删除之前添加这个/li>
最佳答案
在您的删除方法中,您应该在执行删除操作之前执行合并。我不确定为什么要创建 Ship 类的新实例,我认为可以省略:
private void removeShip(Session session, Ship ship) {
Ship managedShip = session.merge(ship);
session.delete(managedShip);
}
在删除实体之前,您必须确保它由持久性提供程序管理。
关于java - 在删除处理中处理临时实体。实体不被删除[解决],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42669965/