Within a Spring Boot application, I have a class A that has a reference to another class B. The reference in class A is annotated with @ManyToOne.
When I get the records from class A, each reference to class B is expanded to show the values in B.
If I add an @JsonIdentityInfo annotation, then listing class A only expands the class B reference the first time it is retrived. Subsequent class A records with the same class B ref simply have the id.
What annotation do I need not to expand the class B ref so that each class A record simply shows the class B id.
Updated with code
Climate.java
@Entity
@SequenceGenerator(name = "climate_gen", sequenceName = "climate_gen", initialValue = 100)
public class Climate {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "climate_gen")
private long id;
private float temperature;
private float humidity;
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime date;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "location_id", nullable = false)
private Location location;
Location.java
@Entity
@SequenceGenerator(name = "location_gen", sequenceName = "location_gen", initialValue = 100)
public class Location {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "location_gen")
private long id;
private String name;
private String gps;
private String timezone;
So setting fetchType to EAGER gives an expanded location record for each climate.
Setting fetchtype to LAZY gives an error
$ curl -k -w "\n" -H "Authorization: Bearer $TOKEN" https://mint191:8453/api/v1/climates {"timestamp":"2019-12-23T16:33:36.047+0000","status":500,"error":"Internal Server Error","message":"Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->com.norricorp.bikes.model.Climate[\"location\"]->com.norricorp.bikes.model.Location$HibernateProxy$atNV2KHn[\"hibernateLazyInitializer\"])","path":"/api/v1/climates"}
Regards,