Hibernate 实体在持久化时抛出 InvocableTargetException
我试图弄清楚为什么在保留修改后的玩家对象时会收到 InvocableTargetException 。 该项目是一个 Spring Roo 项目,使用 Hibernate 作为 ORM(前端使用 GWT,但这与这里无关,因为错误发生在后端)。
单步执行代码,错误发生在通过 RPC 调用调用的player.persist() 处:
@Override
public LeagueDto setPlayerLeague(long playerId, String session, long leagueId) {
Player player = Player.findPlayer(playerId);
League league = League.findLeague(leagueId);
player.setLeague(league);
player.persist(); // fails here
// do some more stuff here before returning the DTO
return leagueDto;
}
进入player.persist() 进入模型:
@RooJavaBean
@RooToString
@RooEntity(finders = { "findPlayersByUsername" })
public class Player {
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = "S-")
private Date created;
@NotNull
@Column(unique = true)
@Size(min = 3, max = 32)
private String Username;
....
@Size(max = 64)
private String FirstName;
@Size(max = 64)
private String LastName;
@ManyToOne
private Country country;
@ManyToOne
private League league;
...
}
进一步进入模型进入调用 persist 的 AspectJ 代码:
privileged aspect Player_Roo_Entity {
declare @type: Player: @Entity;
@PersistenceContext
transient EntityManager Player.entityManager;
....
@Transactional
public void Player.persist() {
if (this.entityManager == null) this.entityManager = entityManager();
this.entityManager.persist(this);
}
....
}
然后它设法跳过 this.entityManager.persist(this) ,当函数退出时,它在 RPC.java 中失败
public static String invokeAndEncodeResponse(Object target,
Method serviceMethod, Object[] args,
SerializationPolicy serializationPolicy, int flags)
throws SerializationException {
....
String responsePayload;
try {
Object result = serviceMethod.invoke(target, args);
responsePayload = encodeResponseForSuccess(serviceMethod, result,
serializationPolicy, flags);
} catch (IllegalAccessException e) {
....
} catch (IllegalArgumentException e) {
....
} catch (InvocationTargetException e) {
// Try to encode the caught exception
//
Throwable cause = e.getCause();
responsePayload = encodeResponseForFailure(serviceMethod, cause, serializationPolicy, flags);
}
知道为什么会失败吗?我没有做任何复杂的事情,只是进行基本的更新。
I'm trying to figure out why I'm getting an InvocationTargetException when persisting a modified player object.
The project is a Spring Roo project with Hibernate as ORM (and GWT for the frontend, but that's not relevant here as the error happens in the backend).
Stepping through the code, the error occurs at player.persist() which is invoked via a RPC call:
@Override
public LeagueDto setPlayerLeague(long playerId, String session, long leagueId) {
Player player = Player.findPlayer(playerId);
League league = League.findLeague(leagueId);
player.setLeague(league);
player.persist(); // fails here
// do some more stuff here before returning the DTO
return leagueDto;
}
Stepping into player.persist() goes to the model:
@RooJavaBean
@RooToString
@RooEntity(finders = { "findPlayersByUsername" })
public class Player {
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = "S-")
private Date created;
@NotNull
@Column(unique = true)
@Size(min = 3, max = 32)
private String Username;
....
@Size(max = 64)
private String FirstName;
@Size(max = 64)
private String LastName;
@ManyToOne
private Country country;
@ManyToOne
private League league;
...
}
Stepping further into the model goes to the AspectJ code where persist is being called:
privileged aspect Player_Roo_Entity {
declare @type: Player: @Entity;
@PersistenceContext
transient EntityManager Player.entityManager;
....
@Transactional
public void Player.persist() {
if (this.entityManager == null) this.entityManager = entityManager();
this.entityManager.persist(this);
}
....
}
It then manages to step over this.entityManager.persist(this) and when the function exits, it fails in RPC.java
public static String invokeAndEncodeResponse(Object target,
Method serviceMethod, Object[] args,
SerializationPolicy serializationPolicy, int flags)
throws SerializationException {
....
String responsePayload;
try {
Object result = serviceMethod.invoke(target, args);
responsePayload = encodeResponseForSuccess(serviceMethod, result,
serializationPolicy, flags);
} catch (IllegalAccessException e) {
....
} catch (IllegalArgumentException e) {
....
} catch (InvocationTargetException e) {
// Try to encode the caught exception
//
Throwable cause = e.getCause();
responsePayload = encodeResponseForFailure(serviceMethod, cause, serializationPolicy, flags);
}
Any idea why this is failing? I'm not doing anything complex, just a basic update.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于破解了。
数据库中的版本字段为空,将其更改为 0 后可以完美运行。
似乎 Hibernate 正在尝试增加 null 字段。
我想我将来在创建灯具数据时会更加小心,不知道 Hibernate 对数据库中的空值如此敏感。
I finally cracked it.
The version field in the database was null, after changing it to 0 it worked flawlessly.
Seems Hibernate was trying to increment the null field.
Guess I'll be taking a lot more care when creating fixtures data in the future, didn't know Hibernate was this sensitive to nulls in the database.