Title: How to get data from a table by entity class name using Spring Data JPA
Tags: spring;spring-data-jpa
Question: I have a base entity class ```BaseDictionary```:

```@Entity
@Inheritance
public abstract class BaseDictionary {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Long id;

    @Column(name = "code")
    protected String code;

    @Column(name = "is_enabled")
    protected Boolean enabled = true;
}
```

any child classes

```@Entity
@Table(name = DB.DIC_RIGHTS_TYPE, schema = DB.SCHEMA_DIC)
public class DicRightsType extends BaseDictionary {}

@Entity
@Table(name = DB.DIC_ROLES_TYPE, schema = DB.SCHEMA_DIC)
public class DicRolesType extends BaseDictionary {}
```

There are many child classes like this.

Given an entity class name like ```DicRightsType``` I would like to get data from the table associated with the entity of that name. How is it possible to implement?

I wanted to use JPA repositories like this: Using generics in Spring Data JPA repositories but this does not suit my case because I only have the name of the entity class at runtime and do not know how to create a dynamic repository for the class.


Here is the accepted answer: You can write your own ```JpaRepository``` implementation to achieve this.

```
  Step 1: A repository registry
```

```class RepositoryRegistrar {
  private static final Map<Class<T&gt;, Repository<T, Serializable&gt;&gt; DICTIONARY = new HashMap<&gt;();

  public static void register(Class<T&gt; entityClass, Repository<T, Serializable&gt; repository) {
    DICTIONARY.put(entityClass, repository);
  }

  public static Repository<T, Serializable&gt; get(Class<T&gt; entityClass) {
    return DICTIONARY.get(entityClass);
  }
}
```

```
  Step 2: Custom ```JpaRepository``` implementation to populate the registry
```

```@NoRepositoryBean
class RegisteringJpaRepository<T, ID extends Serializable&gt; extends SimpleJpaRepository<T, ID&gt; {
  public RegisteringJpaRepository(JpaEntityInformation<T, ?&gt; entityInformation, EntityManager entityManager) {{
    super(entityInformation, entityManager);

    RepositoryRegistrar.register(entityInformation.getJavaType(), this);
  }
}
```

You will have to tweak the configuration to use your custom implementation. Details for this are provided in the Spring Data JPA documentation.

```
  Step 3: Obtain repository references from the registrar
```

```Repository<?, ?&gt; getRepository(String entityClassName) {
  return RepositoryRegistrar.get(Class.forName(entityClassName));
}
```



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:09/24/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<spring><spring-data-jpa>', 'Title': 'How to get data from a table by entity class name using Spring Data JPA', 'ViewCount': 1548, 'id': 39675644, 'user_AboutMe': '', 'user_AccountId': 3478101.0, 'user_DisplayName': 'arman.s', 'user_Reputation': 3.0, 'user_Views': 7.0, 'user_id': 2910838.0}}