Skip to content

Hibernate @Enity annotation changes Jackson serialization behavior #2267

Closed

Description

@efenderbosch

When an object is annotated with @entity, then Jackson doesn't serialize the id.

@Entity
public class Test implements Serializable {
    @Id
    private Long id;
    private String attribute;
    // getters, setters, toString, hashCode, equals, serialVersionUID, etc.
}

This will serialize to

{
  "attribute" : "attribute"
}

Commenting out @Entity will fix the serialization:

{
  "id" : 1,
  "attribute" : "attribute"
}

I've created a minimal project to demonstrate the problem here:
https://github.com/efenderbosch/spring-hibernate-jackson-error

Should I also submit this to the Hibernate project?

I'm using OpenJDK 1.7.0_65 on Ubuntu 14.04.

Activity

  1. added this to the 1.2.1 milestone on Jan 1, 2015
  2. philwebb commented on Jan 1, 2015

    @philwebb
    Member

    It appears to be PersistentEntityJackson2Module from spring-data-rest that is causing this behavior. The updateBuilder method is called which removes ID attributes.

    @olivergierke it looks like this is expected behavior, do we need to provide a quick global opt-out property.

    @efenderbosch Are you actually using Spring Data Rest? If not you could try removing it from your classpath. You can also exclude RepositoryRestMvcAutoConfiguration as a work-around.

  3. added
    status: waiting-for-feedback
    on Jan 1, 2015
  4. philwebb commented on Jan 1, 2015

    @philwebb
    Member

    BTW also tested with 1.1.10 so this isn't a recent regression.

  5. removed
    type: bug
    on Jan 1, 2015
  6. efenderbosch commented on Jan 1, 2015

    @efenderbosch
    Author

    I'm using Spring Data Rest because it brings in spring-web (HttpStatus) and spring-hateos (PagedResources). Our app is a pretty basic RESTful service w/ JPA.

    If there's a different spring-boot-starter or combination that will provide this functionality, I can use that instead.

  7. efenderbosch commented on Jan 1, 2015

    @efenderbosch
    Author

    I was hopeful that this would work, but it didn't:

        @Bean
        @Primary
        public RepositoryRestConfiguration repositoryRestConfiguration() {
            RepositoryRestConfiguration repositoryRestConfiguration = new RepositoryRestConfiguration();
            repositoryRestConfiguration.exposeIdsFor(Test.class);
            return repositoryRestConfiguration;
        }

    I'm not confident that my bean is replacing the default bean. If I leave off @Primary then I get "expected single matching bean but found 2."

  8. efenderbosch commented on Jan 1, 2015

    @efenderbosch
    Author

    This does work, however:

        @Bean
        @Primary
        public RepositoryRestConfiguration repositoryRestConfiguration(PersistentEntities persistentEntities,
                RepositoryRestConfiguration repositoryRestConfiguration) {
            for (PersistentEntity<?, ?> persistentEntity : persistentEntities) {
                repositoryRestConfiguration.exposeIdsFor(persistentEntity.getType());
            }
            return repositoryRestConfiguration;
        }
  9. removed
    status: waiting-for-feedback
    on Jan 2, 2015
  10. modified the milestones: 1.2.2, 1.2.1 on Jan 7, 2015
  11. wilkinsona commented on Feb 5, 2015

    @wilkinsona
    Member

    I'd like to make RepositoryRestMvcBootConfiguration's RepositoryRestConfiguration bean @ConditionalOnMissingBean, however Spring Framework won't let me. It evaluates the condition on the overriding method and determines that it should be skipped, but then looks at the unconditional overridden method on the superclass and creates the bean anyway. I've opened SPR-12695.

  12. wilkinsona commented on Feb 12, 2015

    @wilkinsona
    Member

    My comment above was with a view towards users declaring their own RepositoryRestConfiguration bean as @efenderbosch has attempted above. A major drawback of this approach is that the bean has to be called config as Spring Data REST references it by name.

    RepositoryRestMvcBootConfiguration was recently made public (#2392). This provides a much nicer way to customise RepositoryRestConfiguration. For example, here's how to enable the exposure of ids for an entity:

    @Configuration
    class CustomRepositoryRestMvcConfiguration extends RepositoryRestMvcBootConfiguration {
        @Override
        protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
            config.exposeIdsFor(City.class);
        }
    }

    This allows you to use Spring Data REST's approach to customising the configuration while also taking advantage of Boot's auto-configuration – any spring.data.rest.* properties will still be applied to the RepositoryRestConfiguration instance.

  13. wilkinsona commented on Feb 12, 2015

    @wilkinsona
    Member

    @efenderbosch If you're not actually using Spring Data REST, but simply looking for a starter to pull in spring-web and spring-hateoas, we added spring-boot-starter-hateoas recently (#2396). It'll be in 1.2.2.

  14. new23d commented on Sep 26, 2015

    @new23d

    Note that RepositoryRestMvcBootConfiguration has since been renamed to SpringBootRepositoryRestMvcConfiguration .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions