1

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,

John
  • 1,593
  • 3
  • 17
  • 28

2 Answers2

0

If I got you correctly then I guess you are searching for the LazyLoading annotation inside the OneToMany Annotation

  @OneToMany(fetch = FetchType.LAZY)

eg.: https://www.baeldung.com/hibernate-lazy-eager-loading

Maybe you can share some example code?

Kennuel
  • 169
  • 7
  • I would have thought lazy would work but it causes a read error. Also I have the relationship as ManyToOne. Let me show the code in the updated Question. – John Dec 23 '19 at 16:39
  • Could you try to annotate your lazy loaded private property additonally with: `@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})` – Kennuel Dec 23 '19 at 20:09
0

Here are some ideas:

UPDATE (25.12.2019)

When FetchType.LAZY is used Hibernate returns a proxy. Here are some ways to work around the error you're getting:

MartinBG
  • 1,500
  • 13
  • 22
  • Thnak you. I think serializer is the way to go. Though from what I have now read, private fields in climate should be accessible because there are getters and setters, yet compiler errors - they are not visible because of private access. – John Dec 24 '19 at 10:19
  • 1
    I went with a custom serializer eventually. Seemed the most straightforward that worked. I also just ref'd the public getters and left the properties private. Again that worked and ref'ing the private properties directly just did not despite every example saying it woruld work. Marking this as the answer. – John Dec 30 '19 at 08:49