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 6886707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:47:36+00:00 2026-05-27T05:47:36+00:00

So I have a class with three fields that maps to a table using

  • 0

So I have a class with three fields that maps to a table using hibernate

Class Widget
{
     String field1;
     String field2;
     String field3;
}

On application startup a number of instances these widgets will be added to the database from an external files, but when I exit the application I need to know which (if any) of these fields have been changed by the user since the application was started, so the changes can be saved back to the files. I also need to store the original value for logging purposes.

I can’t work whether I need a status field in the table or whether there is already a way of doing this using Hibernate/Database.

EDIT:A good solution to the program was given below . however the main reason I am using Hibernate is to reduce memory consumption so storing the original values when changed is not a good solution for me , I want everthing stored in the database. So I have create this new question How do I store a copy of each entity I add to database in Hibernate

  • 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-27T05:47:36+00:00Added an answer on May 27, 2026 at 5:47 am

    Given an entity like the following you can track changes on one of it’s field (while preserving its original value too).

    @Entity
    @Table(schema = "test", name = "test")
    public final class Test {
    
      private static final int ORIGINAL = 0;
      private static final int CURRENT = 1;
    
      private Integer id;
    
      // holds the original and current state of the field
      private final AtomicReferenceArray<String> field = new AtomicReferenceArray<>(2);
    
      @Id
      public Integer getId() {
        return id;
      }
    
      public void setId(Integer id) {
        this.id = id;
      }
    
      @Transient
      public String getOriginalField() {
        return field.get(ORIGINAL);
      }
    
      @Basic
      public String getField() {
        return field.get(CURRENT);
      }
    
      public void setField(String field) {
        this.field.compareAndSet(ORIGINAL, null, field);
        this.field.set(CURRENT, field);
      }
    
      @PreUpdate
      public void preUpdate() {
        System.out.format("Original: %s, New: %s\n", getOriginalField(), getField());
      }
    
      ...
    

    }

    If there is a single row in a database like this:

         id: 1
      field: a
    version: 2011-12-02 11:24:00
    

    before the field gets updated (say, from a to b) you’ll get the following output.

    Original: d, New: b
    

    The original value gets preserved even if the the entity is updated multiple times and both state can be accessed through the corresponding getters (getField and getOriginalField—you can get more creative than me in the naming :).

    This way, you can spare yourself from creating version columns in your database and also can hide the implementation details from clients.

    Instead of an AtomicReferenceArray you could use arrays, lists, etc, to track all changes like this way.

    The @PreUpdate isn’t necessary of course, but this way you can be notified of changes in the entity’s state and atomically save the updated fields into file. There more annotations like these: see the documentation for javax.persistence for other annotation types.

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

Sidebar

Related Questions

I have three files: one called sql.php witch has a class db that I
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
I have created a JPQL like this: select sum(c.field1) field1, sum(c.field2) field2, sum(c.field3) field3
Imagine that we have a poco that maps a table in a db. There
I have an EF object called PagePreference. The corresponding table contains three fields: UserName(PK,
I have a class that is maps to a field in a database. The
I have a some class, which contains three fields: protected bool _isRunning = false;
I have a library base class ( Controller ) and three sub-classes that inherit
I have an app with a login page that has three fields for three

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.