I have my main object:
@Data
@Entity
@EqualsAndHashCode(callSuper = false)
@Table(name = MAIN_OBJ)
public class MainObj {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "MAIN_OBJ_ID", unique = true, nullable = false)
private Integer manObjId;
@ManyToOne
@JoinColumn(name = "SUB_OBJ_ID", referencedColumnName = "SUB_OBJ_ID", nullable = false)
@NotNull
private SubObj subObj;
}
and it's sub object:
@Data
@Entity
@EqualsAndHashCode(callSuper = false)
@Table(name = SUB_OBJ)
public class SubObj {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "SUB_OBJ_ID", unique = true, nullable = false)
private Integer subObjId;
@Column(name = "NAME", nullable = false)
@NotNull
private String name;
}
When I do a creation:
@Override
@Transactional
public MainObj create(MainObj mainObj) {
MainObj createdMainObj = mainObjRepository.save(mainObj);
return createdMainObj;
}
It saves properly but when I return it to the user (back through the controller) I get a 500 back. After debugging I can see that all the fields in the subObj are null.
But it does actually create the mainObj and put it into the db, it just fails to serialize it and return.
If I trace it enough I see:
No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer
Not really sure what I need to add so I can return the fully created object with it's foreign key reference to the subObj back to the user.
I tried Hibernate.initialize() like a lot of posts said but no luck.
If it wasn't clear it gives me one of these back:
Full stack trace (some details removed):
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.path.to.MainObj["subObj"]-com.path.to.SubObj_$$_jvsta14_f["handler"])