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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:32:04+00:00 2026-05-15T05:32:04+00:00

So here’s my conundrum. I am programming a tool that needs to work on

  • 0

So here’s my conundrum. I am programming a tool that needs to work on old versions of our application. I have the code to the application, but can not alter any of the classes. To pull information out of our database, I have a DTO of sorts that is populated by Hibernate. It consumes a data object for version 1.0 of our app, cleverly named DataObject. Below is the DTO class.

public class MyDTO {  
    private MyWrapperClass wrapper;  

    public MyDTO(DataObject data) {
        wrapper = new MyWrapperClass(data);
    }
}

The DTO is instantiated through a Hibernate query as follows:

select new com.foo.bar.MyDTO(t1.data) from mytable t1

Now, a little logic is needed on top of the data object, so I made a wrapper class for it. Note the DTO stores an instance of the wrapper class, not the original data object.

public class MyWrapperClass {

    private DataObject data;

    public MyWrapperClass(DataObject data) {
        this.data = data;
    }

    public String doSomethingImportant() { ... version-specific logic ... }
}

This works well until I need to work on version 2.0 of our application. Now DataObject in the two versions are very similar, but not the same. This resulted in different sub classes of MyWrapperClass, which implement their own version-specific doSomethingImportant(). Still doing okay. But how does myDTO instantiate the appropriate version-specific MyWrapperClass? Hibernate is in turn instantiating MyDTO, so it’s not like I can @Autowire a dependency in Spring.

I would love to reuse MyDTO (and my dozens of other DTOs) for both versions of the tool, without having to duplicate the class. Don’t repeat yourself, and all that. I’m sure there’s a very simple pattern I’m missing that would help this. Any suggestions?

  • 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-15T05:32:05+00:00Added an answer on May 15, 2026 at 5:32 am

    My co-worker and I tried many options. We settled on using Hibernate’s poorly documented ResultTransformer interface (really, Hibernate, the lack of documentation there is shameful). Although use of the Transformer forced us to manually parse an Object[] array in our MyDTO constructor, it was a worthy tradeoff.

    In the ResultTransformer, we injected a version-specific WrapperFactory through Spring. We changed our queries to allow ResultTransformer to instantiate MyDTO, and voila! Problem solved. Below are our modified query and DTO class:

    "select t1.data from mytable t1"
    

    public class MyDTO<T> {  
        private MyWrapperClass wrapper;  
    
        public MyDTO(Object[] fields, WrapperFactory<T> wrapperFactory) {
            T data = (T) fields[0];        
            wrapper = wrapperFactory.newWrapper(data);
        }
    }
    

    As per my comments to Guillaume, the Interceptor did not work as hoped. Presumably because MyDTO is not a persistent class.

    We also tried having the DTO access the ApplicationContext directly through a singleton class, and from there obtaining the WrapperFactory. Although this worked, it predictably fubar’d our unit tests, and we scrapped the approach.

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

Sidebar

Related Questions

Here is my code (Say we have a single button on the page that
Here is the issue I am having: I have a large query that needs
Here is the problem that I am trying to solve. I have two folders
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here's my scenario - I have an SSIS job that depends on another prior
Here is an object that I'd like to use with ng-repeat, but it's not
Here's my code, what I'm trying to do is have javascript check a form
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code in a function I'm trying to revise. This example works
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`

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.