0

Hello i am using spring with JPA and mysql i have a class FishJournal

@Entity
@Table(name = "journal")
public class FishJournal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;


@ManyToOne(targetEntity =User.class,cascade = CascadeType.ALL)
@JoinColumn(name="user_id", referencedColumnName = "id")
private User users;


@ManyToOne(targetEntity =Fish.class,cascade = CascadeType.ALL)
@JoinColumn(name="fish_id", referencedColumnName = "id")
private Fish fishes;


@ManyToOne(targetEntity =Water.class,cascade = CascadeType.ALL)
@JoinColumn(name="water_id", referencedColumnName = "id")
private Water waters;


@ManyToOne(targetEntity =Bait.class,cascade = CascadeType.ALL)
@JoinColumn(name="bait_id", referencedColumnName = "id")
private Bait baits;

Date date;

String info;
String path;

boolean shared;

getters and setter() {
    
 }

And I want it to have a unidirectional many-to-one relationship with user,water,bait and fish. I want to be able when given the FishJournal object to be able to get the data for those fields. The problem is when I try to use JPA to query a fishjournal from the database with jpa an infinity recursion occurs and the only way I was able to fix it was to change the getWaters,getUsers methods to return only the name and not the whole object.That isn't good enough because I want to be able to use the whole objects as getters.

agunimonx1
  • 67
  • 7
  • 1
    Learn the difference between _querying_ and _writing as JSON_, and then learn why DTOs or a comprehensive translation layer are important. – chrylis -cautiouslyoptimistic- Aug 05 '21 at 00:55
  • @chrylis-cautiouslyoptimistic- so with a DTO i would be able to write them to JSON format without getting recursion ? – agunimonx1 Aug 05 '21 at 01:11
  • JsonManagedReference and JsonBackReference is what you are lookin for. Please check this out Please have a look here https://stackoverflow.com/questions/37392733/difference-between-jsonignore-and-jsonbackreference-jsonmanagedreference – Simon Martinelli Aug 05 '21 at 08:53

0 Answers0