Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4039590
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:38:05+00:00 2026-05-20T12:38:05+00:00

I am building a new web app and I am using Spring, JPA/Hibernate, and

  • 0

I am building a new web app and I am using Spring, JPA/Hibernate, and Postgres. Some of my tables have creation_ts and lastupdate_ts columns which are timestamp columns that track when an insert occurred and when the last update occurred on a row.

I am also using a naming convention for columns in my tables so as a matter of design policy every table is guaranteed to have two columns pkey which is an integer surrogate key, and version for optimistic locking.

I have two ways to keep these fields up to date.

Option A: use triggers

This is the solution I have in place right now, I have two Postgres trigger that fire on insert and update and will keep these fields up to date. and I have two classes.

@MappedSuperclass
public abstract class PersistableObject
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="pkey")
    private Integer pkey;

    @Version
    @Column(name="version")
    private Integer version;

    public Integer getPkey()
    {
        return this.pkey;
    }

    public Integer getVersion()
    {
        return this.version;
    }
}

and I have

@MappedSuperclass
public class TimeStampedPersistableObject extends PersistableObject {

    @Column(name = "creation_ts")
    @Temporal(TemporalType.DATE)
    @org.hibernate.annotations.Generated(value = GenerationTime.INSERT)
    private Date    creationTimestamp;

    @Column(name = "update_ts")
    @Temporal(TemporalType.DATE)
    @org.hibernate.annotations.Generated(value = GenerationTime.ALWAYS)
    private Date    updateTimestamp;

    public Date getCreationTimestamp()
    {
        return this.creationTimestamp;
    }

    public Date getUpdateTimestamp()
    {
        return this.updateTimestamp;
    }
}

Option B: Use JPA listeners

In this option I would use JPA listeners to keep to timestamp columns up-to-date.

My Question:

Which of those two approaches is better? As I see things here is my personal list of pros and cons of each option and I very interested from hearing the experience of others with these two choices.

Option A pros:

  1. Database is doing the updates with the triggers so there is no danger of having clock skew in the cluster running the web app.
  2. If a non-JPA application accesses the database the requirement to keep those two columns is enforced.

Option A cons:

  1. Have to do a select after the insert and update to read the values that the triggers put into place.
  2. I am using hibernate annotations to read back the values

Option B pros:

  1. Less typing when creating the DDL
  2. No need to read back values from the database after insert and update
  3. Pure JPA annotations no hibernate specific annotations

Option B cons:

  1. Danger of clock skew in the cluster
  2. Fields set whenever the JPA provider decides to call the callback methods not predictable

How you would solve this problem for a new app where you have total control over the database and the java code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:38:06+00:00Added an answer on May 20, 2026 at 12:38 pm

    Have to do a select after the insert and update to read the values that the triggers put into place.

    You can use INSERT ... RETURNING or UPDATE ... RETURNING to retrieve the values that were changed by the trigger, so there is no need to do another SELECT.

    Apart from that, I’d say it depends on your environment. If the application is mission critical and will fail miserably if those columns aren’t maintained correctly, then I’d stick with the triggers.

    If this is only for convenience in the front end (and it can handle conflicts due to incorrect values gracefully), then the JPA approach is probably easier to maintain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a web app for my lesson using java servlets. At some point
I'm building an app which searches the web using search engine. I have one
I'm new to Clojure and building a web app using the Noir framework (very
I'm building a web app using EF Code First and ASP.NET MVC. I have
I'm building a new web app that has a requirement to generate an internal
I am building a new web-app, LAMP environment... I am wondering if preg_match can
I'm building a web application using Yii framework, and i'm quite new to it.
I am building a facebook platform web app using GWT and hosting it on
I am building an MVC3 web app and new to .NET and programming in
I'm building a web app using MVC architecture in PHP, and one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.