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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:35:15+00:00 2026-05-26T22:35:15+00:00

I have a class which does operations on the database, but I want to

  • 0

I have a class which does operations on the database, but I want to keep it as general and abstract as possible, thus by Object return types.

Here’s an example method of that class:

public static List<Object> getAll(String table) throws HibernateException{
        Session sessie = HibernateUtil.getSessionFactory().openSession();
        Transaction trans = sessie.beginTransaction();

        List<Object> objects = sessie.createSQLQuery("SELECT * FROM " + table + ";").list();
        trans.commit();
        sessie.close();
        return objects;
 }

Now I have another class User which should have a a method getAll with a List<User> return type and the implementation of the method is the same.

Is this even possible? I know it isn’t possible to overload methods with different return types. Is this doable with some kind of a design pattern, interface setup?

So basicly I don’t want to do the implementation again, it is already specified in the getAll method which returns an object. I just want to perform that method but with dynamic return types.

The reason I am asking this is because I don’t want to do the following somewhere else in my code:

User user = new User(...);
// this should be user.getAll() and the type of that method should be an User, not an object
(User)user.getAll() 

The reason I am doing it this way is because I can have other tables in the future whom would be able to call the getAll method but get a cast object back as return type and not the type object.

I am looking at the cleanest and best OO solution, even if it means that I need to restructure my 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-26T22:35:15+00:00Added an answer on May 26, 2026 at 10:35 pm

    If you really need it you can make your class generic with returned type as generic type parameter, so you can have:

    public static List<T> getAll(String table)
    

    and then do

    user = new YourGenericClass<User>();
    List<User> list = user.getAll("user");
    

    It’s ugly though as you pass the name of the table as a parameter. You should probably rethink your data access layer design .

    edit:

    Ok to make it clearer:

    //Template generic class
    public class GenericDAOAndModelClass<T> {
        public static List<T> getAll(String table) { your code};
        public static T getOne(String table) { your other code };
    
    }
    
    public class User extends GenericDAOAndModelClass<User> {
        user methods and fields
    
    }
    

    and then you can call it like

    List<User> userList = User.getAll("user");
    
    User u = User.getOne("user");
    

    You could put a static field in in every class extending GenericDAOAndModelClass to represent table name, so that you don’t have to pass the parameter to getAll and getOne.

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

Sidebar

Related Questions

I have YoutubeVideoService class which does CRUD(Create, Read, Update, and Delete) operations. In my
I have made an class which conforms to the NSCoding protocol and does all
I have a class named toto which I send to a function that does
Does css hover work on mobile devices? I have a css hover class which
So, I have a base class which has a private locking object like so:
We have a class library that does some basic operations similar to an ORM,
I'm writing a semi-simple database wrapper class and want to have a fetching method
I have a class which I'm serialising to send over a unix socket and
I have a class which implements UserControl. In .NET 2005, a Dispose method is
I have a class which is marked with a custom attribute, like this: public

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.