1

When I try to read from a database table into a @Entity Backtest, I get the following error:

{"timestamp":"2021-01-13T00:42:52.516+00:00","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: net.tekknow.moneymachine.model.JsonResponse[\"data\"]->java.util.ArrayList[0]->net.tekknow.moneymachine.model.Backtest[\"strategy\"]->net.tekknow.moneymachine.model.Strategy$HibernateProxy$NY2C2PBb[\"hibernateLazyInitializer\"])","details":"uri=/api/v1/backtests"}

The @Entity backtest contains a property "Strategy strategy". From the error message, I'm guessing it doesn't like the "strategy" field which exists in the Backtest entity, but not in the corresponding mysql table. All properties are public and contain getters and setters.

@Entity
@Table(name="backtest")
public class Backtest {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id;
    
    @JsonManagedReference       //This prevents an "infinite recursion" error.  Might not need it anymore because I've since added FetchType.LAZY
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="strategy_id", nullable=false) //strategy_id will be the name of the foreign key in the backtest table that points to the primary key of the strategy table
    Strategy strategy;

The hibernate save() command creates and stores the data into the table correctly. All columns match the Backtest properties, except of course, for the "strategy" property which is an object. But hibernate also creates a foreign key called "strategy_id" in its place. I suspect that reading the data back into the same @Entity Backtest is causing the serialization error.

@GetMapping("/backtests") 
public JsonResponse getUserBacktests(@RequestParam int userId) {
    List<Backtest> backtests = backtestService.getUserBacktests(userId);
    JsonResponse response = new JsonResponse();
    response.setStatus("success");
    response.setData(backtests);
    return response;
}

This has to be a common problem. Is there a common solution?

Mário Garcia
  • 553
  • 3
  • 13
user3217883
  • 1,216
  • 4
  • 38
  • 65
  • Does this answer your question? [No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor](https://stackoverflow.com/questions/52656517/no-serializer-found-for-class-org-hibernate-proxy-pojo-bytebuddy-bytebuddyinterc) – SternK Jan 13 '21 at 16:47
  • No, I don't think so. My question is about trying to use hibernate to populate the same Backtest entity that it used to save it. When it saves it, it creates a new foreign key in the table. I believe this field is what is preventing it from binding during the db read. – user3217883 Jan 13 '21 at 21:37

0 Answers0