0

In spring data, I have a Entity that use EntityStatus.

When I serialized json using my @Controller, the id of EntityStatus is removed.

This is how look the generated entity:

{
  "description" : "Some testing",
  "version" : null,
  "createdDate" : "2020-03-10T05:46:25.516Z",
  "createdById" : null,
  "lastModifiedById" : null,
  "title" : "This is a test",
  "content" : "Foo bar fizz buzz",
  "userId" : 2,
  "category" : {
    "description" : "Foobar",
    "version" : null,
    "createdDate" : "2020-03-10T05:18:30.282Z",
    "lastModifiedDate" : "2020-03-10T05:18:41.827Z",
    "createdById" : null,
    "lastModifiedById" : null,
    "deleted" : false
  },
  "status" : {
    "description" : "Created"
  },
  "deleted" : false,
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/management/entity/5"
    }
  }
}

How can I force it to be included for all sub-entities globally?

Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204

1 Answers1

1

You can configure this using the RepositoryRestConfigurerAdapter. If you want to have it for all your entities, just iterate over all of them and expose their ids:

@Configuration
public class MyRepositoryRestConfigurerAdapter extends RepositoryRestConfigurerAdapter {

    @Autowired
    private EntityManager entityManager;

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(entityManager.getMetamodel().getEntities()
              .stream().map(EntityType:getJavaType))
              .collect(Collectors.toList())
              .toArray(new Class[0]));
    }

}

If you just want it for specific entities, filter them out while doing the processing above.

UPDATE: Another more manual approach without using the EntityManager might be to explicitly add all entities:

@Configuration
public class MyRepositoryRestConfigurerAdapter extends RepositoryRestConfigurerAdapter {
    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(YourEntityOne.class);
        config.exposeIdsFor(YourEntityTwo.class);
    }
}
rieckpil
  • 10,470
  • 3
  • 32
  • 56
  • I have : `Field entityManager in com.myapp.api.config.context.security.RepositoryRestConfig required a bean of type 'javax.persistence.EntityManager' that could not be found. ` – Dimitri Kopriwa Mar 10 '20 at 06:21
  • 1
    do you use a relational database and Spring Data JPA or something different for presistence? – rieckpil Mar 10 '20 at 06:23
  • I tried to add a `@ConditionalOnBean(EntityManager.class)` in `MyRepositoryRestConfigurerAdapter` , but the method is not called and the id are still not exposed. I use Spring data with spring data mybatis : https://github.com/hatunet/spring-data-mybatis – Dimitri Kopriwa Mar 10 '20 at 06:26
  • 1
    ah okay, that's important because MyBatis does not use JPA and hence you won't get a ready to inject `EntityManager` which is part of the JPA spec and implemented by Hibernate – rieckpil Mar 10 '20 at 06:28
  • Ok, is there another way to achieve the desired results? – Dimitri Kopriwa Mar 10 '20 at 06:29
  • 1
    I've added another solution, can you try this? – rieckpil Mar 10 '20 at 06:34