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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:49:44+00:00 2026-05-29T13:49:44+00:00

I have set of classes which inherit from a single super class: Super |

  • 0

I have set of classes which inherit from a single super class:

   Super
     |
     +------+-------+
     Aaaa   Bbbb    Cccc

Each of the Aaaa,Bbbb,Cccc then should contain method findByTag. The problem is that I can’t manage to define it generally. Following example defines specific findByTag for Aaaa.

public interface AaaaRepository extends SuperRepository<Aaaa> {
    @Query("select distinct a from Aaaa a " +
            "join a.tags t " +
            "join fetch a.locale where t = ?1")
    public List<Event> findByTag(Tag t);
}

Note that the Superclass is @MappedSuperclass and does not have its own table in database.

I would like to use some kind of “Super” in the query which would be replaced in each class by its name.

My second problem is that I don’t know how to force @ElementCollection to be Eagerly fetched. I have to always explicitly say “join fetch” in the query. If it is not fetched, once the transaction is finished, I can’t access those objects, which I did not explicitly fetched. (LazyFetch Exceptions…)

Thanks

  • 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-29T13:49:45+00:00Added an answer on May 29, 2026 at 1:49 pm

    Looking at the documentation, custom implementations section, what about this approach:

    1. Create an interface that extends repository and has your findByTag method, without annotations.
    2. Create an implementation of that class, and in the method implementation you use the JPA criteria. You also need a class field to hold the actual class for the domain object, because generics are erased at compilation time. Then you use that field to build the criteria.
    3. Read the documentation to use this implementation as a base class for the repository factory, then Spring Data will build implementations for the other repositories based on this custom one.

      public interface MyRepository<T, ID> extends JpaRepository<T, ID> {        
          public List<Event> findByTag(Tag t);
      }
      
      public class MyRepositoryImpl<T, ID> implements MyRepository<T, ID> {
      
          private Class<T> actualClass; // initialized in the constructor
      
          public List<Event> findByTag(Tag t) {
               // here you build the criteria using actualClass field, and execute it.
          }
      }
      
      public interface AaaaRepository extends MyRepository <Aaaa, Integer> {
          // other methods...
      }
      

    Look at “Example 1.16. Custom repository factory bean” of the documentation to create the factory bean.

    When Spring instantiates the implementation of AaaaRepository, it will use MyRepositoryImpl as base class.

    Will this work for you?

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

Sidebar

Related Questions

I have set of classes. Each class is inherited from another class. The relationship
I have a class called ItemBase from which a number of classes inherit. In
I have a set of XSDs from which I generate data access classes, stored
I have a common base class from which all my ASMX webservice classes will
I have a set of some classes which are all capable of being constructored
I have classes which have automatic properties only like public customerName {get; set;}. They
I have a set of classes which I am using for a Data Access
I have a set of classes which all take part in an inheritance chain,
I have a set of classes and interfaces which have a relatively simple hierarchy,
If have a set of classes that all implement an interface. interface IMyinterface<T> {

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.