0

Im practicing Relationship Mapping.

Implementaion: Im trying to get info of User is in infinite recursion with location and post. Solution for infinite recursion is adding @JsonBackReference but the location wont show in Users. Any idea for this? so i can get also the location data.

User class

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Users {
    
    @Id
    private Integer id;
    private String firstname;
    private String lastname;
    
    @JsonBackReference
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "location_id",insertable  =false, updatable  =false)
    private Location location;
    
    @JsonManagedReference
    @OneToMany(mappedBy = "users", cascade = CascadeType.ALL)
    private List<Posts> posts;
    private String email;
    private Integer location_id;
    

    
    
    

}

Location class

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import com.fasterxml.jackson.annotation.JsonManagedReference;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity

public class Location {
    
    @Id
    private Integer id;
    private String name;
    
    @JsonManagedReference
    @OneToMany(mappedBy = "location", cascade = CascadeType.ALL)
    private List<Users> users;
    
    


}

Post class


import java.time.LocalDateTime;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.fasterxml.jackson.annotation.JsonBackReference;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Posts {
    
    @Id
    private Integer id;
    private LocalDateTime postDate;
    
    @JsonBackReference
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "users_id",insertable  =false, updatable  =false)
    private Users users;
    private String details;
    private Integer users_id;
    
    

}


USer and post only show and i need also location data for this.

    @GetMapping("/{id}")
    public Optional<Users> getbyId(@PathVariable Integer id) {
        
        return userService.getbyId(id);
    }

This will work but not this implementation i want direct from user

    @GetMapping("/{id}/location")
    public Location getLocationByUsers(@PathVariable Integer id){
        
        Optional<Users> user = userService.getbyId(id);
        
        if (user.isPresent()) {
            return  user.get().getLocation();
        }
        return null;
    }
SpicySandwich
  • 49
  • 1
  • 10
  • 1
    Probably will be useful https://stackoverflow.com/questions/71991102/spring-data-jpa-findall-does-not-return-relations-data/71993222#71993222 – Eugene Jul 13 '22 at 19:35
  • 1
    @Eugene thanks , removing `@JsonBackReference` `@JsonManagedReference` and added `@JsonIdentityInfo` works – SpicySandwich Jul 14 '22 at 06:13

0 Answers0