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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:42:54+00:00 2026-05-23T23:42:54+00:00

I want to implement a sort of transfer object pattern. So, i have a

  • 0

I want to implement a sort of transfer object pattern. So, i have a method that fills object´s properties via BeanUtils.copyProperties(..,..) (apache common).

Here is the code:

public abstract class BaseTO<TObject> {

    public Long id;

    /***
     * Obtains id of the object that´s transfered
     * @return object´s id
     */
    public Long getId() {
        return id;
    }

    /****
     * set transfered object´s id 
     * @param id object´s id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /***
     * Fill transfer object properties.
     * @param entity entity to be transfered
     * @return self reference
     */
    public BaseTO<TObject> build(TObject entity){
        try {
            BeanUtils.copyProperties(this, entity);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        customDataTransformation(entity);
        return this;
    }

    protected void customDataTransformation(TObject entity) {
    }


}

CustomerTO Class

public class CustomerTO extends BaseTO<Customer> {

    private String name;
    private String surname;
    private String email;
    private String sex;
    private DocumentType documentType;
    private String documentNumber;

    --- getters and setters

    @Override
    protected void customDataTransformation(Customer entity) {
        this.sex = Sex.MALE.equals(entity.getSex()) ? "M" : "F";
    }


}

the problem

CustomerTO toEdit = (CustomerTO) (customerId!=null ? new CustomerTO().build(entityObject):new CustomerTO());

as you can see here have to cast to (CustomerTO). I want if it´s possible avoid that, to make the code simpler.

Is it posible that public BaseTO build(TObject entity) can return the object of the subclass??

I hope to be clear.

Thanks in advance.

  • 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-23T23:42:55+00:00Added an answer on May 23, 2026 at 11:42 pm

    Maybe try this:

        class BaseTO<TObject, R extends BaseTO<TObject,R>> {
    
        public R build(TObject entity) {
    

    and then CustomerTO:

    class CustomerTO extends BaseTO<Customer, CustomerTO> {
    

    or less restrictively, only change the build signature:

    public <X extends BaseTO<TObject>> X build(TObject entity) {
    

    But IMHO better approach will be simply adding constructor to TO with TObject parameter.

    public BaseTO(TObject entity) {
            try {
                BeanUtils.copyProperties(this, entity);
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            customDataTransformation(entity);
    }
    

    then in each extending class create simple constructor

         public CustomerTO(Customer entity) {
            super(entity);
        }
    

    and forget about the build method and use it simply

    CustomerTO toEdit = (customerId!=null ? new CustomerTO(entityObject):new CustomerTO());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement some sort of lookup table in C++ that will act
I want to implement a delegate solution for Bubble sort. I have this code:
I want to implement a sort of preview mechanism for my application. I have
I want to implement a sort of a singleton object ( About in the
I want to implement bubble sort with writing to the file. I think that
i want to implement a sort of feedback form/survey form in asp.net which is
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to implement a sort of backup feature for my backup app, but
I am working on a little project and want to implement some sort of
I want to implement something similar on my site. Submissions have up_votes and down_votes.

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.