8

I have faced with issue trying to select for update row using Spring data with Hibernate as JPA implementation and Postgresql.

Suppose we have entities:A,B,C.

public class A{
   @Id
   private Long id;

   @OneToMany(fetch = FetchType.EAGER)
   private Set<B> bSet;

   @OneToMany(fetch = FetchType.EAGER)
   private Set<C> cSet;
}

Suppose we want to select A with all related B and C entities for update i.e. with locking row related to A table.

@Query(SELECT a FROM A a 
       LEFT JOIN FETCH a.bSet
       LEFT JOIN FETCH a.cSet
       WHERE a.id=?)
@Lock(LockModeType.PESSIMISTIC_WRITE)
public A selectAndLockA(Long Aid);

The query will look like

SELECT a.column1, ... from tableA a LEFT JOIN tableB b ... FOR UPDATE of a,c

FOR UPDATE of a,c

The query will try to lock two tables what leads to exception like : org.postgresql.util.PSQLException: ERROR: FOR UPDATE cannot be applied to the nullable side of an outer join

What I try to archive is locking only first table "FOR UPDATE OF a"

Is it possible to configure somehow or tell Hibernate to lock only first table.

4 Answers 4

12

This is not supported by PostreSQL. If you do an outer SELECT nothing can prevent somebody from inserting a row into the LEFT JOINED table thereby modifiying the result set you are looking at (e.g. the columns would not be NULL anymore on a repeated read).

For a detailed explanantion see here

4

It's been a long time since question was created, but I have a similar problem and hope my answer will help somebody.

Suppose that we have this JPA entities:

@Entity
@Table(name = "card_transactions")
public class CardTransactionsEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "card_trans_seq")
    @SequenceGenerator(name = "card_trans_seq", sequenceName = "card_trans_seq")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({
            @JoinColumn(name = "ofd_id", referencedColumnName = "ofd_id"),
            @JoinColumn(name = "receipt_id", referencedColumnName = "receipt_id")})
    private ReceiptsEntity receipt;

    @Column
    @Enumerated(EnumType.STRING)
    private CardTransactionStatus requestStatus;

    ...

}
@Entity
@Table(name = "receipts")
public class ReceiptsEntity {
    @EmbeddedId
    private OfdReceiptId id; 

    ...
}

@Embeddable
public class OfdReceiptId implements Serializable {

    @Column(name = "ofd_id")
    @Enumerated(EnumType.STRING)
    private OfdId ofdId;

    @Column(name = "receipt_id")
    private String receiptId;

    ...
}

And we want select CardTransactionsEntity with fetched ReceiptsEntity for pessimistic update only CardTransactionsEntity. This can be done using Hibernate and Spring Data JPA repository as

public interface CardTransactionRepository extends JpaRepository<CardTransactionsEntity, Long> {

    @Query("select ct from CardTransactionsEntity ct left join fetch ct.receipt r where ct.requestStatus = :requestStatus")
    @Lock(value = LockModeType.PESSIMISTIC_WRITE)
    @QueryHints(value = {
            @QueryHint(name = "javax.persistence.lock.timeout", value = "-2"), // LockOptions.SKIP_LOCKED
            @QueryHint(name = "org.hibernate.lockMode.r", value = "NONE") // "r" is alias for ct.receipt and will excluded from PESSIMISTIC_WRITE
    })
    List<CardTransactionsEntity> loadCardTransactions(@Param("requestStatus") CardTransactionStatus requestStatus, Pageable pageable);

}

This repository method will execute query like

SELECT ct.*, r.* from card_transactions ct LEFT OUTER JOIN receipts r ON ct.ofd_id = r.ofd_id and ct.receipt_id = r.receipt_id WHERE ct.request_status=? LIMIT ? FOR UPDATE OF ct SKIP LOCKED
0
0

You can bypass this error with joining the tables with FetchType.LAZY. This fetch type is the default one and it is not required to specify for @OneToMany joins.

public class A{
   @Id
   private Long id;

   @OneToMany
   private Set<B> bSet;

   @OneToMany
   private Set<C> cSet;
}
0
0

Old question, but description of my use-case could be helpful to somebody.

I have a Join of lot of Tables in the same query + @EntityGraph to fetch something eagerly. But need to PESIMISTIC_LOCK only in one, main table.

Proposed early solution to exclude a table from lock is cumbersome(lot of exclusions), + unknown alias in case of @EntityGraph.

So, we can specify exact table for lock(not the whole hierarchy) by providing only @QueryHints:

//@Lock(LockModeType.PESSIMISTIC_WRITE) is inappropriate because it locks the whole hierarchy.
    @QueryHints(value = {
            @QueryHint(name = "org.hibernate.lockMode.ms", value = "PESSIMISTIC_WRITE") // "ms" is alias for MeasurementSet and will only be used in PESSIMISTIC_WRITE
    })

But even in this case there is a bug in Hibernate. Aliases from HQL are interferred by implicit aliases from @EntityGraph.

Solution: Get rid of @EntityGraph and fetch everything by HQL only.

@QueryHints(value = {
            @QueryHint(name = "org.hibernate.lockMode.ms", value = "PESSIMISTIC_WRITE") // "ms" is alias for MeasurementSet and will only be used in PESSIMISTIC_WRITE
    })
    @Query("SELECT ms from MeasurementSet ms "
            + "INNER JOIN ms.measurementSetGroup msg "
            + "LEFT JOIN FETCH ms.pomSections ps LEFT JOIN FETCH ps.pointsOfMeasure pom LEFT JOIN FETCH pom.pomMeasurements "
            + "LEFT JOIN FETCH ms.prototypeMilestones "
            + "WHERE ms.id = :id and msg.modelNumber = :modelNumber")
    Optional<MeasurementSet> findEagerlyByIdAndModelNumberWithLock(Long id, String modelNumber);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.