Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is @Fetch annotation in hibernate?

Tags:

orm

hibernate

jpa

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@Fetch(FetchMode.SUBSELECT)
@JoinColumn(name = "ORU_OAUTH_ID", nullable = false)
@OrderBy("ORU_ORDER ")
private List<RedirectedURLs> acceptedReturnUrls;
  1. In this code i want to understand what @Fetch(FetchMode.SUBSELECT) does ?
  2. what is the difference between orphanRemoval and CascadeType.DELETE ?
like image 319
Mohammad Faizan Avatar asked Oct 17 '22 13:10

Mohammad Faizan


1 Answers

  1. If this link can help you.
    1. For CascadingType.DELETE and orphanRemoval

Cascading Remove

Marking a reference field with CascadeType.REMOVE (or CascadeType.ALL, which includes REMOVE) indicates that remove operations should be cascaded automatically to entity objects that are referenced by that field (multiple entity objects can be referenced by a collection field):

@Entity
class Employee {
     :
    @OneToOne(cascade=CascadeType.REMOVE)
    private Address address;
     :
}

Orphan Removal

JPA 2 supports an additional and more aggressive remove cascading mode which can be specified using the orphanRemoval element of the @OneToOne and @OneToMany annotations:

@Entity
class Employee {
     :
    @OneToOne(orphanRemoval=true)
    private Address address;
     :
}

DIFFERENCE:-

The difference between the two settings is in the response to disconnecting a relationship. For example, such as when setting the address field to null or to another Address object.

  • If orphanRemoval=true is specified the disconnected Address instance is automatically removed. This is useful for cleaning up dependent objects (e.g. Address) that should not exist without a reference from an owner object (e.g. Employee).

  • If only cascade=CascadeType.REMOVE is specified no automatic action is taken since disconnecting a relationship is not a remove operation.

(Cascading Remove and Delete are synonyme)

From here.

like image 84
P.Seg Avatar answered Oct 21 '22 03:10

P.Seg