Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hibernate @Enity annotation changes Jackson serialization behavior #2267

Closed
efenderbosch opened this issue Dec 31, 2014 · 9 comments
Closed
Milestone

Comments

@efenderbosch
Copy link

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.

@philwebb philwebb added this to the 1.2.1 milestone Jan 1, 2015
@philwebb philwebb added the type: bug A general bug label Jan 1, 2015
@philwebb
Copy link
Member

philwebb commented Jan 1, 2015

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.

@philwebb philwebb added the status: waiting-for-feedback We need additional information before we can continue label Jan 1, 2015
@philwebb
Copy link
Member

philwebb commented Jan 1, 2015

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

@philwebb philwebb removed the type: bug A general bug label Jan 1, 2015
@efenderbosch
Copy link
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.

@efenderbosch
Copy link
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."

@efenderbosch
Copy link
Author

This does work, however:

    @Bean
    @Primary
    public RepositoryRestConfiguration repositoryRestConfiguration(PersistentEntities persistentEntities,
            RepositoryRestConfiguration repositoryRestConfiguration) {
        for (PersistentEntity<?, ?> persistentEntity : persistentEntities) {
            repositoryRestConfiguration.exposeIdsFor(persistentEntity.getType());
        }
        return repositoryRestConfiguration;
    }

@philwebb philwebb removed the status: waiting-for-feedback We need additional information before we can continue label Jan 2, 2015
@philwebb philwebb modified the milestones: 1.2.2, 1.2.1 Jan 7, 2015
@wilkinsona
Copy link
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.

@wilkinsona
Copy link
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.

@wilkinsona
Copy link
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.

@new23d
Copy link

new23d commented Sep 26, 2015

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants