开发者

Hibernate entity throws InvocationTargetException on persist

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:

@Roo开发者_Python百科JavaBean
@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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜