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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:06:52+00:00 2026-06-05T23:06:52+00:00

I have an entity named Commercial. I have an Category entity where the list

  • 0

I have an entity named Commercial. I have an Category entity where the list of commercial categories are hold. For each category there is an separate entity extending Commercial(like RestaurantCommercial, PhotoStudioCommercial etc. total up to 20 entities) with JOINED inheritance strategy.

Commercial entity holds up general properties like title, text contactnumber of some company’s commercial, while RestaurantCommercial and PhotoStudioCommercial holds additional specific properties concerned with that category.

The problem is that writing a separate dao and controller for each entity is a bit plenty of work, so I am searching for a neat way to handle this issue.

I need an unified controller and may be the DAO for handling the form control and persisting new instances of the entities that extend Commercial.

Here is approximately what I was thinking about:

@RequestMapping(value={"/vendor/commercial/{categoryName}/new"}, method=RequestMethod.GET)
public String showNewCommercialForm(@PathVariable("categoryName") String categoryName,
                                    Map<String, Object> map) {
    Category category = categoryDAO.getCategoryByServiceName(categoryName);

    Class clazz = Class.forName(category.getClassName()); //here className is smth like domain.commercial.RestaurantCommercial

    map.put("commercialInstance", clazz.newInstance());
    //separate view for each category of commercial
    return "vendor/commercial/"+categoryName+"/new";
}

And I was thinking for a similar controller for saving form data even if I would have to write a sperate binder for this stuff.

So the question is: What would you suggest to handle this issue or what would be the best practice if you had already faced similar need(Generics, Reflection or smth else)? Or if that would be worthy or not and why?
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-06-05T23:06:54+00:00Added an answer on June 5, 2026 at 11:06 pm

    I create a Dao interface for such cases:

    public interface Dao<T extends Commercial> {
    
        T getAll();
    }
    

    After that an abstract Dao implementation, for example hibernate based:

    public CommonHibernateDao<T extends Commercial> implements Dao<T> 
    {
        private final Class<T> entityType;
    
        protected CommonHibernateDao(Class<T> entityType) {
            this.entityType = entityType;
        }
    
        public List<T> getAll() {
            // hibernate get all implementation
        }
    }
    

    And RestaurantCommercial Dao interface and implementation:

    public interface RestaurantCommercialDao extends Dao<RestaurantCommercial> {
    
    }
    
    public class HibernateRestaurantCommercialDao extends CommonHibernateDao<RestaurantCommercial> implements RestaurantCommercialDao {
    
        public HibernateRestaurantCommercialDao() {
            super(RestaurantCommercial.class);
        }
    }
    

    All implementation goto CommonHibernateDao. In it’s subclasses only constructor calling neaded. Basically you can do it with reflection but as for me it is not clear.

    For controller (something like RESTfull API):

    @Controller
    public class YourController() {
    
        @RequestMapping(value = "/restapi/{entityType}")
        public String postEntity(HttpServletRequest request, @PathVariable(value = "entityType") String entityType) {
            // If enetity name will be packegae + class name
            Object beanInstance = Class.forName(entityName);
            ServletRequestDataBinder binder = new ServletRequestDataBinder(beanInstance);
            binder.bind(request);
    
            // Here by type of entity you can get DAO and persist
        }
    }
    

    If form input names will be same to your bean names – binding will do all automatically.

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

Sidebar

Related Questions

For example, I have an entity named Profile with attributes like name, gender, dateOfBirth,
I have an entity in EF named Profile and I would like to add
I would like to have an entity named GROUP in my JPA setup. Now
I have a core data entity named Folder. Each Folder has a 1-to-many relationship
I have a fairly simple question. There is a named HTML entity in most
Right now, I have a domain entity named StyleBundle. This StyleBundle takes a list
I have an entity named 'account' which will have a username column and I
Lets say thay I have an entity with name A.It has two columns named
I have an old database (with terribly named tables and columns) and an entity
I have a question regarding DataBase design. I have a Entity Table named: CARS

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.