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

  • Home
  • SEARCH
  • 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 6340039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:45:45+00:00 2026-05-24T19:45:45+00:00

I have big enough and already working project, written not by a programmer, but

  • 0

I have big enough and already working project, written not by a programmer, but a scientist. The program holds lot of data in a huge object tree. Almost all of the classes involved are mutable and even more — they tend to change state of other objects in almost all methods, even getters and setters. This object tree is than stored via java serialization to disk.

My task is to migrate from serialization to database, to reduce memory consumption. Manual refactoring of this tons of mutables, changing each other at some unknown points in time is completely hell, especially for me — not knowing the subject domain.

Are there any approaches, practices, or refactoring patterns for such cases?

  • 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-24T19:45:47+00:00Added an answer on May 24, 2026 at 7:45 pm

    I think you have answered your question in the subject line. Using JPA or Hibernate with Annotations, you can take the existing object model and treat each object class as it’s own table. Don’t worry about constraints to start with.

    In every class, add an id property, so that it can be stored, and implement equals and hashCode. (so you’ll have to figure out what makes an object equal to another). This will help avoid the creation of duplicates in your database.

    You will need to write code to persist your existing records. Perhaps the Visitor pattern would be a good fit here. To each domain object you add a method which takes a visitor and invokes it to persist the object.

    public DomainObject { 
      public DomainObject() {} 
      private int id; public int getId() { return id; } public void setId(int id) { this.id = id; }
      private OtherObject myref;
      // properties for 'myref' and other existing public fields may need to be added
      void visit(IVisitor iv) {
        // traverse object tree to hit leafs first
        myref.visit(iv); 
        // pass to other referenced objects too.
        iv.visit(this);
      }
    
      public boolean equals(Object alt) { return alt.id == id; }
      public int hashCode() { return id.hashCode(); }
    }
    
    public interface IVisitor { 
      public void visit(Object o);
      public void visit(DomainObject do);
    }
    
    public class PersistVisitor implements IVisitor {
    
      // Your favorite injection annotation, perhaps, or populate through constructor
      EntityManager em;
    
      public void PersistVisitor(EntityManager em) { this.em = em; }
    
      public void visit(Object o) {
        em.persist(o);
      }
    
      public void visit(DomainObject do) {
        // anything you need to do specifically for the DomainObject type
        visit((Object)o);
      }
    
    }
    

    After the initial persisting of the records, when you next load in your root object from Session or EntityManager, Hibernate will take care of loading all the referenced instances.

    Over time, you can move business logic out of the model classes, but you won’t have to do this to start with. Hibernate will commit any changes made to any of the instances in the object model, such as those made by the existing code.

    (Alternatively I guess you could hook into the deserialization of the object tree in order to persist or lookup the objects as they are deserialized. I’ll leave that as an exercise for another user.)

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

Sidebar

Related Questions

I have big issue with url-rewriting for IIS 7.0. I've written simple module for
I have this big data-entry sort of page, a table kind of layout using
I have a big lump of binary data in a char[] array which I
I suddenly have a big problem I didn't have before. In my current project,
We are an iPhone Developer Program member. We've got a DUNS number but not
Does System.Activator.CreateInstance(T) method have performance issues (since I'm suspecting it uses reflection) big enough
How would you maintain the legacy applications that: Has no unit tests have big
I have a big string (let's call it a CSV file, though it isn't
i have a big web application running in perl CGI. It's running ok, it's
I have a big load of documents, text-files, that I want to search for

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.