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

370
Visualizações
Does hibernate set back reference in @OneToMany @ManyToOne relationship automatically?

I have Order entity that has orderItems list.

@OneToMany(mappedBy ="order")
List<OrderItem> orderItems;

OrderItem has back reference to Order

@ManyToOne
Order order;

When I save Order should I manually set backreference in OrderItem entity? Like this

OrderItem orderItem1 = new OrderItem( //constructor );
OrderItem orderItem2 = new OrderItem( //constructor );
List orderItems = Arrays.asList(orderItem1, orderItem2);
Order order = new Order( orderItems);

orderItems.forEach(orderItem -> orderItem.setOrder(order); // like this?

Do Hibernate and Spring data jpa set it automatically?

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

0

For creating and updating case , they will not help you to set it automatically and you have to configure the relationship by yourself.

The mappedBy here is to define whether Order.orderItems or OrderItem.order is to used to provide the value for the corresponding DB column that link between them. (i.e. order_id column in order_item table).

If mappedBy is defined , OrderItem.order will be used to provide the value for the relationship. Otherwise , Order.orderItems will be used.

For the loading case , it will help you to set it automatically.

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