Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Breaking JPA spec lifecycle (PrePersist, PreUpdate) rules - problem or not?

JPA 2.0 spec states that:

3.5 ....The following rules apply to lifecycle callbacks:
....
"In general, the lifecycle method of a portable application should not
invoke EntityManager
or Query operations, access other entity instances, or modify
relationships within the
same persistence context.[43] A lifecycle callback method may modify
the non-relationship
state of the entity on which it is invoked." [43] The semantics of
such operations may be standardized in a future release of this
specification.

In practise, I have broken this rule and it seems to work. I have
setup similar to

class Team {
  @Id
  @GeneratedValue....
  private Long id

  @ManyToOne
  @JoinColumn(name="TEAM_LEADER_ID")
  private Member teamLeader
}

class Member {
    @Id
    @GeneratedValue....
    private Long id

    @ManyToOne
    @JoinColumn(name = "TEAM_ID")
    private Team team;

    @Column
    private boolean leader;

    @PreUpdate
    @PrePersist
    public void updateTeamLeadership() {
        if (isLeader()) {
            getTeam().setTeamLeader(this);
        }
    }
}

Updating Team.teamLeader this way is a bit more convenient and
foolproof if done automatically using listeners like this.

Now, if I understand the spec wording right, I am doing what I should
not - modifying relationships within the same persistence context. In
practise, the code seems to work fine. I know end results are probably
"undefined" and cannot be trusted 100% but...

- is this really a problem with current EclipseLink implementation, or
would this be a usable approach?
- are there any foreseeable changes coming to EclipseLink that would
break this functionality?


Back to the top