I have entities with combined inheritance:
Supporter
@Entity @Inheritance(strategy=InheritanceType.JOINED) @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "supporterType") @JsonSubTypes({ @JsonSubTypes.Type(value = PersonSupporterEntity.class, name = "PERSON"), @JsonSubTypes.Type(value = CompanySupporterEntity.class, name = "COMPANY") }) @DiscriminatorColumn(name="supporter_type") @Table(name = "supporter") public class SupporterEntity extends UpdatableEntity { private long id; private SupporterType supporterType; private PartnerEntity partner; ... }
PersonSupporter
@Entity @DiscriminatorValue("PERSON") @Table(name = "person_supporter") public class PersonSupporterEntity extends SupporterEntity { ... }
Companysupporter
@Entity @DiscriminatorValue("COMPANY") @Table(name = "company_supporter") public class CompanySupporterEntity extends SupporterEntity { ... }
I have another object that references SupporterEntity
@Entity @Table(name = "contact") public class ContactEntity extends UpdatableEntity { private long id; private SupporterEntity supporter; ... @ManyToOne
Storage facilities
@Transactional @RepositoryRestResource(collectionResourceRel = "supporters", path = "supporters") public interface SupporterEntityRepository extends JpaRepository<SupporterEntity, Long> { @Transactional(readOnly = true) @RestResource(path = "by-partner", rel = "by-partner") public Page<SupporterEntity> findByPartnerName(@Param("name") String name, Pageable pageable); }
@Transactional @RepositoryRestResource(collectionResourceRel = "person_supporters", path = "person_supporters") public interface PersonSupporterEntityRepository extends JpaRepository<PersonSupporterEntity, Long> { }
@Transactional @RepositoryRestResource(collectionResourceRel = "company_supporters", path = "company_supporters") public interface CompanySupporterEntityRepository extends JpaRepository<CompanySupporterEntity, Long> { }
@Transactional @RepositoryRestResource(collectionResourceRel = "contacts", path = "contacts") public interface ContactEntityRepository extends JpaRepository<ContactEntity, Long> { @Transactional(readOnly = true) @RestResource(path = "by-supporter", rel = "by-supporter") public ContactEntity findBySupporterId(@Param("id") Long id); }
I use Spring Boot, Spring Data REST, Spring Data JPA, Hibernate, Jackson. When I try to create a new ContactEntity with an email request as follows:
{ "supporter":"/supporters/52", "postcode":"1111", "city":"Test City 1", "address":"Test Address 1", "email":" test1@email.com ", "newsletter":true }
I get this exception:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (VALUE_STRING), expected FIELD_NAME: missing property 'supporterType' that is to contain type id (for class com.facer.domain.supporter.SupporterEntity) at [Source: HttpInputOverHTTP@4321c221 ; line: 1, column: 2] (through reference chain: com.facer.domain.supporter.ContactEntity["supporter"]) at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148) ~[jackson-databind-2.4.4.jar:2.4.4]
After two days of debugging, I found a way, but I kind of figured it out. Therefore, if I publish it as follows:
{ "supporter":{ "supporterType":"PERSON", "id":"52" }, "postcode":"1111", "city":"Test City 1", "address":"Test Address 1", "email":" test1@email.com ", "newsletter":true }
It works, but I donβt know why. What is wrong with another request? It works just like everywhere else when the reference object has no inheritance.