Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
Hibernate: One to many mapping is leading to circular objects

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?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda