The fastest way to update a table row when using Hibernate and Oracle

The fastest way to update a table row when using Hibernate and Oracle
Techiio-author
Written by Sagar RabidasFebruary 23, 2022
8 min read
Hibernate
3 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 3 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Now we start the discussion on the above topic.

In this blog, I’m going to show you how you can access a database record by its ROWID when using Hibernate.

@RowId:-

The first thing to do is to annotate the JPA entity using the Hibernate-specific @RowId.

Considering that we have the following two entities:

@Entity(name = "Post")
@Table(name = "post")
@RowId( "ROWID" )
public class Post {
 
    @Id
    private Long id;
 
    private String title;
 
    @OneToMany(
        cascade = CascadeType.ALL,
        mappedBy = "post",
        orphanRemoval = true
    )
    private List<PostComment> comments = new ArrayList<>();
 
    //Getters and setters omitted for brevity
 
    public void addComment(PostComment comment) {
        comments.add(comment);
        comment.setPost(this);
    }
 
    public void removeComment(PostComment comment) {
        comments.remove(comment);
        comment.setPost(null);
    }
}
 
@Entity(name = "PostComment")
@Table(name = "post_comment")
@RowId( "ROWID" )
public class PostComment {
 
    @Id
    @GeneratedValue
    private Long id;
 
    @ManyToOne(
        fetch = FetchType.LAZY
    )
    private Post post;
 
    private String review;
 
    //Getters and setters omitted for brevity
 
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof PostComment)) return false;
        return
            id != null &&
            id.equals(((PostComment) o).getId());
    }
 
    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}

The @RowId( "ROWID" ) mapping tells Hibernate to use the ROWID pseudo column when accessing the database record when executing an UPDATE statement.

Testing time:-

Assuming we have the following entities in our database:

Post post = new Post();
post.setId(1L);
post.setTitle(
    "High-Performance Java Persistence"
);
 
entityManager.persist(post);
 
PostComment comment1 = new PostComment();
comment1.setReview("Great!");
post.addComment(comment1);
 
PostComment comment2 = new PostComment();
comment2.setReview("To read");
post.addComment(comment2);
 
PostComment comment3 = new PostComment();
comment3.setReview("Lorem Ipsum");
post.addComment(comment3);

When executing the following test case:

SELECT p.id AS id1_0_0_,
       p.title AS title2_0_0_,
       p.ROWID AS rowid_0_,
       c.post_id AS post_id3_1_1_,
       c.review AS review2_1_1_,
       c.ROWID AS rowid_1_,
       c.post_id AS post_id3_1_0__,
       c.id AS id1_1_0__
FROM   post p
INNER JOIN
       post_comment c ON p.id = c.post_id
WHERE  p.id = 1
 
-- Merge SELECT query skipped for brevity
 
UPDATE post_comment
SET    post_id = 1,
       review = 'Must read!'
WHERE  ROWID = AAAwmzAAEAAACZDAAA
 
DELETE FROM
       post_comment
WHERE  id = 3

The SELECT statement includes the ROWID pseudo column which is stored in the currently running Persistence Context. At some point in the merge entity kingdom transition, hibernate copies the detached entity nation onto the newly fetched entity versions, and the grimy checking mechanism propagates the adjustments to the database.

Best the update announcement blessings from the ROWID pseudo column, the delete assertion does not (but). The HHH-11761 goes to attend to this problem, but it's going to only be addressed in 6.0.

Conclusion:-

Hibernate isn't simply an ORM tool, but full-blown records get admission to a framework, offering all types of performance optimizations. If you are the use of oracle and you execute lots of replacing statements, it makes a whole lot of experience to apply the @rowid mapping.

Hibernate
Oracle
Java
Rowld
3 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 3 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Sagar Rabidas
Software Developer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+8 more
300 Blog Posts
14 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance