Hibernate实体在persist上抛出InvocationTargetException

时间:2011-04-03 13:46:57

标签: hibernate spring-roo

我试图找出为什么在持久化修改的播放器对象时我得到一个InvocationTargetException。 该项目是一个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;

    ...

}

进一步进入模型转到AspectJ代码,其中调用了persist:

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);
}

知道为什么会失败吗?我没有做任何复杂的事情,只是一个基本的更新。

1 个答案:

答案 0 :(得分:0)

我终于破解了它。

数据库中的版本字段为null,将其更改为0后,它完美无缺。 似乎Hibernate试图增加空字段。

猜猜我在将来创建灯具数据时会更加小心,不知道Hibernate对数据库中的空值是否敏感。