I have an use case where a single user can have multiple houses. This is how the models look like ApplicationUser.java
@Getter
@Setter
@Entity
public class ApplicationUser {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
@Column(nullable = false)
private String emailId;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "applicationUser", cascade = CascadeType.REMOVE)
private List<House> houses;
}
House.Java
@Getter
@Setter
@Entity
public class House {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
@ManyToOne()
@JoinColumn(name = "application_user")
private ApplicationUser applicationUser;
@Column(name = "House_Name")
private String houseName;
}
HouseRepository
public interface HouseRepository extends JpaRepository<House, Long> {
public House findByHouseName(String houseName);
public List<House> findAllByApplicationUser_Username(String userName);
}
Whenever I try to retrieve any house, the house object contains the user object and the user object again contains the house object. This goes on infinitely.
{
"id": 3,
"applicationUser": {
"id": 2,
"username": "test",
"emailId": "testmail",
"houses": [
{
"id": 3,
"applicationUser": {
"id": 2,
"username": "test",
"emailId": "testmail",
"houses": [
{
"id": 3,
"applicationUser": {
"id": 2,
"username": "test",
"emailId": "testmail",
"houses": [
How do I stop that from happening?