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

1.2K
Visualizações
Spring Data JPA save child without fetch the parent

How to save child entity with Spring data JPA repositories if you know the parent Id?

For example if we have one to many relation:

@Entity
public class Customer {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;
    @ManyToOne
    private CustomerCategory category;
}

@Entity
public class CustomerCategory {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String name;
}

One can do something like:

CustomerCategory someCategory = customerRepository.findOne(1L);// how to skip that line. The id should be enough. 
Customer cust = new Customer("Evgeni", "Dimitrov", someCategory);           
customerRepository.save(cust);

JPA can do load(just create a proxy and set the Id) instead of 'get'(select from the DB). Is that possible with Spring Data JPA?

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

0

Forget Id. You are now at the Object level of ORM (ObjectRelationMapping). Objects are composed of other objects. NO IDs.

about 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