3

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?

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
Shubham
  • 95
  • 13
  • 1
    `@JsonIdentityInfo`. This has nothing to do with Hibernate and everything to do with JSON serialization (Jackson). – chrylis -cautiouslyoptimistic- Dec 01 '20 at 04:50
  • Hibernate is working correctly; but, it is more a mis-mapping of the database model to the OO model on top of the DB backing store. – Edwin Buck Dec 01 '20 at 05:05
  • 1
    Take a look at similar questions: [Jackson serialize problem in @ManyToMany relationship](https://stackoverflow.com/questions/61392551/jackson-serialize-problem-in-manytomany-relationship/61398920#61398920), [serialization issue. Only first object of the same entity serializes well](https://stackoverflow.com/questions/63633994/jackson-serialization-issue-only-first-object-of-the-same-entity-serializes-wel/63640517#63640517), [Jackson/Hibernate meta get methods and serialization](https://stackoverflow.com/questions/55383889/jackson-hibernate-meta-get-methods-and-serialization/55386877#55386877) – Michał Ziober Dec 01 '20 at 15:48

1 Answers1

0

Since your House has an ApplicationUser and your ApplicationUser has a list of Houses, you've defined the classes to be circular.

Odds are that your Object Oriented model is 100% identical to the database model. This is probably a bad idea; as in a model of the home application, you wouldn't generally hold the application within the user embedded within the application.

Read What is “the inverse side of the association” in a bidirectional JPA OneToMany/ManyToOne association? for more details.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138